我无法插入 sql
I cant insert into sql
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
错误消息:
Msg 2627, Level 14, State 1, Line 71 Violation of PRIMARY KEY
constraint 'pk_moviecast'. Cannot insert duplicate key in object
'dbo.Movie_Cast'. The duplicate key value is ((Unknown)).
Msg 547, Level 16, State 0, Line 77 The INSERT statement conflicted
with the FOREIGN KEY constraint "fk_directors_Person". The conflict
occurred in database "FletNix_2", table "dbo.Person", column
'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
报错:
distinct
附近的语法不正确
当我在 SELECT 之后放置 distinct 时,我仍然得到一个错误:
Violation of PRIMARY KEY constraint 'pk_moviecast'. Cannot insert
duplicate key in object 'dbo.Movie_Cast'. The duplicate key value is
((Unknown))
和
The INSERT statement conflicted with the FOREIGN KEY constraint
"fk_directors_Person". The conflict occurred in database "FletNix_2",
table "dbo.Person", column 'person_id'.
distinct 应该在 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
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
错误消息:
Msg 2627, Level 14, State 1, Line 71 Violation of PRIMARY KEY constraint 'pk_moviecast'. Cannot insert duplicate key in object 'dbo.Movie_Cast'. The duplicate key value is ((Unknown)).
Msg 547, Level 16, State 0, Line 77 The INSERT statement conflicted with the FOREIGN KEY constraint "fk_directors_Person". The conflict occurred in database "FletNix_2", table "dbo.Person", column '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
报错: distinct
附近的语法不正确当我在 SELECT 之后放置 distinct 时,我仍然得到一个错误:
Violation of PRIMARY KEY constraint 'pk_moviecast'. Cannot insert duplicate key in object 'dbo.Movie_Cast'. The duplicate key value is ((Unknown))
和
The INSERT statement conflicted with the FOREIGN KEY constraint "fk_directors_Person". The conflict occurred in database "FletNix_2", table "dbo.Person", column 'person_id'.
distinct 应该在 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