Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Ghost Constraint

Author  Topic 

WalkerDA
Yak Posting Veteran

61 Posts

Posted - 2004-08-24 : 18:39:53
I have a ghost constraint that I need to find. The constraint does not appear to exist as a table dependancy nor does it exist in sysobjects or sysconstraints..though when I try to drop the table SQL tells me that it does exist. Any thoughts?

Derrick

nr
SQLTeam MVY

12543 Posts

Posted - 2004-08-24 : 18:42:14
What's the error message?


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-08-25 : 04:03:29
Have you tried, in no particular order:

1. sp_help, or sp_helpconstraint on the table
2. scripting the table with all the constraints to see if you find it
3. in QA view the Constraints defined on the table. ( you can do this in the objectbrowser )

/rockmoose
Go to Top of Page

WalkerDA
Yak Posting Veteran

61 Posts

Posted - 2004-08-25 : 09:11:04
quote:
Originally posted by rockmoose

Have you tried, in no particular order:

1. sp_help, or sp_helpconstraint on the table
2. scripting the table with all the constraints to see if you find it
3. in QA view the Constraints defined on the table. ( you can do this in the objectbrowser )

/rockmoose



Here is the most explicit message that appears useful to some degree:

Server: Msg 3725, Level 16, State 1, Line 1
The constraint 'PK_COMP_ID' is being referenced by table '224719853', foreign key constraint '272720024'.
Server: Msg 3727, Level 16, State 1, Line 1
Could not drop constraint. See previous errors.

The only on that I didn't try was sp_helpconstraint, it does send back some information, though the object name or data is NULL..so it appears that someone has deleted or dropped the table that did reference the table in question, of course that was apparent early on. The question is how do I fix something that I cannot find?

Here is the result from sp_helpconstraint:

Table is referenced by foreign key
------------------------------------
NULL

There are 3 other constraints on the table, 2 for default values and 1 as the PK which is the fk in the table that doesn't exist. Thankfully this is a table that is rarely touched and is on the dev side but I would still like to know how to fix it, that way maybe I will understand how to prevent it or how it happened.



Derrick
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-08-25 : 09:20:58
Run a DBCC CHECKTABLE('tablename') on the table in question.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

WalkerDA
Yak Posting Veteran

61 Posts

Posted - 2004-08-25 : 09:44:35
Results:

DBCC results for 'rptCOMP'.
There are 526 rows in 4 pages for object 'rptCOMP'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.


Derrick
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-08-25 : 13:13:22
Hi,

Run this to see if you have rogue objects in your sysreferences or sysconstraints tables


select * from sysreferences
where fkeyid not in( select id from sysobjects )
or rkeyid not in( select id from sysobjects )

select * from sysconstraints
where id not in( select id from sysobjects )


If you do the system tables have been altered somehow.
You will need to clean up these rogue tables from sysconstraints and sysreferences
before you can drop your table.

You probably have some more seriuos problem if your system tables have been altered / corrupted.

/rockmoose
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-08-25 : 13:19:24
Here is a reproduction of the error that you got.


set nocount on

-- create storage place for object id that we are about to delete from sysobjects
create table #missing_table( id int not null primary key )


-- create tables with reference
create table pktbl(id int not null primary key)
create table fktbl(id int not null primary key, pktbl_id int not null references pktbl(id) )

-- store away id of fktble
insert #missing_table(id) select object_id('fktbl')



exec sp_helpconstraint 'pktbl'

/*
Table is referenced by foreign key
----------------------------------------------
MV.dbo.fktbl: FK__fktbl__pktbl_id__1F10398A
*/

delete sysobjects where id = ( select id from #missing_table )

drop table pktbl
/*
Server: Msg 3726, Level 16, State 1, Line 1
Could not drop object 'pktbl' because it is referenced by a FOREIGN KEY constraint.
*/

exec sp_helpconstraint 'pktbl'
/*
Table is referenced by foreign key
----------------------------------------------
NULL
*/

delete sysreferences where fkeyid = ( select id from #missing_table )
delete sysconstraints where id = ( select id from #missing_table )

exec sp_helpconstraint 'pktbl'
/*
No foreign keys reference this table.
*/


drop table pktbl
/*
The command(s) completed successfully.
*/


-- cleanup
drop table #missing_table



/rockmoose
Go to Top of Page

WalkerDA
Yak Posting Veteran

61 Posts

Posted - 2004-08-25 : 17:14:09
Awesome call..never used sysreferences before... Of course I had to update the catalog, but things look fine with the table...

Any thoughts on a Trigger for System Tables..never used one but one would be good in cases like this so maybe it will send an email or further prohibit action in the table.

Thanks again...

Derrick
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-08-25 : 17:18:50
From BOL:

"Because SQL Server does not support user-defined triggers on system tables, it is recommended that no user-defined triggers be created on system tables."

Tara
Go to Top of Page

WalkerDA
Yak Posting Veteran

61 Posts

Posted - 2004-08-25 : 17:52:40
Sadly there is no reference to its protection outside of the config. settings... Thanks for the help!

Derrick
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-08-25 : 17:52:48
You shouldn't allow modifications to the system tables.
If your server is set up that way try to find out if there is a reason for this, or just an oversight.
( checkbox on "server settings" tab on the Sql Server propertise dialog )
see BOL - allow updates Option

As for triggers on system tables, no I don't think that is a good idea.


/rockmoose
Go to Top of Page

WalkerDA
Yak Posting Veteran

61 Posts

Posted - 2004-08-25 : 18:50:58
It's a project Server..so we r using it for development though I haven't any control over who does what..sucks..but I've learned from experience not to take project work to heart!

Derrick
Go to Top of Page
   

- Advertisement -