Reputation: 14978
I am working on a PHP application which Db design was created by another guy who added FK constraints(On Cascade delete etc) between tables. So far what I am used to do is to put a FK in another table. For instance we have two tables:
Users
Countries
At application Level I will create two Separate INSERTs calls. If FK is present, then what change do I need to make at my application level?
Upvotes: 0
Views: 73
Reputation: 286
Foreign keys enforce referential integrity.
These constraints guarantee that a row in a table order_details with a field order_id referencing an orders table will never have an order_id value that doesn't exist in the orders table.
Foreign keys aren't required to have a working relational database (in fact MySQL's default storage engine doesn't support FKs), but they are definitely essential to avoid broken relationships and orphan rows (ie. referential integrity). The ability to enforce referential integrity at the database level is required for the C in ACID to stand.
Upvotes: 1
Reputation: 943142
You need to make sure that the row you reference with the FK exists before you create the row in the other table.
… but you are probably doing this already as that is the logical order to create the rows.
Upvotes: 1