第一次建立数据库,有很多问题,求指教
use movie
create table actors
(
AID numeric(4,0) primary key,
name varchar(8),
);
create table movies
(
MID numeric(4,0) primary key,
title varchar(15),
);
create table actor_role
(
MID numeric(4,0),
AID numeric(4,0),
rolename varchar(15),
foreign key (MID) references movies,
foreign key (AID) references actors,
primary key (MID,AID),
); //这是表格的建立
use movie
insert into movies values (0001,'breaveheart');
insert into movies values (0002,'whitehouse');
insert into movies values (0003,'batman');
insert into movies values (0004,'spiderman');
insert into movies values (0005,'superman');
insert into actors values (1111,'brucelee');
insert into actors values (2222,'frank');
insert into actors values (3333,'jimmy');
insert into actors values (4444,'sky');
insert into actors values (5555,'enda');
insert into actor_role values (0001,3333,'shaw');
insert into actor_role values (0001,5555,'wayen');
insert into actor_role values (0002,2222,'cale');
insert into actor_role values (0002,1111,'lee');
insert into actor_role values (0003,5555,'park');
insert into actor_role values (0004,1111,'paul');
insert into actor_role values (0005,2222,'bayen');
//这是数据的录入
但是出现了很多错误:
Violation of PRIMARY KEY constraint 'PK__movies__C797348A03317E3D'. Cannot insert duplicate key in object 'dbo.movies'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 4
Violation of PRIMARY KEY constraint 'PK__movies__C797348A03317E3D'. Cannot insert duplicate key in object 'dbo.movies'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 5
Violation of PRIMARY KEY constraint 'PK__movies__C797348A03317E3D'. Cannot insert duplicate key in object 'dbo.movies'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 6
Violation of PRIMARY KEY constraint 'PK__movies__C797348A03317E3D'. Cannot insert duplicate key in object 'dbo.movies'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 7
Violation of PRIMARY KEY constraint 'PK__movies__C797348A03317E3D'. Cannot insert duplicate key in object 'dbo.movies'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 9
Violation of PRIMARY KEY constraint 'PK__actors__C69007C87F60ED59'. Cannot insert duplicate key in object 'dbo.actors'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 10
Violation of PRIMARY KEY constraint 'PK__actors__C69007C87F60ED59'. Cannot insert duplicate key in object 'dbo.actors'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 11
Violation of PRIMARY KEY constraint 'PK__actors__C69007C87F60ED59'. Cannot insert duplicate key in object 'dbo.actors'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 12
Violation of PRIMARY KEY constraint 'PK__actors__C69007C87F60ED59'. Cannot insert duplicate key in object 'dbo.actors'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 13
Violation of PRIMARY KEY constraint 'PK__actors__C69007C87F60ED59'. Cannot insert duplicate key in object 'dbo.actors'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 15
Violation of PRIMARY KEY constraint 'PK__actor_ro__5BFE34F607020F21'. Cannot insert duplicate key in object 'dbo.actor_role'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 16
Violation of PRIMARY KEY constraint 'PK__actor_ro__5BFE34F607020F21'. Cannot insert duplicate key in object 'dbo.actor_role'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 17
Violation of PRIMARY KEY constraint 'PK__actor_ro__5BFE34F607020F21'. Cannot insert duplicate key in object 'dbo.actor_role'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 18
Violation of PRIMARY KEY constraint 'PK__actor_ro__5BFE34F607020F21'. Cannot insert duplicate key in object 'dbo.actor_role'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 19
Violation of PRIMARY KEY constraint 'PK__actor_ro__5BFE34F607020F21'. Cannot insert duplicate key in object 'dbo.actor_role'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 20
Violation of PRIMARY KEY constraint 'PK__actor_ro__5BFE34F607020F21'. Cannot insert duplicate key in object 'dbo.actor_role'.
The statement has been terminated.
Msg 2627, Level 14, State 1, Line 21
Violation of PRIMARY KEY constraint 'PK__actor_ro__5BFE34F607020F21'. Cannot insert duplicate key in object 'dbo.actor_role'.
The statement has been terminated.
到底是哪里出了错误,求指正。
[解决办法]
create table actors
(
AID numeric(4,0) primary key,
name varchar(8),
);
create table movies
(
MID numeric(4,0) primary key,
title varchar(15),
);
create table actor_role
(
MID numeric(4,0),
AID numeric(4,0),
rolename varchar(15),
foreign key (MID) references movies,
foreign key (AID) references actors,
primary key (MID,AID),
);
insert into movies values (0001,'breaveheart');
insert into movies values (0002,'whitehouse');
insert into movies values (0003,'batman');
insert into movies values (0004,'spiderman');
insert into movies values (0005,'superman');
insert into actors values (1111,'brucelee');
insert into actors values (2222,'frank');
insert into actors values (3333,'jimmy');
insert into actors values (4444,'sky');
insert into actors values (5555,'enda');
insert into actor_role values (0001,3333,'shaw');
insert into actor_role values (0001,5555,'wayen');
insert into actor_role values (0002,2222,'cale');
insert into actor_role values (0002,1111,'lee');
insert into actor_role values (0003,5555,'park');
insert into actor_role values (0004,1111,'paul');
insert into actor_role values (0005,2222,'bayen');
这个代码我运行一次没有错误。
上面提示不能插入重复 主键,是不是你运行多次?
[解决办法]
从你的报错信息看,应该是在表dbo.movies,插入了重复数据,你用的sql server 2005以后的版本吗,
从你的插入的数据来看,你的语句并没有插入重复数据,主键字段,都是不同的值,照理应该不会报错的。
我在sql server 2008r2上运行了下面的代码,没有报错:
--create database movie
--go
use movie
go
create table actors
(
AID numeric(4,0) primary key,
name varchar(8),
);
create table movies
(
MID numeric(4,0) primary key,
title varchar(15),
);
create table actor_role
(
MID numeric(4,0),
AID numeric(4,0),
rolename varchar(15),
foreign key (MID) references movies,
foreign key (AID) references actors,
primary key (MID,AID),
); --注意不能用//,这个不是注释,这是表格的建立
use movie
go
insert into movies values (0001,'breaveheart');
insert into movies values (0002,'whitehouse');
insert into movies values (0003,'batman');
insert into movies values (0004,'spiderman');
insert into movies values (0005,'superman');
insert into actors values (1111,'brucelee');
insert into actors values (2222,'frank');
insert into actors values (3333,'jimmy');
insert into actors values (4444,'sky');
insert into actors values (5555,'enda');
insert into actor_role values (0001,3333,'shaw');
insert into actor_role values (0001,5555,'wayen');
insert into actor_role values (0002,2222,'cale');
insert into actor_role values (0002,1111,'lee');
insert into actor_role values (0003,5555,'park');
insert into actor_role values (0004,1111,'paul');
insert into actor_role values (0005,2222,'bayen');
[解决办法]
你能把这个数据库删除吗?
[解决办法]
先删除movie数据库,然后重新建表,插入数据,一步一步运行,不要一下子运行,这样也可以看看,到底是哪儿错了,先试试:
use master
go
drop database movie
go
create database movie
go
use movie
go
create table actors
(
AID numeric(4,0) primary key,
name varchar(8),
);
create table movies
(
MID numeric(4,0) primary key,
title varchar(15),
);
create table actor_role
(
MID numeric(4,0),
AID numeric(4,0),
rolename varchar(15),
foreign key (MID) references movies,
foreign key (AID) references actors,
primary key (MID,AID),
); --注意不能用//,这个不是注释,这是表格的建立
use movie
go
insert into movies values (0001,'breaveheart');
insert into movies values (0002,'whitehouse');
insert into movies values (0003,'batman');
insert into movies values (0004,'spiderman');
insert into movies values (0005,'superman');
insert into actors values (1111,'brucelee');
insert into actors values (2222,'frank');
insert into actors values (3333,'jimmy');
insert into actors values (4444,'sky');
insert into actors values (5555,'enda');
insert into actor_role values (0001,3333,'shaw');
insert into actor_role values (0001,5555,'wayen');
insert into actor_role values (0002,2222,'cale');
insert into actor_role values (0002,1111,'lee');
insert into actor_role values (0003,5555,'park');
insert into actor_role values (0004,1111,'paul');
insert into actor_role values (0005,2222,'bayen');
[解决办法]
唐诗大大正解,创建表的时候,定义了主键,执行多次重复插入主键值肯定报错
[解决办法]
如果你的代码需要运行多次,需要加上判断,比如:
if db_id('库名')is null
create database xxx
go
这样你就可以运行多次
[解决办法]
先查询一下你的表中是否有数据,如果有数据是否和你插入的数据有重复。
然后建议一步一步的执行,便于判断错误。