473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Table order when inserting data

sam
I have to import data into a empty database, that has many tables.
some tables have to be inserted first than others due to the foreign
keys.

How do I find out the order of the tables that I have to insert data
into?

Thanks in advance!

Sam

Jul 23 '05 #1
6 1721
"sam" <sa*****@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I have to import data into a empty database, that has many tables.
some tables have to be inserted first than others due to the foreign
keys.

How do I find out the order of the tables that I have to insert data
into?

Thanks in advance!

Sam


You need to insert to each parent table before the tables that reference it
with foreign keys. Do you have a database diagram? If not you can generate
one in Enterprise Manager and identify the dependency order from that.

--
David Portas
SQL Server MVP
--
Jul 23 '05 #2
"sam" <sa*****@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I have to import data into a empty database, that has many tables.
some tables have to be inserted first than others due to the foreign
keys.

How do I find out the order of the tables that I have to insert data
into?

Thanks in advance!

Sam


You need to insert to each parent table before the tables that reference it
with foreign keys. Do you have a database diagram? If not you can generate
one in Enterprise Manager and identify the dependency order from that.

--
David Portas
SQL Server MVP
--
Jul 23 '05 #3

You can check for Table dependencies in EM

best Regards,
Chandra
http://groups.msn.com/SQLResource/
http://chanduas.blogspot.com/
---------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #4

You can check for Table dependencies in EM

best Regards,
Chandra
http://groups.msn.com/SQLResource/
http://chanduas.blogspot.com/
---------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #5
sam
I have too many tables in the database that I could not check the
denpency one by one. I am looking for a SQL statement that looks
through the system tables such as sysobjects and others to return the
table names in the proper order.

Jul 23 '05 #6
sam (sa*****@hotmail.com) writes:
I have too many tables in the database that I could not check the
denpency one by one. I am looking for a SQL statement that looks
through the system tables such as sysobjects and others to return the
table names in the proper order.


First do this:

SELECT 'ALTER TABLE ' + object_name(parent_obj) +
' NOCHECK CONSTRAINT ' + name
FROM sysobjects
WHERE xtype = 'F'
ORDER BY object_name(parent_obj)

Copy and paste into query window and run. This disables all foreign keys.

Second, bulk load your files in any order.

Third, run:

SELECT 'ALTER TABLE ' + object_name(parent_obj) +
' WITH CHECK CHECK CONSTRAINT ' + name
FROM sysobjects
WHERE xtype = 'F'
ORDER BY object_name(parent_obj)

This reenables the constraints and also checks that data satisfies the
constraints. If you change WITH CHECK to WITH NOCHECK, the constraints
will be enabled without checking. However, in this case, SQL Server
will mark the constraint as "not trusted", which means that the optimizer
will not consider them when building query plans.

(And yes that syntax is really WITH CHECK CHECK.)

I should also add that this is good for Sam, because he has an empty
database. If you have a similar problem for a live database, you need
to be more discriminate, if there are other operations going on in
parallel with your bulk-load.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: traceable1 | last post by:
I am inserting data rows into a table via a stored procedure. After the inserts, I query the rows in the table and I want them to spit back out in the same order I put them in. However,...
1
by: Jay | last post by:
Hi I have a huge table with over 100million records and on regular basis ineed to delete nearly a million records and insert a million records. Currently I delete indexes before going through the...
3
by: JB | last post by:
To anyone that is able to help.... What I am trying to do is this. I have two tables (Orders, and OrderDetails), and my question is on the order details. I would like to set up a stored...
5
by: tilak.negi | last post by:
We have one single hash (#) table, in which we insert data processing priority wise (after calculating priority). for. e.g. Company Product Priority Prod. Qty Prod_Plan_Date C1...
2
by: binary-nomad | last post by:
Hello, To followup my last post, how do I keep a field in a table already sorted. eg. if a field in it going to have values like "10", "3330" and "1", I want the row with the "1' first in the...
2
by: Charles Wilt | last post by:
I have a IBM iSeries (aka AS-400) running v5r3 of OS/400 that I access via a linked server from SQL Server 2000. The following select works fine: select * from...
9
by: Oonz | last post by:
Hi Friends, How can we insert records in sorted order like consider a table No Name Phone 1 test1 12345 1 test1 23455 2 test2 68638
4
bugboy
by: bugboy | last post by:
I'm inserting a new word into table 'w' and a definition into table 'c' which are linked in table 's' which is the relation table for the many to many relationship between 'w' and 'c'. I've been...
9
by: JWM81274 | last post by:
Is there a statement that will allow appending to the end of table when inserting new data? I use multiple "insert into table1(timestamp1,area1,value1,value2...)" to insert different area data with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.