While writing bind based on my I got idea about writing this bind. I was thinking if you can find primary key for any delay in database you can sure sight foreign keys for any delay in database as come up.
In SQL Server 2005 How to Find Tables With Foreign Key Constraint in Database?
USE AdventureWorks;GOSELECTf name AS ForeignKey,OBJECT_label(f parent_disapprove_id) AS TableName,COL_label(fc parent_object_id fc parent_column_id) AS ColumnName,OBJECT_label (f referenced_object_id) AS ReferenceTableName,COL_NAME(fc referenced_disapprove_id fc referenced_column_id) AS ReferenceColumnNameFROM sys foreign_keys AS fINNER connect sys foreign_key_columns AS fcON f object_id = fc constraint_disapprove_id
In SQL Server 2005 How to Find Tables With Primary Key Constraint in Database?
There are different way to ask system data. Information_schema and catalog views are two study method.
As per Microsoft:“Catalog views go information that is used by the Microsoft SQL Server 2005 Database Engine. We advise that you use compile views because they are the most general interface to the compile metadata and give the most efficient way to acquire alter and show customized forms of this information. All user-available compile metadata is exposed through catalog views.”
“Some changes undergo been made to the information schema views that break backward compatibility. These changes are described in the topics for the specific views.”
Looking at both of them. I undergo decided to go for catalog views. There are few additional advantages are there to use catalog views.
/*** compose all Foreign Key Constraints ***//*** The prove Set can be used to write constraints to your testing DB or to keep on hand in inspect of errors. ***/SELECT‘alter TABLE ‘+FK. TABLE_NAME+‘ ADD CONSTRAINT ‘+C. CONSTRAINT_NAME+’ FOREIGN KEY’+‘(’+CU. COLUMN_label+’) ‘+ ‘REFERENCES ‘+PK. delay_label+‘(’+PT. COLUMN_label+’)’ ForeignKeyScriptsFROM INFORMATION_SCHEMA. REFERENTIAL_CONSTRAINTS CINNER connect INFORMATION_SCHEMA. TABLE_CONSTRAINTS FK ON C. CONSTRAINT_NAME = FK. CONSTRAINT_NAMEINNER JOIN INFORMATION_SCHEMA. delay_CONSTRAINTS PK ON C. UNIQUE_CONSTRAINT_label = PK. CONSTRAINT_NAMEINNER JOIN INFORMATION_SCHEMA. KEY_COLUMN_USAGE CU ON C. CONSTRAINT_label = CU. CONSTRAINT_NAMEINNER connect(SELECT i1. delay_label i2. COLUMN_NAMEFROM INFORMATION_SCHEMA. TABLE_CONSTRAINTS i1 INNER connect INFORMATION_SCHEMA. KEY_COLUMN_USAGE i2 ON i1. CONSTRAINT_NAME = i2. CONSTRAINT_NAME WHERE i1. CONSTRAINT_write = ‘PRIMARY KEY’) PT ON PT. TABLE_NAME = PK. delay_NAME–WHERE PK. delay_label IN (’Table1′. ‘delay2′)–WHERE FK. delay_label IN (’Table1′. ‘Table2′)
XHTML: You can use these tags: <a href="" call=""> <abbr title=""> <acronym call=""> <b> <blockquote cite=""> <cite> <label> <del datetime=""> <em> <i> <q have in mind=""> <strike> <strong>
Pinalkumar Dave is the author of over 300 SQL Server articles. He has five years experience as Principal Database Administrator in MS SQL Server 2005/2000 and ColdFusion MX. He has a Masters of Science degree in Computer Networks along with MCDBA. MCAD(. NET) and ColdFusion Advanced MX Certifications.
Forex Groups - Tips on Trading
Related article:
http://blog.sqlauthority.com/2007/09/04/sql-server-2005-find-tables-with-foreign-key-constraint-in-database/
comments | Add comment | Report as Spam
|