在SQL中,如何在复合主键上创立外键?
外键如何和表的复合主键建立关系createtables(sidvarchar(20),snamevarchar(20),ssexvarchar(2)check(ssex=...
外键如何和表的复合主键建立关系
create table s
(
sid varchar(20),
sname varchar(20),
ssex varchar(2) check(ssex='男' or ssex='女') default '男',
sage int check(sage between 0 and 100),
sclass varchar(20),
constraint event_key primary key (sid,sclass)----创建复合主键
)
create table t
(
teacher varchar(20) primary key,
tclass varchar(20) foreign key references s(sclass),----复合外键怎么创?
num int
)
外键不可以单一的对应复合主键里的一个键而建立么吗? 展开
create table s
(
sid varchar(20),
sname varchar(20),
ssex varchar(2) check(ssex='男' or ssex='女') default '男',
sage int check(sage between 0 and 100),
sclass varchar(20),
constraint event_key primary key (sid,sclass)----创建复合主键
)
create table t
(
teacher varchar(20) primary key,
tclass varchar(20) foreign key references s(sclass),----复合外键怎么创?
num int
)
外键不可以单一的对应复合主键里的一个键而建立么吗? 展开
2个回答
展开全部
--不能一列引用两列,必髯数列对应
use tempdb
go
create table s
(
sid varchar(20),
sname varchar(20),
ssex varchar(2) check(ssex='男' or ssex='女') default '男',
sage int check(sage between 0 and 100),
sclass varchar(20),
constraint event_key primary key (sid,sclass)----创建复合主键
)
create table t
(
teacher varchar(20) primary key,
sid varchar(20) not null,
sclass varchar(20) not null,
num int,
foreign key(sid,sclass) references s(sid,sclass)
)
use tempdb
go
create table s
(
sid varchar(20),
sname varchar(20),
ssex varchar(2) check(ssex='男' or ssex='女') default '男',
sage int check(sage between 0 and 100),
sclass varchar(20),
constraint event_key primary key (sid,sclass)----创建复合主键
)
create table t
(
teacher varchar(20) primary key,
sid varchar(20) not null,
sclass varchar(20) not null,
num int,
foreign key(sid,sclass) references s(sid,sclass)
)
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
语法:
①创建时:create table sc (
studentno int,
courseid int,
score int,
foreign key (courseid) );
②修改时:
ALTER TABLE news_info[子表名] ADD CONSTRAINT FK_news_info_news_type[约束名] FOREIGN KEY (info_id)[子表列] REFERENCES news_type[主表名] (id)[主表列] 。
①创建时:create table sc (
studentno int,
courseid int,
score int,
foreign key (courseid) );
②修改时:
ALTER TABLE news_info[子表名] ADD CONSTRAINT FK_news_info_news_type[约束名] FOREIGN KEY (info_id)[子表列] REFERENCES news_type[主表名] (id)[主表列] 。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询