alter tb add constraint fk_tb_col foreign key(fkcol) references tb(fkcol) 可能是你有一个表里有不唯一的值.
只能在a表里添加外键关系,使a1对应b1,不能在b表里添加外键关系。
没有重复呀 我直接拿我写的 大家帮我看看吧 if exists (select * from sysdatabases where name='school') drop database school go create database school on ( name='school_data', filename='e:\data\school_data.mdf', size=3mb, filegrowth=15% ) log on ( name='school_log', filename='e:\data\school_log.ldf', size=1mb, filegrowth=15% ) go use school go if exists(select * from sysobjects where name='stu') drop table stu go create table stu ( SNo int constraint PK_Sno primary key, SName nvarchar(10) not null, Sex nvarchar(5) not null constraint CK_Sex check(Sex in('男','女')), Age int constraint CK_Age check(Age between 0 and 100), Native nvarchar(10) ) go if exists(select * from sysobjects where name='teacher') drop table teacher go create table teacher ( TNO int constraint PK_TNO primary key, TName nvarchar(10) not null, Post varchar(5), Sex varchar(5) constraint CK_Sex1 check(Sex in('男','女')), Age int constraint CK_Age1 check(Age between 0 and 100) ) go if exists(select * from sysobjects where name='course') drop table course go create table course ( Cno int, CName nvarchar(10), Tno int constraint FK_Tno foreign key(Tno) references teacher(Tno), Period float, primary key(Cno,Tno) ) go if exists(select * from sysobjects where name='study') drop table study go create table study ( SNo int constraint FK_SNo2 foreign key(SNo) references stu(SNo), CNo int constraint FK_CNo foreign key(CNo) references course(Tno), Grade int constraint CK_Grade check(grade between 0 and 100), primary key(Sno,Cno) ) go
CNo int constraint FK_CNo foreign key(CNo) references course(Tno), 这里去掉约束就不报错 加上就报错
CNo int constraint FK_CNo foreign key(CNo) references course(Cno)
......我的意思是把 CNo int constraint FK_CNo foreign key(CNo) references course(Tno), 里的Tno改为Cno
你的数据库设计上有点小问题. 将课程表中的课程编号与教师编号合并设置为主键本身没什么问题,但如果将该表的Cno与Study表中的单一列Cno建立主从关系就不行了,因为Cno不是主键. 如果将Study表的Cno与课程表中的这个主键建立连接也不行,因为Cno只有一个列,而主键是两列. 解决的办法有两个: 一是课程表中只用Cno作主键,不同教师教授的课程分作不同的课程编号,可同一课程编号与可容纳几名教师.这样,可以将学习表中的课程号与该主键建立主从关系; 二是在学习表中同样建立Tno列,将Cno,Tno两列与课程表中的由两列组成的主键建立主从关系,这样处理,课程表维持原样,学习表作改动,从理论上讲更符合你的要求.从数据库的角度看,既然你要与课程表建立连接,而课程表中决定课程的是课程编号和教师编号两个属性,那你的外键当然出得用同样的两个属性,否则外键是不正确的. 如果用多个列作外键,约束必须定义为表级约束. create table study ( SNo int constraint FK_SNo2 foreign key(SNo) references stu(SNo), CNo int, Tno int, Grade int constraint CK_Grade check(grade between 0 and 100), primary key(Sno,Cno), constraint FK_CNo foreign key(CNo,Tno) references course(Cno,Tno), ) go
b1是主键,是唯一的。
可能是你有一个表里有不唯一的值.
if exists (select * from sysdatabases where name='school')
drop database school
go
create database school
on
(
name='school_data',
filename='e:\data\school_data.mdf',
size=3mb,
filegrowth=15%
)
log on
(
name='school_log',
filename='e:\data\school_log.ldf',
size=1mb,
filegrowth=15%
)
go
use school
go
if exists(select * from sysobjects where name='stu')
drop table stu
go
create table stu
(
SNo int
constraint PK_Sno primary key,
SName nvarchar(10) not null,
Sex nvarchar(5) not null
constraint CK_Sex check(Sex in('男','女')),
Age int
constraint CK_Age check(Age between 0 and 100),
Native nvarchar(10)
)
go
if exists(select * from sysobjects where name='teacher')
drop table teacher
go
create table teacher
(
TNO int
constraint PK_TNO primary key,
TName nvarchar(10) not null,
Post varchar(5),
Sex varchar(5)
constraint CK_Sex1 check(Sex in('男','女')),
Age int
constraint CK_Age1 check(Age between 0 and 100)
)
go
if exists(select * from sysobjects where name='course')
drop table course
go
create table course
(
Cno int,
CName nvarchar(10),
Tno int
constraint FK_Tno foreign key(Tno) references teacher(Tno),
Period float,
primary key(Cno,Tno)
)
go
if exists(select * from sysobjects where name='study')
drop table study
go
create table study
(
SNo int
constraint FK_SNo2 foreign key(SNo) references stu(SNo),
CNo int
constraint FK_CNo foreign key(CNo) references course(Tno),
Grade int
constraint CK_Grade check(grade between 0 and 100),
primary key(Sno,Cno)
)
go
constraint FK_CNo foreign key(CNo) references course(Tno),
这里去掉约束就不报错 加上就报错
constraint FK_CNo foreign key(CNo) references course(Cno)
......我的意思是把
CNo int
constraint FK_CNo foreign key(CNo) references course(Tno),
里的Tno改为Cno
将课程表中的课程编号与教师编号合并设置为主键本身没什么问题,但如果将该表的Cno与Study表中的单一列Cno建立主从关系就不行了,因为Cno不是主键.
如果将Study表的Cno与课程表中的这个主键建立连接也不行,因为Cno只有一个列,而主键是两列.
解决的办法有两个:
一是课程表中只用Cno作主键,不同教师教授的课程分作不同的课程编号,可同一课程编号与可容纳几名教师.这样,可以将学习表中的课程号与该主键建立主从关系;
二是在学习表中同样建立Tno列,将Cno,Tno两列与课程表中的由两列组成的主键建立主从关系,这样处理,课程表维持原样,学习表作改动,从理论上讲更符合你的要求.从数据库的角度看,既然你要与课程表建立连接,而课程表中决定课程的是课程编号和教师编号两个属性,那你的外键当然出得用同样的两个属性,否则外键是不正确的.
如果用多个列作外键,约束必须定义为表级约束.
create table study
(
SNo int
constraint FK_SNo2 foreign key(SNo) references stu(SNo),
CNo int,
Tno int,
Grade int
constraint CK_Grade check(grade between 0 and 100),
primary key(Sno,Cno),
constraint FK_CNo foreign key(CNo,Tno) references course(Cno,Tno),
)
go
一是课程表中只用Cno作主键,不同教师教授的课程分作不同的课程编号,可同一课程编号与可容纳几名教师.这样,可以将学习表中的课程号与该主键建立主从关系;
中的"可"是笔误,应为:一是课程表中只用Cno作主键,不同教师教授的课程分作不同的课程编号,也可在同一课程编号下容纳几名教师.这样,可以将学习表中的课程号与该主键建立主从关系;
抱歉.
这样的主外键关系才是被允许的.