1

I am designing a database, and need a statement inserting values into two tables at a time. The issue for me doing so, is the fact that one of the tables will need a value, which is autoincremented in the other. How can I do this? My host does not support the use of foreign keys unfortunately. (I am using mySQL)
I've put the table names and values below.

Task

+ id (auto incremented value)
+ dataid (should have been foreign key to data.id- which will be auto incremented when +tatement is xecuted)
+ priority (task priority 1-100)
+ statusid (should have been foreign key to status.id - this value is given)
+ toc (TimeOfCreation - timestamp, will be set automatically)

Data

+ id (auto incremented value)
+ text (the text of the task/privatemsg/statusmsg etc.)

Can one or someone of you help me make an insert statment making it possible for me to insert this data into these tables, where data.id is auto incremented, and stored in task.dataid?

text=fix login  
priority=99  
statusid=4

Please ask if you have any questions.
Thanks in advance

Pyracell

Pyracell
  • 157
  • 2
  • 3
  • 10

2 Answers2

1

See this:

http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

"If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function."

Oscar
  • 13,594
  • 8
  • 47
  • 75
0

Just immediately after your INSERT query on first table , write a query that selects MAX(data.id) from first table and insert this value into task.dataid.

Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133