【发布时间】:2017-07-16 00:29:42
【问题描述】:
我正在尝试读取包含艺术家、专辑、歌曲和标签列的 csv 文件。
我希望像这样填充artist_album_song 表:
|artist_id|album_id|song_id|
|---------|--------|-------|
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 1 |
...
| 12 | 1 | 1 |
...
我已经设计并正在尝试填充以下表格。当我在 csv 中读取时,问题是填充 artist_album_song 表中的外键。
在下面使用的 INSERT 语句中插入此表的最佳方法是什么(返回语法错误)?谢谢。
create table artists (
artist_id SERIAL PRIMARY KEY,
artist VARCHAR(100) NOT NULL UNIQUE
);
create table albums (
album_id SERIAL PRIMARY KEY,
album VARCHAR(100) NOT NULL UNIQUE
);
create table songs (
song_id SERIAL PRIMARY KEY,
song VARCHAR(250) NOT NULL UNIQUE
);
create table tags (
tag_id SERIAL PRIMARY KEY,
tag VARCHAR(100) NOT NULL UNIQUE
);
create table artists_albums_songs (
artist_id INTEGER NOT NULL,
album_id INTEGER NOT NULL,
song_id INTEGER NOT NULL,
FOREIGN KEY (artist_id) REFERENCES artists(artist_id),
FOREIGN KEY (album_id) REFERENCES albums(album_id),
FOREIGN KEY (song_id) REFERENCES songs(song_id),
PRIMARY KEY (artist_id, album_id, song_id)
);
create table songs_tags (
song_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
FOREIGN KEY (song_id) REFERENCES songs(song_id),
FOREIGN KEY (tag_id) REFERENCES tags(tag_id),
PRIMARY KEY (song_id, tag_id)
);
在尝试了以下链接中的各种语句变体后,我仍然无法使其正常工作。
我已经尝试了以下语句,但我不断收到错误消息。第一个返回错误:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "ON" Position: 161;
161是指下面SQL语句中的第161个字符吗?
INSERT INTO artists_albums_songs
SELECT artist_id, album_id, song_id
FROM artists a
JOIN albums b
ON a.artist = ?
AND b.album = ?
JOIN songs c
ON c.song = ?
ON DUPLICATE (artist_id, album_id, song_id) DO NOTHING;
INSERT INTO artists_albums_songs
SELECT artist_id, album_id, song_id
FROM artists a
JOIN albums b
ON a.artist = ?
AND b.album = ?
JOIN songs c
ON c.song = ?
WHERE NOT EXISTS (
SELECT *
FROM artists_albums_songs
WHERE * = ?, ?, ?)
INSERT INTO artists_albums_songs
SELECT artist_id, album_id, song_id
FROM artists a
JOIN albums b
ON a.artist = ?
AND b.album = ?
JOIN songs c
ON c.song = ?
ON CONFLICT (song_id) IGNORE;
编辑:如果我删除上面 3 个 INSERT 语句的最后一行,它可以工作,但是当它遇到重复时,它会说:
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "artists_albums_songs_pkey"
Detail: Key (artist_id, album_id, song_id)=(1, 1, 1) already exists.
Insert, on duplicate update in PostgreSQL?
Use INSERT ... ON CONFLICT DO NOTHING RETURNING failed rows
How to UPSERT (MERGE, INSERT ... ON DUPLICATE UPDATE) in PostgreSQL?
【问题讨论】:
标签: sql postgresql insert duplicates conflict