create table Table1 (..., constraint pk_table1 primary key (...));
create table Table2 (..., constraint pk_table2 primary key (...));
..
create table TableN (..., constraint pk_tableN primary key (...));
然后添加外键约束:
alter table Table1 add constraint fk_table1_tablex
foreign key (...)
references TableX (...);
alter table Table2 add constraint fk_table2_tabley
foreign key (...)
references TableY (...);
...
这样就没有顺序依赖了。
select name
from sys.tables
where type = 'u'
and object_id not in
(
select referenced_object_id
from sys.foreign_key_columns
)
这将为您提供一个未被引用的所有表格的列表。要获取一个被引用(应该首先创建)的表格列表,可以使用以下代码:
select name
from sys.tables
where type = 'u'
and object_id in
(
select referenced_object_id
from sys.foreign_key_columns
)
要获取没有任何外键约束(因此没有依赖关系)的表列表,请运行以下代码:
select name
from sys.tables
where type = 'u'
and object_id not in
(
select parent_object_id
from sys.foreign_key_columns
)