1

I have two tables ACCOUNTS and ACCOUNT_LIMITS and they have crossed foreign keys.

ACCOUNTS
--------
id (PK)
account_limits_id (FK)
username

ACCOUNT_LIMITS
--------------
id (PK)
account_id (FK)
limit

I shortened the real example. My question is, How can I make an insert statement for both tables?

agusgambina
  • 6,229
  • 14
  • 54
  • 94
  • Why do you want to have FK on both sides? – Peter Henell Mar 28 '17 at 19:31
  • @PeterHenell thank you for your answer. I did not define the table, but I think it is for guarantee the one to one relationship. – agusgambina Mar 28 '17 at 19:33
  • If you can change it it is better to have a unique constraint on the account_id in the account_limits table instead of having the accounts_limits_id in the accounts table. – Peter Henell Mar 28 '17 at 19:36
  • 1
    If you cannot change it then you need to disable the FK constraint while inserting and then re-enable it after insert. It seems MySql cannot deal with deferred integrity checks (based on this answer http://stackoverflow.com/questions/5014700/in-mysql-can-i-defer-referential-integrity-checks-until-commit) – Peter Henell Mar 28 '17 at 19:38
  • @PeterHenell my bad, on the table `accounts` the `account_limits_id` can be null. However it is also defined as a foreign key. Thank you for your answers although, they were helpful. – agusgambina Mar 28 '17 at 19:55

0 Answers0