473,395 Members | 1,938 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,395 software developers and data experts.

orphan recods

Hi,

Say, a database has all the FK constraints created properly, would sql
server 2000 save this kind of meta data into sysforeignkeys table? Or
it depends on ...?

I'm trying to find all the orphen records in a given database via meta
data.

TIA.

Jul 23 '05 #1
7 2269

"NickName" <da****@rock.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

Say, a database has all the FK constraints created properly, would sql
server 2000 save this kind of meta data into sysforeignkeys table? Or
it depends on ...?

I'm trying to find all the orphen records in a given database via meta
data.

TIA.


sysforeignkeys contains the table and column IDs for all foreign keys (see
sysreferences too), but it doesn't contain any user data, only metadata. I'm
not really sure what you want to do, but I guess you may want to build your
queries dynamically? If so, you can use the OBJECT_NAME() and COL_NAME()
functions to get the table and column names:

select
object_name(fkeyid) as 'ReferencingTable',
col_name(fkeyid, fkey) as 'ReferencingColumn',
object_name(rkeyid) as 'ReferencedTable',
col_name(rkeyid, rkey)'ReferencedColumn'
from
sysforeignkeys

Simon
Jul 23 '05 #2
>> I'm trying to find all the orphen records[sic] in a given database
via meta data. <<

Wouldn't be more informative to just use a query with an NOT EXISTS()
predicate? And then add the DRI actions that were missing the first
time.

I assumethat you do not plan on messing up the schema so often you need
a general tool for cleaning up.

Jul 23 '05 #3
Simon,

That is very similar to what I was doing except that I did not know
about OBJECT_NAME() and COL_NAME() functions.

Thanks.

Don

Jul 23 '05 #4
Joe,

While I appreciate you acclaimed knowledge of sql, I have to say you
seem to have this wicked sense/urge of driling a needle into other's
palm :)

No, no messing around of schema, and again, when one inherits a huge
database he/she never really has sufficient knowledge of it, hence, the
need for a general tool to tackle it.

Don

Jul 23 '05 #5
NickName (da****@rock.com) writes:
Say, a database has all the FK constraints created properly, would sql
server 2000 save this kind of meta data into sysforeignkeys table? Or
it depends on ...?

I'm trying to find all the orphen records in a given database via meta
data.


Hm, if foreign keys are set up properly, there should not really be
orphans to be found through meta-data. ...if it wasn't for the fact
that you can disable a foreign key, and when you re-enable it the
default is not check its validity. Whether you have any such foreign
keys, you can find out by adding this column to Simon's query:

objectproperty(constid, 'CnstIsNotTrusted')

DBCC CHECKCONSTRAINTS is the easiest way to find out if you have any
violations. However, even if there are not, this will not make constraints
trusted.
--
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 #6
Erland,

I ran DBCC CHECKCONSTRAINTS
with all constraints option
against several databases, at least one of them I know that has orphen
records (for I created one for testing), and yet, DBCC ... returned
"(1 row(s) affected)

DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
"
across board, how come?

Thanks.

Don

Jul 23 '05 #7
NickName (da****@rock.com) writes:
I ran DBCC CHECKCONSTRAINTS
with all constraints option
against several databases, at least one of them I know that has orphen
records (for I created one for testing), and yet, DBCC ... returned
"(1 row(s) affected)

DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
"
across board, how come?


I don't know exactly how you define orphan rows. Then again, if you were
able to create a row that violated the foreign-key constraint, then the
constraint is obviously not enabled, and Books Online says that DBCC
CHECKCONSTRATINS only checks enabled constraints. The repro below demon-
straints.

CREATE TABLE main1 (a1 int NOT NULL,
CONSTRAINT pk_main1 PRIMARY KEY(a1))
go
CREATE TABLE main2 (a2 int NOT NULL,
CONSTRAINT pk_main2 PRIMARY KEY(a2))
go
CREATE TABLE child (a1 int NOT NULL,
a2 int NOT NULL,
CONSTRAINT pk_child PRIMARY KEY (a2, a1),
CONSTRAINT fk_child1 FOREIGN KEY(a1)
REFERENCES main1 (a1),
CONSTRAINT fk_child2 FOREIGN KEY(a2)
REFERENCES main2 (a2))
go
INSERT main1 (a1) VALUES (1)
INSERT main2 (a2) VALUES (2)
INSERT child (a1, a2) VALUES (1, 2)
go
ALTER TABLE child NOCHECK CONSTRAINT fk_child1
ALTER TABLE child NOCHECK CONSTRAINT fk_child2
go
INSERT child (a1, a2) VALUES (11, 2)
INSERT child (a1, a2) VALUES (1, 12)
go
ALTER TABLE child WITH NOCHECK CHECK CONSTRAINT fk_child1
go
DBCC CHECKCONSTRAINTS ('child')
go
DROP TABLE child, main1, main2

You can use the objectproperty() function to find disabled constraint. The
repro also shows hows to enable the constraints, but better is to say
WITH CHECK CHECK. The you will check the constraint when you enabled it,
and you will get an error if there are orphans. No report of the orphans,
though. The good thing with WITH CHECK is that the constraint will be
trusted by the optimizer and can thus be used in query plans.


--
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 #8

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

Similar topics

0
by: John Thorne | last post by:
Have 2 tables ts_sheets : TimeSheet_ID TimeSheet_Date Emp_ID Orphan LineItem TimeSheet_ID Client
3
by: James | last post by:
I need to define paragraphs on my site that should not break across a page when printed, similar to widow/orphan control in Word. Is this possible to implement? We only use IE6 so no cross-platform...
0
by: net guest via DotNetMonster.com | last post by:
Hi, i am facing a lot of problems in killing the orphan excel instances. I approached one method like killing the Excel Orphan with processID. I am getting all the Processess that are running...
0
by: prabhupr | last post by:
Hi Folks In CS project, we use "Add Reference" option to make reference to other assemblies. At times, there a is a chance that few of them get added by mistake or must have got added for some...
1
by: Gunnar | last post by:
Are there any way of detecting that a program have crashed and left an orphan file? I have tried with the filesystemwatcher, but that didn't work. Maybe I can use a polling strategy and keep...
1
by: julian_m | last post by:
I'm working with mysql without referential itegrity. Let me make some small example: tableA +-----------+---------------+ | id_1 | data_1 | +-----------+---------------+ | 1 ...
2
by: julian_m | last post by:
I'm working with mysql without referential itegrity. Let me make some small example: tableA +-----------+---------------+ | id_1 | data_1 | +-----------+---------------+ | 1 ...
3
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. Is there a way to create a registry key, but orphan it from the program that created it? Reason: Create a key, but not associate it with the...
2
by: smilem | last post by:
Hi, I use Gallery2 v2.3 system on my web server. When I add and delete photos I see orphan records in my db. The project is opensource and developers are focused on new v3 of the product. However...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
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...

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.