--上机练习1
--创建数据库
use master
if exists(select * from sysdatabases where name='MySchool')
drop database MySchool
create database MySchool
on primary
(
--主数据文件的具体描述
name='MySchool_data',
filename='D:\\MySchool_data.mdf',
size=10mb,
filegrowth=20%)
log on(
--次数据文件的具体描述
name='MySchool_log',
filename='D:\\MySchool_log.ldf',
size=3mb,
maxsize=20mb,
filegrowth=1
)
--上机练习2
--创建Subject表
use MySchool
go
create table Subject
(
SubjectNo int not null,
SubjectName nvarchar(50) null,
ClassHour int null,
GradeID int null
)
--上机练习3
--创建Result表
use MySchool
go
create table Result
(
StudentNo int not null,
SubjectNo int not null,
ExamDate datetime not null,
StudentResult int not null
)
--上机练习4
--创建Student表
use MySchool
go
create table Student
(
StudentNo int not null,
LoginPwd nvarchar(50) not null,
StudentName nvarchar(50) not null,
Sex bit not null,
GradeID int not null,
Phone varchar(50) null,
Address nvarchar(255) null,
BornDate datetime not null,
Email varchar(50) null,
IdentityCard varchar(18) not null
)
--创建Grade表
use MySchool
go
create table Grade
(
GradeID int not null,
GradeName nvarchar(50) not null
)
--------------------------添加数据-------------------
--Grade表
INSERT INTO Grade (GradeID,GradeName)
values(1,'S1')
INSERT INTO Grade (GradeID,GradeName)
values(2,'S1')
INSERT INTO Grade (GradeID,GradeName)
values(3,'S1')
--Subject表
INSERT INTO Subject(SubjectNo,SubjectName,ClassHour,GradeID) VALUES (1,'英语',20,1)
INSERT INTO Subject(SubjectNo,SubjectName,ClassHour,GradeID) VALUES (2,'数学',30,1)
INSERT INTO Subject(SubjectNo,SubjectName,ClassHour,GradeID) VALUES (3,'化学',10,1)
--Student表 0代表男 1 代表女
INSERT INTO Student (StudentNo,LoginPwd,StudentName,Sex,GradeID,Phone,Address,BornDate,Email,IdentityCard)
VALUES (1,'000000','苏琳琳',1,1,'18210392217','北京海淀','1999-09-24','[email protected]','211022199909242925')
INSERT INTO Student (StudentNo,LoginPwd,StudentName,Sex,GradeID,Phone,Address,BornDate,Email,IdentityCard)
VALUES (2,'000000','苏苏',1,1,'18210392217','辽宁辽阳','1999-09-24','[email protected]','211022199909242926')
INSERT INTO Student (StudentNo,LoginPwd,StudentName,Sex,GradeID,Phone,Address,BornDate,Email,IdentityCard)
VALUES (3,'000000','琳琳',1,1,'18210392217','大东北','1999-09-24','[email protected]','211022199909242927')
--Result表
INSERT INTO Result(StudentNo,SubjectNo,ExamDate,StudentResult)
VALUES (1,1,'2017-01-23',99)
INSERT INTO Result(StudentNo,SubjectNo,ExamDate,StudentResult)
VALUES (1,2,'2017-01-24',95)
INSERT INTO Result(StudentNo,SubjectNo,ExamDate,StudentResult)
VALUES (2,1,'2017-01-25',92)
--上机练习5
----------------------------------------- --Grade表 的主键约束
alter table Grade
add constraint PK_GradeID primary key(GradeID)
go
---------------------------------------------Student 表的约束
--主键约束:学号
alter table Student
add constraint PK_IdentityCard unique (IdentityCard)
--唯一约束:身份证号
alter table Student
add constraint UQ_StudentNo primary key(StudentNo)
--默认约束 不填写则为“地址不详”
alter table Student
add constraint DF_Address default('地址不详') for Address
--检查约束 要求出生日期在1980-01-01之后
alter table Student
add constraint CK_BornDate check(BornDate>='1980-01-01')
--外键约束: 主表Grade 和从表Student 通过GradeID列建立引用关系
alter table Student
add constraint FK_GradeID foreign key(GradeID) references Grade(GradeID)
------------------------------------Subject 表的约束
--主键约束
alter table Subject
add constraint PK_SubjectNo primary key(SubjectNo)
--检查约束
alter table Subject
add constraint CK_ClassHour check(ClassHour>0)
--外键约束
alter table Subject
add constraint FK_GradeID foreign key(GradeID) references Grade(GradeID)
---Result表的约束
--主键约束
alter table Result
add constraint PK_ExamDate primary key(ExamDate)
--默认约束
alter table Result
add constraint DF_ExamDate default(GetDate()) for ExamDate
--检查约束
alter table Result
add constraint CK_StudentResult check(StudentResult>=0 and StudentResult<=100)
--外键约束
alter table Result
add constraint FK_StudentNo foreign key(StudentNo) references Student(StudentNo)
alter table Result
add constraint FK_SubjectNo foreign key(SubjectNo) references Subject(SubjectNo)