Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 插入。。。冲突时不执行任何操作-读取csv并生成外键表_Sql_Postgresql_Insert_Duplicates_Conflict - Fatal编程技术网

Sql 插入。。。冲突时不执行任何操作-读取csv并生成外键表

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 | ... 我已经设计并正在尝试填充以下表格。

我试图在csv文件中读取艺术家、专辑、歌曲和标签列

我希望填充艺术家\u专辑\u歌曲表,如下所示:

|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());
    }
}
  • 然后,我用以下语句(通过JDBC)填充新表[2]:

  • 创建带有约束的
    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):

  • 编辑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());
        }
    }
    
  • 然后,我用以下语句(通过JDBC)填充新表[2]:

  • 创建带有约束的
    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;