INSERT 语句与 FOREIGN KEY 约束"FK__sc__sno__0425A276"冲突。该冲突发生于数据库"we",表"dbo.student",
我应该如何插入数据啊?createdatabasewecreatetablestudent(snochar(9)primarykey,snamechar(20)uniqu...
我应该如何插入数据啊?
create database we
create table student
(
sno char(9) primary key,
sname char(20) unique,
ssex char(2),
sage smallint,
sdept char(20)
);
insert into student(sno,sname,ssex,sage,sdept)
values('20025121','李勇','男','20','CS');
insert into student(sno,sname,ssex,sage,sdept)
values('20025122','刘晨','女','19','CS');
insert into student(sno,sname,ssex,sage,sdept)
values('20025123','王敏','女','18','MA');
insert into student(sno,sname,ssex,sage,sdept)
values('20025125','张立','男','19','IS');
create table course
(
cno char(4) primary key,
came char(40),
cpno char(4),
ccredit smallint,
foreign key (cpno) references course(cno)
);
insert into course(cno,came,cpno,ccredit)
values('2','数学',null,'2');
insert into course(cno,came,cpno,ccredit)
values('6','数据处理',null,'2');
insert into course(cno,came,cpno,ccredit)
values('4','操作系统','6','3');
insert into course(cno,came,cpno,ccredit)
values('7','pascal语言','6','4');
insert into course(cno,came,cpno,ccredit)
values('5','数据结构','7','4');
insert into course(cno,came,cpno,ccredit)
values('1','库数据','5','4');
insert into course(cno,came,cpno,ccredit)
values('3','信息系统','1','4');
select * from course
create table sc
(
sno char(9),
cno char(4),
Grade smallint,
primary key (sno,cno),
foreign key (sno) references student(sno),
foreign key (cno) references course(cno)
);
insert into sc(sno,cno,grade)
values('20025121','1',92);
insert into sc(sno,cno,grade)
values('20025121','2',85);
insert into sc(sno,cno,grade)
values('20025121','2',90);
insert into sc(sno,cno,grade)
values('20025121','3',88);
insert into sc(sno,cno,grade)
values('20015122','3',80);
select * from sc;
主要是在sc表中的插入问题! 展开
create database we
create table student
(
sno char(9) primary key,
sname char(20) unique,
ssex char(2),
sage smallint,
sdept char(20)
);
insert into student(sno,sname,ssex,sage,sdept)
values('20025121','李勇','男','20','CS');
insert into student(sno,sname,ssex,sage,sdept)
values('20025122','刘晨','女','19','CS');
insert into student(sno,sname,ssex,sage,sdept)
values('20025123','王敏','女','18','MA');
insert into student(sno,sname,ssex,sage,sdept)
values('20025125','张立','男','19','IS');
create table course
(
cno char(4) primary key,
came char(40),
cpno char(4),
ccredit smallint,
foreign key (cpno) references course(cno)
);
insert into course(cno,came,cpno,ccredit)
values('2','数学',null,'2');
insert into course(cno,came,cpno,ccredit)
values('6','数据处理',null,'2');
insert into course(cno,came,cpno,ccredit)
values('4','操作系统','6','3');
insert into course(cno,came,cpno,ccredit)
values('7','pascal语言','6','4');
insert into course(cno,came,cpno,ccredit)
values('5','数据结构','7','4');
insert into course(cno,came,cpno,ccredit)
values('1','库数据','5','4');
insert into course(cno,came,cpno,ccredit)
values('3','信息系统','1','4');
select * from course
create table sc
(
sno char(9),
cno char(4),
Grade smallint,
primary key (sno,cno),
foreign key (sno) references student(sno),
foreign key (cno) references course(cno)
);
insert into sc(sno,cno,grade)
values('20025121','1',92);
insert into sc(sno,cno,grade)
values('20025121','2',85);
insert into sc(sno,cno,grade)
values('20025121','2',90);
insert into sc(sno,cno,grade)
values('20025121','3',88);
insert into sc(sno,cno,grade)
values('20015122','3',80);
select * from sc;
主要是在sc表中的插入问题! 展开
展开全部
你把sc表的primary key 定义为组合(sno,cno)
主键是唯一的,但是你Insert以下两条记录的时候肯定出问题
insert into sc(sno,cno,grade)
values('20025121','2',85);
insert into sc(sno,cno,grade)
values('20025121','2',90);
前面两个都是:'20025121','2',重复了,主键是唯一的,不能有重复值
主键是唯一的,但是你Insert以下两条记录的时候肯定出问题
insert into sc(sno,cno,grade)
values('20025121','2',85);
insert into sc(sno,cno,grade)
values('20025121','2',90);
前面两个都是:'20025121','2',重复了,主键是唯一的,不能有重复值
2011-04-12
展开全部
你这个sc表是怎样建立的啊!这个sno ,cno既是主键又是外键,你可以在建一个ID列作为主键的。
主键是唯一的不能够出现重复值的,就这两个字段的值都不能够相同。
主键是唯一的不能够出现重复值的,就这两个字段的值都不能够相同。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询