
sql server 2008如何设置外码
用关键语句: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*/
扩展资料
SQL Server 有两个接口可以认为是 SQL Server 7.0 的本机接口,即 OLE-DB 和 ODBC。DB-Library 接口也是本机的,它与 TDS 通信,但是 DB-Library 使用的是 TDS 较老的版本,需要在服务器上进行一些转换。
现有的 DB-Library应用程序仍然可以继续与 SQL Server 7.0 协同使用,但是很多新的功能和性能提高等好处只能通过 ODBC 和 OLE DB 才能利用。
参考资料来源:百度百科-Microsoft SQL Server
下边举例说明:
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*/
);