tags:

views:

38

answers:

3

I'm trying to insert records on multiple mysql tables with similar entities(a normalized table) I tried to do this but I get an error. I've also seen joins but it seems to work only when retrieving data.

      insert into t1(pnum, hospnum) values('117', '656')
      insert into t2(TELNUM, HOSPNUM) values('9981235', '676')
A: 

I suspect your fields are numbers not strings, try this:

insert into t1(pnum, hospnum) values(117, 656)
insert into t2(TELNUM, HOSPNUM) values(9981235, 676)

No need to use quotes for numbers otherwise you will get an error.

Sarfraz
MySQL will implicitly convert strings to int/etc based on the data type of the column involved.
OMG Ponies
@OMG: This is something i have not seen if i put quotes for numbers in the value, i am bound to see the erros. Try it out.
Sarfraz
@Sarfraz: `insert into t1(pnum, hospnum) values('117', '656')` works for me on MySQL 4.1, both columns as INTEGER. Confirmed with DECIMAL too. Shall I continue?
OMG Ponies
@OMG: No thanks for that, i am not denying it, it could be so, i have not tried that versio of mysql. thanks anyways
Sarfraz
+1  A: 

If you are executing these statements in a batch, you may need a semicolon to separate/terminate them:

insert into t1(pnum, hospnum) values(117, 656);
insert into t2(TELNUM, HOSPNUM) values(9981235, 676);
JYelton
A: 

MySQL does not support a statement that inserts into two different tables.

The only option is to use a trigger on t1 that inserts to t2, but of course you don't have access to the telnum value in a trigger; you only have the columns of t1. So this won't work in your situation.

You must execute the two inserts as separate SQL statements.

Bill Karwin