简体   繁体   中英

Auto Incrementing In Another Table?

I am trying to see how to auto-increment a PK from another table.

For example:

I have two tables where UserID is present. A User table where UserID is the primary key & a Ticket table where UserID is a reference. When a row is inserted into the User table, the UserID is incremented (of course). When a row is inserted into the Ticket table, I want the UserID to increment in the Ticket & User table as well. For the Ticket table, should I just have it in my PHP where I add 1 to the last inserted ID in the User Table or is there something in the database where I can set to do this for me?

When a row is inserted into the Ticket table, I want the UserID to increment in the Ticket & User table as well.

It seems your table relation is not defined correct.

If the tables are:
1. User with userid as auto incremented primary key, and
2. Ticket with userid as referenced key from user

then, before inserting into Ticket you should have userid value in hand. Because it has a foreign key relation, you can't generate it new but pick from user table and use in ticket table.

And incrementing the userid value in ticket table has no meaning so long as it has a foreign key relation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM