数据库设置外码实例

用关键语句: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*/***

); 
  • 8
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值