ifexists(select * from sysobjects wherename= 'Students') droptable Students go
createtable Students ( StudentId intidentity(100000,1),--学号 StudentName varchar(20) notnull,--姓名 Gender char(2) notnull, --性别 Birthday datetime notnull,--出生日期 StudentIdNo numeric(18,0) notnull,--身份证号 Age intnotnull,--年龄。其实可以通过身份证号来动态获取 PhoneNumber varchar(50),--电话号码 StudentAddress varchar(500),--地址 ClassId intnotnull--班级外键 ) go
--创建班级表 ifexists(select * from sysobjects wherename='StudentClass') droptable StudentClass go createtable StudentClass ( ClassId int primary key,--班级编号 ClassName varchar(20) notnull--班级名称
) go
--创建成绩表 ifexists(select * from sysobjects wherename='ScoreList') droptable ScoreList go createtable ScoreList ( Idintidentity(1,1) primary key, StudentId intnotnull,--学号外键 CSharp intnull, SQLServer intnull, UpdateTime datetime notnull,--更新时间 ) go
--创建管理员表 ifexists(select * from sysobjects wherename='Admins') droptable Admins go createtable Admins ( LoginId intidentity(1000,1) primary key, LoginPwd varchar(20) notnull,--登录密码 AdminName varchar(20) notnull ) go
--添加相关约束 --创建主键约束 use TestDB go ifexists(select * from sysobjects wherename='pk_StudentId') altertable Students dropconstraint pk_StudentId
altertable Students addconstraint pk_StudentId primary key(StudentId)
--添加相关约束 --创建唯一约束 use TestDB go ifexists(select * from sysobjects wherename='uq_StudentsIdNo') altertable Students dropconstraint uq_StudentsIdNo altertable Students addconstraint uq_StudentsIdNo unique(StudentIdNo)