简体   繁体   中英

Using “Create Table” for an SQL Server 2008 R2 database

如果要编写整个SQL脚本以创建带有表(具有外键)的数据库,是否应该首先编写从属表?

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.

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