snippetsqlMinor
How to know dependency on a table
Viewed 0 times
tabledependencyknowhow
Problem
I have a Database Called Total and I have about 40 tables in it.I am trying to move the tables in the database into a different server So I have prepared all the create table scripts but the problem is all the tables have foreign keys so I want to know the order in which I have to execute the tables.
Is there any way to know it?
Is there any way to know it?
Solution
You should do it in two passes:
First create the tables:
Then add the foreign key constraints:
This way the there is no order dependency.
First create the tables:
create table Table1 (..., constraint pk_table1 primary key (...));
create table Table2 (..., constraint pk_table2 primary key (...));
..
create table TableN (..., constraint pk_tableN primary key (...));Then add the foreign key constraints:
alter table Table1 add constraint fk_table1_tablex
foreign key (...)
references TableX (...);
alter table Table2 add constraint fk_table2_tabley
foreign key (...)
references TableY (...);
...This way the there is no order dependency.
Code Snippets
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 (...);
...Context
StackExchange Database Administrators Q#6530, answer score: 4
Revisions (0)
No revisions yet.