消息 547,级别 16,状态 0,第 1 行 INSERT 语句与 FOREIGN KEY 约束"FK__Tb_Studen__Stude__1920BF5C"冲

create database Mstanford
create table Tb_Student
(
StudentNo int IDENTITY(1,1) PRIMARY KEY,
StudentName varchar(20) not null,
StudentAge int check(StudentAge>20 and StudentAge<30),
Country varchar(20) default'中国',
StuTime datetime not null,
Tuitime money not null
)create table Tb_Student_Course
(
SCNo int IDENTITY(1,1) PRIMARY KEY,
StudentNo int references Tb_Student(StudentNo),
Coursename varchar(30) unique not null,
CourseTime datetime not null,

Notes varchar(1000)
)
insert Tb_Student VALUES('凯奇',26,'法国','2009-09-01',4000.00)
insert Tb_Student VALUES('saha',26,'印度','2009-09-01',1000.50)
insert Tb_Student VALUES('张小飞',26,'中国','2009-09-01',2000.50)
insert Tb_Student VALUES('李刚',26,'中国','2009-09-01',7000.50)

insert Tb_Student_Course VALUES(200709002,'java','2009-09-09','通过')
insert Tb_Student_Course VALUES(200709003,'.net','2009-09-09','通过')
insert Tb_Student_Course VALUES(200709004,'jsp','2009-09-09',null)

StudentNo int references Tb_Student(StudentNo),
的意思是Tb_Student_Course表StudentNo的值必须是Tb_Student表StudentNo字段已有的值、
但是你Tb_Student表的主键StudentNo的值是自增的、所以按你的插入语句Tb_Student_Course表StudentNo的值只能取1,2,3,4

不要把具有实际意义的字段设置为自增主键、因为自增主键值不用自己插入、而是按照
插入的顺序自动编号。你现在要么按规则取值、要么把表结构换了重新定义主键、外键。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-29
你的这个表Tb_Student_Course中的字段StudentNo 是与Tb_Student表是外表约束关系~
也就是说insert Tb_Student_Course VALUES(200709002,'java','2009-09-09','通过')
中的200709002这数据必须在表Tb_Student中存在~
相似回答