简体   繁体   English

如何根据主键的值更改外键?

[英]how can changes foreign key according to the value of the primary key?

I have two tables now, table talkview: 我现在有两个表,表talkview:

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(10)     | NO   | PRI | NULL    | auto_increment |
| titlename | varchar(64) | NO   |     | NULL    |                |
| postname  | varchar(64) | NO   |     | NULL    |                |
| counts    | varchar(11) | YES  |     | 0       |                |
+-----------+-------------+------+-----+---------+----------------+

and table maintalk: 和表maintalk:

+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| title    | varchar(64) | NO   |     | NULL    |                |
| text     | varchar(64) | NO   |     | NULL    |                |
| username | varchar(64) | NO   |     | NULL    |                |
| talk_id  | int(10)     | YES  | MUL | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

and now I want to insert the data into these tables together and 'talk_id' will Automatically change according 'id' from table talkview. 现在我想将数据一起插入这些表中,并且“ talk_id”将根据表talkview中的“ id”自动更改。

in Java, i try this code,but 'talk_id' has no change: 在Java中,我尝试使用此代码,但'talk_id'不变:

public void posttalk(String title, String con, String name){
        connection = DBConection.getConnection();
        Statement stmt = null;
        String SQL_1 = "insert into talkview(titlename,postname) values(? , ?)";
        String SQL_2 = "insert into maintalk(title,text,username) values(? , ? , ?)";
        try {
            connection.setAutoCommit(false);
            PreparedStatement preparedStatement = connection.prepareStatement(SQL_1);
            preparedStatement.setString(1, title);
            preparedStatement.setString(2, name);
            Integer a = preparedStatement.executeUpdate();
            preparedStatement = connection.prepareStatement(SQL_2);
            preparedStatement.setString(1, title);
            preparedStatement.setString(2, name);
            preparedStatement.setString(3, con);
            Integer b = preparedStatement.executeUpdate();
            connection.commit();
            connection.setAutoCommit(true);
            } catch (SQLException sqle) {
            try {
                connection.rollback();
                stmt.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            sqle.printStackTrace();
            } finally {
            DBConection.closeConnection(connection);
        }
    }

After you insert some datas in your first table called talkview . 在第一个称为talkview表中插入一些数据talkview you can retrieve the last record you insert by using the largest primary key because your PK is auto increment and then put it on talk_id column of maintalk table. 您可以使用最大的主键来检索插入的最后一条记录,因为您的PK是自动递增的,然后将其放在maintalk表的talk_id列上。 You can do below code to retrieve it. 您可以在下面的代码中进行检索。

    String SQL_1 = "insert into talkview(titlename,postname) values(? , ?)";
    String SQL_2 = "insert into maintalk(title,text,username,talk_id) values(? , ? , ?, ?)";
    String SQL_3 = "SELECT id FROM talkview order by id DESC LIMIT 1;";
    try {
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement(SQL_1);
        preparedStatement.setString(1, title);
        preparedStatement.setString(2, name);
        Integer a = preparedStatement.executeUpdate();

        // This is the query to retrieve the last ID you insert in talkveiw table
        PreparedStatement preparedStatement = connection.prepareStatement(SQL_3);
        ResultSet result = preparedStatement.executeQuery();
        Integer id = 0; // This will be the id you will add on your talk_id
        if(result.next()) {
            id = result.getInt(1);
        }

        preparedStatement = connection.prepareStatement(SQL_2);
        preparedStatement.setString(1, title);
        preparedStatement.setString(2, name);
        preparedStatement.setString(3, con);
        preparedStatement.setInt(4, id); // you will insert it here the 'id'
        Integer b = preparedStatement.executeUpdate();
        connection.commit();
        connection.setAutoCommit(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM