user5331623
user5331623

Reputation:

Mysql - Adding Records

If two individual records are added to a database such as two patients being added to the table 'Paitents' and when they are added the Primary_Key such as Paitient_ID is created automatically and given to each new account. .(Auto Incremented) That bit is quite straight forward and understand I can just use an 'INSERT INTO SONGS' statement. But what if the two patients are related and I have another table called "Relations" Where by I need it to pull in the two Paitent_ID's and create a relation from the same insert query. Can this be done?

Upvotes: 0

Views: 53

Answers (1)

ROAL
ROAL

Reputation: 1779

ID generated by MySQL automatically in auto_increment column can be obtained using the LAST_INSERT_ID() function. Languages/libraries often offer a function for this (e.g. in PHP PDO you call PDO::lastInsertId() instead of making another query).

How to solve this exactly depends on how you are inserting the values into a database, but the basics can be:

  1. Insert patient one
  2. Get the ID
  3. Insert patient two
  4. Get the ID
  5. Create the relation

Upvotes: 1

Related Questions