Auto generate id in child table when value insert in parent table

Ren

I have two table which is the parent and the child table. Now I insert a value to the parent table, with auto increment id. The code is something like this:

string query2 = "INSERT INTO printer_info(printer_ip) VALUES(@ipAddress)";
MySqlCommand cmd1 = new MySqlCommand(query2, connection);

cmd1.Parameters.AddWithValue("@ipAddress", pAddress);
cmd1.ExecuteNonQuery();

Now my problem is that when the value insert in the parent table. How if I want the id in the child table to be generated automatically?Please advise.

   ID | printer_ip               printer_id | page_count
   ---------------    ------>    -----------------------
    1 | 10.0.0.0                            |

   PRIMARY KEY ID                FOREIGN KEY printer_id
Ravinder Reddy

If you want to insert a row in child too, just after parent row is created, then you can make use of last_insert_id() in the next insert statement.

Example:

string query2 = "INSERT INTO printer_info(printer_ip) VALUES(@ipAddress)";
MySqlCommand cmd1 = new MySqlCommand(query2, connection);

cmd1.Parameters.AddWithValue("@ipAddress", pAddress);
cmd1.ExecuteNonQuery();

string query_for_child_row = "INSERT INTO printer_queue(printer_id, page_count) 
                              VALUES( LAST_INSERT_ID(), @page_count )";
MySqlCommand cmd2 = new MySqlCommand( query_for_child_row, connection );

cmd2.Parameters.AddWithValue( "@page_count", page_count );
cmd2.ExecuteNonQuery();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Auto generate id in child table when value insert in parent table

From Dev

how to insert the value of current primary key of parent table into child table

From Dev

Parent id not insert to child table whit OneToMany relationship in java hibernate

From Dev

Insert into parent and child table at same time in MySQL

From Dev

How do I Automatically insert a new record into a child table when a new record is added to a parent table in Lightswitch?

From Dev

no entry into parent table but "0" or "blank" insert into FK in child table

From Dev

SQL Logic: When joining Child table B to Parent Table A on A.FID = B.ID

From Dev

Subtracting value from parent table with SUM(value from child table)

From Dev

How to get child table rows using parent table ID?

From Dev

Change value in child table if parent table record meets condition

From Dev

Append parent table column value to child table column in sql

From Dev

Update parent table cell value from child table

From Dev

Select all Child Table ID for a Matching Parent of a Child

From Dev

XSLT generate table out of child nodes in same parent node

From Dev

Generate child row under a parent row in html table

From Dev

How to update parent table timestamp when child table is modified?

From Dev

Generate rows and insert into a table

From Dev

Insert id (auto increment) into other table in same moment

From Dev

Insert id (auto increment) into other table in same moment

From Dev

Insert record to a table using linq query that contains auto increment ID

From Dev

How to insert a value into an auto_increment of a different table in the same string

From Dev

Insert auto_increment value of the test table to the score table table using a single "update" JButton

From Dev

Generate HTML table row containing a value and its sibling and parent values

From Dev

Generate HTML table row containing a value and its sibling and parent values

From Dev

Single MySQL query for parent child table value sorting

From Dev

Insert into table when one value not exist in another table?

From Dev

Insert into table when one value not exist in another table?

From Dev

How can I insert data into a child table from a parent table columns?

From Dev

How to insert a single row in the parent table and then multiple rows in the child table in single SQL in PostgreSQL?

Related Related

  1. 1

    Auto generate id in child table when value insert in parent table

  2. 2

    how to insert the value of current primary key of parent table into child table

  3. 3

    Parent id not insert to child table whit OneToMany relationship in java hibernate

  4. 4

    Insert into parent and child table at same time in MySQL

  5. 5

    How do I Automatically insert a new record into a child table when a new record is added to a parent table in Lightswitch?

  6. 6

    no entry into parent table but "0" or "blank" insert into FK in child table

  7. 7

    SQL Logic: When joining Child table B to Parent Table A on A.FID = B.ID

  8. 8

    Subtracting value from parent table with SUM(value from child table)

  9. 9

    How to get child table rows using parent table ID?

  10. 10

    Change value in child table if parent table record meets condition

  11. 11

    Append parent table column value to child table column in sql

  12. 12

    Update parent table cell value from child table

  13. 13

    Select all Child Table ID for a Matching Parent of a Child

  14. 14

    XSLT generate table out of child nodes in same parent node

  15. 15

    Generate child row under a parent row in html table

  16. 16

    How to update parent table timestamp when child table is modified?

  17. 17

    Generate rows and insert into a table

  18. 18

    Insert id (auto increment) into other table in same moment

  19. 19

    Insert id (auto increment) into other table in same moment

  20. 20

    Insert record to a table using linq query that contains auto increment ID

  21. 21

    How to insert a value into an auto_increment of a different table in the same string

  22. 22

    Insert auto_increment value of the test table to the score table table using a single "update" JButton

  23. 23

    Generate HTML table row containing a value and its sibling and parent values

  24. 24

    Generate HTML table row containing a value and its sibling and parent values

  25. 25

    Single MySQL query for parent child table value sorting

  26. 26

    Insert into table when one value not exist in another table?

  27. 27

    Insert into table when one value not exist in another table?

  28. 28

    How can I insert data into a child table from a parent table columns?

  29. 29

    How to insert a single row in the parent table and then multiple rows in the child table in single SQL in PostgreSQL?

HotTag

Archive