IMDB database Image to database structure FletNix
INSERT INTO FletNix_2.dbo.Movie_Cast
SELECT cast(Pid AS int) AS person_id,
cast(Mid AS int) AS movie_id,
LEFT(Role, 255) AS role
FROM MYIMDB.dbo.Imported_Cast
INSERT INTO FletNix_2.dbo.Movie_Directors
SELECT cast(Did AS int) AS person_id,
cast(Mid AS int) AS movie_id
FROM MYIMDB.dbo.Imported_Movie_Directors
错误信息:
消息2627,级别14,状态1,第71行违反主键 约束“ pk_moviecast”。无法在对象中插入重复键 'dbo.Movie_Cast'。重复的键值为((未知))。
消息547,级别16,状态0,第77行INSERT语句冲突 具有FOREIGN KEY约束“ fk_directors_Person”。冲突 发生在数据库“ FletNix_2”的表“ dbo.Person”的列中 “ person_id”。
我知道我需要使用distinct,但是当我这样做时
INSERT INTO FletNix_2.dbo.Movie_Cast
SELECT cast(Pid AS int) AS person_id,
cast(Mid AS int) AS movie_id,
distinct LEFT(Role, 255) AS role
FROM MYIMDB.dbo.Imported_Cast
它给出一个错误: 语法不正确
当我在SELECT之后放不同时,我仍然收到错误:
违反主键约束“ pk_moviecast”。无法插入 对象“ dbo.Movie_Cast”中的重复键。重复的键值为 ((未知))
AND
INSERT语句与FOREIGN KEY约束冲突 “ fk_directors_Person”。数据库“ FletNix_2”中发生了冲突, 表“ dbo.Person”的“ person_id”列。
答案 0 :(得分:1)
区别应该在select
INSERT INTO FletNix_2.dbo.Movie_Cast
SELECT distinct cast(Pid AS int) AS person_id,
cast(Mid AS int) AS movie_id,
LEFT(Role, 255) AS role
FROM MYIMDB.dbo.Imported_Cast
由于您的角色列是主要角色,因此不必重复,因此我使用row_number()生成唯一编号
select
cast(Pid AS int) AS person_id,
cast(Mid AS int) AS movie_id,
row_number()over( order by (select null)) as role
FROM MYIMDB.dbo.Imported_Cast