Skip to content
Advertisement

INSERT INTO two different tables, but have the same ID?

I have a database of Users and another table for Teachers. Teachers have all the properties as a user but also an e-mail address. When inserting into the DB how can I insert the info, ensuring that the ID is the same for both?

the ID currently is on automatic incrament.

this is what I have at the moment:

$sqlQuery="INSERT INTO user(firstName,lastName,DOB,title,password,classRoomID) 
    VALUES('$myFirstName','$myLastName','$myDOB','$myTitle','$newPassword','$myClassRoom')";
    $result=mysql_query($sqlQuery);

$sqlQuery = "INSERT INTO teacher(email) VALUES ('$myEmail')";
$result=mysql_query($sqlQuery);

thank you!

Advertisement

Answer

why to use separate table for teachers. instead, you can have email field with in user table and additional field with flag (T ( for teacher) and U (for user). Default can be a U. This have following Pros.

  1. Will Not increase table size as email would be varchar
  2. Remove extra overhead of maintaining two tables.
  3. Same Id can be used

If you want to have that as separate table then answer you selected is good one but make sure last insert id is called in same connection call.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement