How to add to INSERT INTO PRIMARY KEY AND FOREIGN KEY

Bruno Is

So i have two tables: locations and employees i want locations_id to be the same in employees.locations_id, I am trying to make it all in one statement the erros is this You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO employees(employees_id, locations_id) VALUES('e1598','')' at line 1

String sql = " INSERT INTO locations( locations_id , username, password, id, type_of_id, first_name, last_name, phone, email, date_of_birth, address, sex ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)"
 **Error here --->**           + "INSERT INTO employees(employees_id,locations_id) VALUES (?,SELECT locations_id FROM locations INNER JOIN employees ON locations.locations_id =employees.locations_id)";
    try {

       MicroModelGUI micro = new MicroModelGUI();
        PreparedStatement consulta =  micro.connection.prepareStatement(sql);
         consulta.setString(1, tflid.getText());
         consulta.setString(2, tfuser.getText());
         consulta.setString(3, tfpass.getText());
         consulta.setString(4, tfid.getText());
         consulta.setString(5, tftoid.getText());
         consulta.setString(6, tffirst.getText());
         consulta.setString(7,tflast.getText());
         consulta.setString(8,tfphone.getText());
         consulta.setString(9,tfemail.getText());
         consulta.setString(10,tffdn.getText());
         consulta.setString(11,tfaddress.getText());
         consulta.setString(12,tfsex.getText());
         consulta.setString(13,tfeid.getText());
         int resultado = consulta.executeUpdate();
    
Tim Biegeleisen

You should be using an INSERT INTO ... SELECT here:

INSERT INTO employees (employees_id, locations_id)
SELECT ?, l.locations_id
FROM locations l
INNER JOIN employees e ON l.locations_id = e.locations_id;

To the ? placeholder you would bind a value from your Java code. Note that while your version of SQL might support putting a scalar subquery into a VALUES clause, it is likely that your exact version would cause an error, as it would return more than one row.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Foreign key as primary key?

How to delete foreign key as well as primary key

How to match values of primary key and foreign key?

Cannot add foreign key constraint with foreign key being primary

How to add foreign key?

How to insert primary key from one table as a foreign key into secondary table

how to insert primary key and foreign key values at same time using PHP

How to insert a record and return the primary key to update the foreign key in another table?

How to insert values into two SQL Server tables updating primary key and foreign key simultaneously, using a procedure?

How to insert a row into a table with a composite foreign key referencing a composite primary key

How to insert my primary key under my signup table into my feedback table as a foreign key

Insert into two tables simultaneously primary key and foreign key using Laravel

Insert generated primary key as foreign key in different table from controller

MySQL INSERT INTO inside UPDATE and SET new primary key as foreign key

Violation Primary Key, tries to insert foreign key table

How to insert a foreign key in a row?

How to insert foreign key in jsp

How can I add primary key and foreign key constraints after export data from Azure SQL

how to add a foreign key that references 2-columns primary key with "limited length"

Rails add foreign key on non-primary key

Primary Key and Foreign Key on Laravel

foreign key is not equal to primary key

Composite foreign key as primary key

Foreign Key to Primary key relation

Replacing a foreign key by a primary key

Find foreign key by primary key?

How to use primary and foreign key constraints?

How to refer primary key as Foreign to various table

How to fix "java.sql.SQLException: Cannot add foreign key constraint." when creating table with double primary/foreign key relationship