SQL Server使用介紹(六)

今天我們將剩下的表都創(chuàng)建好,出了之前的AccpStudent,AccpClass表,還有AccpSubject(課程表),AccpScore(成績(jī)表),以及他們之間的引用關(guān)系.
首先創(chuàng)建AccpSubject表
create table AccpSubject
(
 sid int identity(1,1) primary key, --課程編號(hào)
 subName varchar(50) not null  --課程名稱
)
go

create table AccpScore
(
 sid int identity(1,1), --成績(jī)編號(hào)
 subId int not null, --課程Id
 stuId int not null, --學(xué)生Id
 Score int check(Score>=0 and Score<=100), --成績(jī)
)
go
然后再創(chuàng)建表和表之間的引用關(guān)系,這里有2個(gè)引用關(guān)系需要?jiǎng)?chuàng)建
第一,是AccpScore表和accpStudent之間的引用關(guān)系,創(chuàng)建引用關(guān)系代碼如下:
alter table AccoScore
add constrint FK_AccpScore_AccpStudent foreign key(stuId) references AccpStudent(sid)
go

第二,是AccpScore表和AccpSubject課程表之間的引用關(guān)系,創(chuàng)建引用關(guān)系代碼如下:
alter table AccoScore
add constrint FK_AccpScore_AccpSubject foreign key(subId) references AccpSubject(sid)
go
這樣到目前為止,所有的數(shù)據(jù)庫(kù)和表都創(chuàng)建好了。
下次,我們講解一下如何創(chuàng)建索引。

 

北大青鳥網(wǎng)上報(bào)名
北大青鳥招生簡(jiǎn)章