资源描述:
《SQLServer创建数据表》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、/*SQLServer中关键字、表名、列名都是不区分大小写。如果想要在脚本命令区分大小写可执行命令:alterdatabase数据库名collateChinese_PRC_CI_AS*/--11311110805-软件技术班-骆巍createdatabasetestonprimary(name=testdata,filename='D:javaDatabaseDATAtestdata.mdf',size=10,maxsize=500,filegrowth=5)logon(name=testLog,filename='D:javaDatabaseDATAt
2、estLog.ldf',size=3,filegrowth=10%)--创建数据表useclass--选择数据库gocreatetableDepartment--创建表(DepartmentIDchar(4)primarykey,--primarykey是设置主键DepartmentNamevarchar(20)notnullunique,--unique是设置唯一DepartmentHeadervarchar(8)notnull,--notnull是非空TeacherNumint)useclassgocreatetableClass(ClassIDchar(8)pri
3、marykey,ClassNamevarchar(20)notnull,Monitorchar(8),StudentNumintcheck(StudentNum>=0),--check约束DepartmentIDchar(4)foreignkeyreferencesDepartment(DepartmentID),--foreignkey定义外键references用于指定外键所引用的表及表的主键)useclassgocreatetableStudent(StudentIDchar(12)primarykey,StudentNamechar(8)notnull,Sexc
4、har(2)notnullcheck(Sex='男'orSex='女'),Birthdatenotnull,HomeAddrvarchar(80),EntranceTimedatedefaultgetdate(),--getdate()是得到系统时间的方法ClassIDchar(8)referencesClass(ClassID)--行级约束时可以省略foreignkey)createtableCourse(CourseIDchar(8)primarykey,CourseNmaevarchar(60)notnull,BookNamevarchar(80)notnull,
5、Creditintnotnull)createtableGrade(CourseIDchar(8)referencesCourse(CourseID),StudentIDchar(12)referencesStudent(StudentID),Semesterintnotnull,SchoolYearint,Gradenumeric(5,1)check(Grade>=0),primarykey(CourseID,StudentID)--在定义完CourseID和StudentID列后定义主键约束,这种约束为表级约束)useclassgocreatetableTeache
6、r(TeacherIDchar(8)primarykey,TeacherNamechar(8)notnull,Sexchar(2)notnullcheck(Sex='男'orSex='女'),Birthdate,Professionchar(8)check(Professionin('教授','副教授','讲师','助教')),--还可以这样写(Profession='教授'orProfession='副教授'orProfession='讲师'orProfession='助教')Telephonevarchar(20),HomeAddrvarchar(50),Depar
7、tmentIDchar(4)referencesDepartment(DepartmentID))createtableSchedule(TeacherIDchar(8)referencesTeacher(TeacherID),CourseIDchar(8)referencesCourse(CourseID),ClassIDchar(8)referencesClass(ClassID),Semesterintnotnull,SchoolYearintnotnull,ScheduleIdentvarchar(40)notnull,Class