[关闭]
@rg070836rg 2018-12-03T16:32:04.000000Z 字数 3848 阅读 829

数据库创建

数据库实验


  1. use Students;
  2. drop table Student;
  3. drop table Course;
  4. drop table SC;
  5. create table Student
  6. (
  7. sno char(8) not null primary key,
  8. Sname char(15) null,
  9. Ssex char(4) null,
  10. Sage int null,
  11. Sdept char(10) null
  12. )
  13. create table Course
  14. (
  15. Cno char(6) not null,
  16. Cname char(10),
  17. Cpno int null,
  18. Ccredit int null,
  19. primary key(cno),
  20. )
  21. go
  22. create table SC
  23. (
  24. Sno char(8) not null primary key,
  25. Cno int null,
  26. Grade int null
  27. )
  28. insert into Student values('19130201','丁鹏','男',20,'Software')
  29. insert into Student values('19130202','王韵婷','女',20,'Software')
  30. insert into Student values('19130203','尹嘉琪','男',18,'Software')
  31. insert into Student values('19130204','卢冬冬','男',20,'Software')
  32. insert into Student values('19130205','史逸凡','男',19,'Software')
  33. insert into Course values('1','数据库',5,4)
  34. insert into Course values('2','数学',null,2)
  35. insert into Course values('3','信息系统',1,4)
  36. insert into Course values('4','操作系统',6,3)
  37. insert into Course values('5','数据结构',7,5)
  38. insert into Course values('6','数据处理',null,2)
  39. insert into Course values('7','PASCAL语言',6,4)
  40. insert into SC values('19130201',1,99)
  41. insert into SC values('19130202',5,95)
  42. insert into SC values('19130203',3,100)
  43. insert into SC values('19130205',1,93)
  44. insert into SC values('19130204',5,92)
  45. select Sno as '学号',Sname as '姓名',Ssex as '性别',Sage as'年龄',Sdept as '所在系' from Student
  46. select Cno'课程号',Cname'课程名',Cpno'先行课',Ccredit'学分' from Course
  47. select * from SC
  48. update Student set Sage=22 where sno='19130202'
  49. delete from Student where sno='19130205'
  50. ///////////////////////////////////////////////////////
  51. select * from student
  52. alter table student add phone char(15) null,qq varchar(12) //给表增加一个列
  53. alter table student alter column phone int null //更改列的类型
  54. execute sp_rename 'student.phone','phnum','column' //修改列名
  55. alter table student drop column qq //删除表中的类
  56. alter table student add constraint sex check (sex in('男''女'))//给列增加一个check约束
  57. alter table student add constraint pk_student_sno primary key(sno) //构建主键
  58. alter table student alter column sno char(9) not null
  59. alter table student drop constraint pk_student_sno //删除主键
  60. create table spe(id char(6) primary kry,name char(10) null) //创建表格
  61. sp_rename 'spe','special' //修改表名
  1. select * from student
  2. alter table student add phone char(15) null,qq varchar(12) //给表增加一个列
  3. alter table student alter column phone int null //更改列的类型
  4. execute sp_rename 'student.phone','phnum','column' //修改列名
  5. alter table student drop column qq //删除表中的类
  6. alter table student add constraint sex check (sex in('男''女'))//给列增加一个check约束
  7. alter table student add constraint pk_student_sno primary key(sno) //构建主键
  8. alter table student alter column sno char(9) not null
  9. alter table student drop constraint pk_student_sno //删除主键
  10. create table spe(id char(6) primary kry,name char(10) null) //创建表格
  11. sp_rename 'spe','special' //修改表名

1 建表

  1. create database Students
  2. on
  3. primary(name=Students,
  4. filename='D:\test\test.mdf',
  5. size=4mb,
  6. maxsize=10mb,
  7. filegrowth=2mb
  8. )
  9. log on
  10. (name=Studentslog,
  11. filename='D:\test\testlog.ldf',
  12. size=1mb,
  13. maxsize=5mb,
  14. filegrowth=1mb
  15. )
  16. use Students;
  17. create table Student
  18. (
  19. sno char(8) not null primary key,
  20. Sname char(15) null,
  21. Ssex char(4) null,
  22. Sage int null,
  23. Sdept char(10) null
  24. )
  25. create table Course
  26. (
  27. Cno char(6) not null,
  28. Cname char(10),
  29. Cpno char(6),
  30. Ccredit int null,
  31. primary key(cno),
  32. )
  33. go
  34. create table SC
  35. (
  36. Sno char(8) not null foreign key references Student(sno),
  37. Cno char(6) not null foreign key references course(cno),
  38. primary key(Sno,Cno),
  39. Grade int null
  40. )
  41. insert into Student values('19130201','丁鹏','男',20,'Software')
  42. insert into Student values('19130202','王韵婷','女',20,'Software')
  43. insert into Student values('19130203','尹嘉琪','男',18,'Software')
  44. insert into Student values('19130204','卢冬冬','男',20,'Software')
  45. insert into Student values('19130205','史逸凡','男',19,'Software')
  46. insert into Course values('1','数据库',5,4)
  47. insert into Course values('2','数学',null,2)
  48. insert into Course values('3','信息系统',1,4)
  49. insert into Course values('4','操作系统',6,3)
  50. insert into Course values('5','数据结构',7,5)
  51. insert into Course values('6','数据处理',null,2)
  52. insert into Course values('7','PASCAL语言',6,4)
  53. insert into SC values('19130201',1,99)
  54. insert into SC values('19130202',5,95)
  55. insert into SC values('19130203',3,100)
  56. insert into SC values('19130205',1,93)
  57. insert into SC values('19130204',5,92)
  58. alter table course add foreign key(cpno) references course(cno)
  59. select * from Course;
  60. select * from SC;
  61. select * from Student;
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注