If I'm going to write a whole SQL script to create a database with tables (that has foreign ke开发者_如何学Goys) should I write the dependent tables first?
You have some options:
- You can create all the tables first, and then use
ALTER TABLE
to add the Foreign Keys. - You can create the one to many relationships as the tables are created. In that case, the order of table creation will matter.
When you create such DBs you (more often than not) seed the tables with data as well.
Depending on how much data you insert, you may want to make a decision to either INSERT data first, or to enforce RI first. If you have small tables, the RI checks don't consume too many resources. If you have large tables, then you may want to first insert the data and then implement the RI - that way the check is not done one row at a time, but at one time for all rows. Since you're seeding the tables, you know your data - presumably you'll do clean inserts so as to not fail the downstream RI check.
精彩评论