用关键语句:foreign key …(属性名) references …表名+(属性名)
下边举例说明:
create table Student /*建立一个学生表*/
(
Sno char(8),
Sname char(6) unique not null default 'wang',
Ssex char(2) check(Ssex='男'or Ssex='女') not null,
Sage smallint not null check(Sage<150),
Sdept char(20) not null,
primary key(Sno)
);
create table Course /*建立课程表*/
(Cno char(3) primary key ,
Cname char(20) not null,
Cpno char(3) foreign key references Course(Cno), /*这里是自引用主码*/
Ccredit smallint,
--foreign key Cpno references Course(Cno)
);
create table SC /*建立学生选课表*/
(Sno char(8) ,
Cno char(3) not null ,
Grade smallint not null,
--foreign key Sno references Student(Sno)
primary key (Sno,Cno), /*主码由两个属性构成*/
***foreign key (Sno) references Student(Sno), /*引用学生表的主码Sno*/
foreign key (Cno) references Course(Cno) /*引用课程表的主码Cno*/***
);