SQL Server使用介绍(六)

今天我们将剩下的表都创建好,出了之前的AccpStudent,AccpClass表,还有AccpSubject(课程表),AccpScore(成绩表),以及他们之间的引用关系.
首先创建AccpSubject表
create table AccpSubject
(
 sid int identity(1,1) primary key, --课程编号
 subName varchar(50) not null  --课程名称
)
go

create table AccpScore
(
 sid int identity(1,1), --成绩编号
 subId int not null, --课程Id
 stuId int not null, --学生Id
 Score int check(Score>=0 and Score<=100), --成绩
)
go
然后再创建表和表之间的引用关系,这里有2个引用关系需要创建
第一,是AccpScore表和accpStudent之间的引用关系,创建引用关系代码如下:
alter table AccoScore
add constrint FK_AccpScore_AccpStudent foreign key(stuId) references AccpStudent(sid)
go

第二,是AccpScore表和AccpSubject课程表之间的引用关系,创建引用关系代码如下:
alter table AccoScore
add constrint FK_AccpScore_AccpSubject foreign key(subId) references AccpSubject(sid)
go
这样到目前为止,所有的数据库和表都创建好了。
下次,我们讲解一下如何创建索引。

 

北大青鸟网上报名
北大青鸟招生简章