Using LAST_INSERT_ID() in insert statement : LAST_INSERT_ID « Function « SQL / MySQL






Using LAST_INSERT_ID() in insert statement

       
mysql>
mysql> CREATE TABLE seminar(
    ->     att_id     INT UNSIGNED NOT NULL,
    ->     sem_title  ENUM('Database Design','Query Optimization','SQL Standards','Using Replication'),
    ->     INDEX (att_id)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> INSERT INTO seminar (att_id,sem_title) VALUES(LAST_INSERT_ID(),'Database Design');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO seminar (att_id,sem_title) VALUES(LAST_INSERT_ID(),'SQL Standards');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO seminar (att_id,sem_title) VALUES(LAST_INSERT_ID(),'Using Replication');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> drop table seminar;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
  








Related examples in the same category

1.Save the result from LAST_INSERT_ID( ) after creating a new record in a table that has an AUTO_INCREMENT column to a variable
2.Each time a conference registration form is received, enter the attendee information into the attendee table.
3.Update with LAST_INSERT_ID
4.To see what the new seminar records look like, use the LAST_INSERT_ID() value to retrieve them:
5.Use the new value to retrieve the entire record, without even knowing what the id is