473,396 Members | 2,037 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,396 software developers and data experts.

Regarding table Order

Greetings,
I have an application that need to get all the user
defined child tables first before their parents.
I wrote a query, given in this newsgroup only, as below

SELECT o.name
FROM sysobjects o
WHERE o.type='U'
ORDER BY case WHEN exists ( SELECT *
FROM sysforeignkeys f
WHERE o.id = f.fkeyid )
THEN 1
ELSE 0
end, o.name
go

When i try to truncate the first table of the list, it still tells me
that tha table is being referenced by foreign key in another table. My
main job is to truncate all the user defined tables before loading data
into them.
Is there something wrong in the query? Or if someone can tell me a
better approach.

Any help will be appreciated.

TIA

Dec 16 '05 #1
5 1328
Hi,

You can do one of these:

a)
1. Drop all the FK-s
2. Truncate the tables (in any order)
3. Re-create the FK-s

b)
1. Delete the tables (in a particular order, so FK-s won't be
violated).

Of course, DELETE is slower than TRUNCATE, but if there are many FK-s
and not too much data, I preffer not to drop the FK-s.

Razvan

Dec 16 '05 #2
Like said in the BOL:

"You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY
constraint; instead, use DELETE statement without a WHERE clause."

Therefore the only way is to delete the FK and do the truncate and
recreate the FK, or to use an insert statement instead.

HTH, jens Suessmeyer.

Dec 16 '05 #3
thank for the help. i hope i can get back to u ppl if any more
questions.
thanx again

Dec 16 '05 #4
>> Is there something wrong in the query? Or if someone can tell me a better approach. <<

The correct terms are "referenced" and "referencing" tables; "child"
and "parent" are terms from IDMS, IMS and other network DBMS systems.

It is always a dangerous thing to do queries and execture statements on
the schema information tables. It says that you do not know what you
are doing until run time.

Why not put DRI actions for ON DELETE CASCADE on the referencing tables
and let the system do the work properly so you do not have to do this
kind of manual housecleaning?

Why do you allow users to define tables on the fly in a production
database? That means you have no data model and users control the
schema, so you do not even know the names of your entities.

TRUNCATE is both proprietrary and dangerous (see what it does with
logging, its limitations, etc.)

The kludge to fix the bad design is to create DELETE FROM statements on
the fly. The real answer is to get a workable schema and to get rid of
this code.

Dec 16 '05 #5
> Why do you allow users to define tables on the fly in a production
database? That means you have no data model and users control the
schema, so you do not even know the names of your entities.
Think Knowledge Management, where you define your knowledge model on the fly
and build the table structure to support it; better that then creating
1,000's of tables containing generic stuff.
TRUNCATE is both proprietrary and dangerous (see what it does with
logging, its limitations, etc.)


What does it do with logging? Why is it dangerous?

What rubbish.

TRUNCATE TABLE is logged, SQL Server logs extent deallocations so is fully
recoverable if in the middle of the truncate a problem occurs.

Yes there are limitations, specifically and for good reason, not being able
to use it when you have foriegn key constraints - you should know that.

Say you have a 10GB table that you want to clear down and empty - would you
really issue a DELETE so that ALL the rows and index pages are logged
causing a 10GB+ log file, it would also take an exceedingly long time to
run. Its one of the first things you learn as a DBA, TRUNCATE is better than
DELETE when clearing down a table.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"--CELKO--" <jc*******@earthlink.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Is there something wrong in the query? Or if someone can tell me a
better approach. <<


The correct terms are "referenced" and "referencing" tables; "child"
and "parent" are terms from IDMS, IMS and other network DBMS systems.

It is always a dangerous thing to do queries and execture statements on
the schema information tables. It says that you do not know what you
are doing until run time.

Why not put DRI actions for ON DELETE CASCADE on the referencing tables
and let the system do the work properly so you do not have to do this
kind of manual housecleaning?

Why do you allow users to define tables on the fly in a production
database? That means you have no data model and users control the
schema, so you do not even know the names of your entities.

TRUNCATE is both proprietrary and dangerous (see what it does with
logging, its limitations, etc.)

The kludge to fix the bad design is to create DELETE FROM statements on
the fly. The real answer is to get a workable schema and to get rid of
this code.

Dec 16 '05 #6

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

Similar topics

8
by: LG | last post by:
Just have a question with regards to the clipboard, and how to read what other applications (Adobe InDesignCS) place in the clipboard. I am currently in the process of creating a booklet from a...
5
by: Jeremy | last post by:
I am relatively inexperienced with SQL, and I am trying to learn how to analyze some data with it. I have a table with the following information. COMPANY ID , DATE, MarektValue I would like...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
9
by: noone | last post by:
I have a database file that I use an autonumber field as the primary key index. Because of some rearrangements in the past, this index does not match the order that I would like it to be in, that...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
2
by: Terrance | last post by:
Hello, I have a question in regards to hashtables that I was hoping someone can share some light on. I'm currently working with VB.net and I'm trying to learn as much as possible about the...
15
by: rAinDeEr | last post by:
Suppose i have a table which holds thousands of records with the following structure CREATE TABLE "test "."T_CNTRY" ( "CNTRY_CDE" CHAR(2) NOT NULL , "CNTRY_NAME" VARCHAR(50) ) and i have...
8
by: Roger | last post by:
I have a question regarding the behaviour of sql with OR and fetch first 1 rows only : I have a table with data : ACNO NAME TELNO CITY ZIP 1000 ...
3
by: deepthithallada | last post by:
Hi, While inserting records in the table, is there any way we can change the order in which we insert the records. Eg: Say i have 3 columns in a table, while inserting the records can i make...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.