Sql 插入。。。冲突时不执行任何操作-读取csv并生成外键表
我试图在csv文件中读取艺术家、专辑、歌曲和标签列 我希望填充艺术家\u专辑\u歌曲表,如下所示:Sql 插入。。。冲突时不执行任何操作-读取csv并生成外键表,sql,postgresql,insert,duplicates,conflict,Sql,Postgresql,Insert,Duplicates,Conflict,我试图在csv文件中读取艺术家、专辑、歌曲和标签列 我希望填充艺术家\u专辑\u歌曲表,如下所示: |artist_id|album_id|song_id| |---------|--------|-------| | 1 | 1 | 1 | | 1 | 1 | 2 | | 1 | 2 | 1 | ... | 12 | 1 | 1 | ... 我已经设计并正在尝试填充以下表格。
|artist_id|album_id|song_id|
|---------|--------|-------|
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 1 |
...
| 12 | 1 | 1 |
...
我已经设计并正在尝试填充以下表格。问题是当我在csv中读取时,填充Artister_album_song表中的外键
在下面使用的INSERT语句(返回语法错误)中,插入到这个表中的最佳方法是什么?谢谢
在尝试了下面链接中各种不同的语句之后,我仍然无法实现这一点
我试过下面的语句,但我总是出错。第一个返回错误:
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.
编辑1:
我刚刚意识到我可以用Java处理这些错误!因此,我的解决方案只需要添加一个catch
语句来处理重复的SQLException
private <T> void insertIntoArtistAlbumSong(T artist, T album, T song) throws SQLException {
try {
String artString = artist.toString();
String albString = album.toString();
String songString = song.toString();
// Create SQL insert statement
String stm =
"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 = ? ;";
PreparedStatement pstmt = connection.prepareStatement(stm);
// Set values in prepared statement
pstmt.setString(1, artString);
pstmt.setString(2, albString);
pstmt.setString(3, songString);
// Insert into table
pstmt.executeUpdate();
// ADDED THIS CATCH STATEMENT!
} catch (SQLException e){
System.out.println(e.getSQLState());
}
}
tmp
[3]表(通过psql命令行):
artists\u albums\u songs
[2]中不同的行插入tmp
[3](通过psql):
artists\u albums\u songs
[2]并将tmp
[3]重命名为artists\u albums\u songs
(通过psql):
catch
语句来处理重复的SQLException
private <T> void insertIntoArtistAlbumSong(T artist, T album, T song) throws SQLException {
try {
String artString = artist.toString();
String albString = album.toString();
String songString = song.toString();
// Create SQL insert statement
String stm =
"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 = ? ;";
PreparedStatement pstmt = connection.prepareStatement(stm);
// Set values in prepared statement
pstmt.setString(1, artString);
pstmt.setString(2, albString);
pstmt.setString(3, songString);
// Insert into table
pstmt.executeUpdate();
// ADDED THIS CATCH STATEMENT!
} catch (SQLException e){
System.out.println(e.getSQLState());
}
}
tmp
[3]表(通过psql命令行):
artists\u albums\u songs
[2]中不同的行插入tmp
[3](通过psql):
artists\u albums\u songs
[2]并将tmp
[3]重命名为artists\u albums\u songs
(通过psql):
ON DUPLICATE (artist_id, album_id, song_id) DO NOTHING;
Postgtresql在冲突上使用
关键字
此行中出现错误:
ON DUPLICATE (artist_id, album_id, song_id) DO NOTHING;
Postgtresql在冲突上使用
关键字
CREATE TABLE tmp (
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)
);
INSERT INTO tmp SELECT DISTINCT * FROM artists_albums_songs
ORDER BY artist_id, album_id, song_id ASC;
DROP TABLE artists_albums_songs;
ALTER TABLE tmp RENAME TO artists_albums_songs;
ON DUPLICATE (artist_id, album_id, song_id) DO NOTHING;