Tuesday, 15 January 2013

mysql - database input copy -



mysql - database input copy -

i´m gonna start of typing want.

so, have 1 table, each row gets new id automatically, , each new row created there, want id set table specific column.

database-structure: event

(this table want re-create id from)

eventid | lots of more columns

-

database-structure: jointable (this table want "eventid" copied to)

eventid | more columns.

so, want eventid set in jointable automatically when event set in table "event".

use function returns lastly inserted auto-generated id.

in mysql it's last_insert_id() in sql server it's scope_identity, ident_current, @@identity

for illustration in mysql can this

class="lang-sql prettyprint-override">insert event (column1,column2...) values (...); set @last_event_id = last_insert_id(); insert jointable (eventid, column1, column2...) values (@last_event_id, ...);

update: client (php+mysqli) utilize mysqli_insert_id()

your code in case like

class="lang-php prettyprint-override">$db = mysqli_connect(...); ... $query = "insert event (column1, column2...) values (...)"; mysql_query($query); $last_event_id = mysqli_insert_id($db); $query = "insert jointable (eventid, column1, column2...) values ($last_event_id, ...)"; mysql_query($query);

mysql sql database

No comments:

Post a Comment