My article SQL SERVER - 2005 Find Table without Clustered Index - Find Table with no Primary Key has received following question many times. I have deleted similar questions and kept only latest comment there.
In SQL Server 2005 How to Find Tables With Primary Key Constraint in Database?
Script to find all the primary key constraint in database:
USE AdventureWorks;
GO
SELECT i.name AS IndexName,
OBJECT_NAME(ic.OBJECT_ID) AS TableName,
COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.OBJECT_ID = ic.OBJECT_ID
AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1
In SQL Server 2005 How to Find Tables With Foreign Key Constraint in Database?
SQL SERVER - 2005 - Find Tables With Foreign Key Constraint in Database
Reference : Pinal Dave (http://www.SQLAuthority.com)






hi
grt query !
but i have a different question
i want to know whether i can delete a specfic record in sql server 2005 prior to execute the delete query.
mean if the record will have foreign key constraint and with the records child records will exists then db will not allow me to delete that record.
i want to know whether master table have child recrods or not
is there any query to do so ?
thanks in advance.
tgc
faheem
Whilst the above may/does work for SQL2k5 ,
The following also works for SQL2k DB.
I had to crib this together because some of our processes are ’so generic’ they don;t know what table they’ll be passed let alone what the primary key will be (for use in DML)
SELECT SC.Name, SC.COLID
FROM SYSOBJECTS SO
INNER JOIN SYSINDEXKEYS SI ON SO.ID = SI.ID AND SI.INDID = 1
INNER JOIN SYSCOLUMNS SC ON SO.ID = SC.ID AND SC.COLID=SI.COLID
WHERE SO.NAME= @MyTableName
AND SO.XTYPE=’U’
Corrections are welcome.
hi
i read about ur article do you know difference between
rowlevel primary key and column level if yes send me an example of this also
thanks
lalit bohra
How to find the constraint name of a particular Table at SQL SERVER 2005
Please Help Me
[...] SQL SERVER - 2005 - Find Tables With Primary Key Constraint in Database SQL SERVER - 2005 - Find Tables With Foreign Key Constraint in Database [...]
How to delete records parent and child tables
waiting for your query
with regards
sekar
How do u find out the which column is the primary key column in a table if the we are not able to view the design or the script the tabel?
Thanks,
Bharath
Why we see all related table data on a primary key. for example a employe table e1 has a primary key id and e2 has f. k id and e3 has also id as a f.k. we search all data on 3 tables e1 e2 e3 on primary key without gaves table e2 and e3 names only e1 and primary key
i want to join two table with same column name but different values.
hi,
i want to know how do we define multiple primary keys into a table.when i gave command twice, it is showing syntax error
expecting reply soon
raghu
I want to know how to write a constraint i.e a primary key in already defined table.Alter syntax with constraints
I’m havin two database, could i map a foeign key constraint in the other database
e.g
database A->table xyz.id set primary key
I’wanna make foreign key to
database B-> table xyz.pid
is it possible
Hello Shwetang,
What I read on internet, is it is not possible to create a foriegn key constraint referencing table outside database.
I tried myself creating this but had no luck…
The only solution to your problem is triggers.
or create child table in the same database as parent table.
Thanks,
Imran
Imran,
You are correct.
Regards,
Pinal Dave ( http://www.SQLAuthority.com )
I have one parent table and has 10 child tables.
I would like to delete the child table records first then perent
database level, through constraints and triggers.
All these tables has forign key constrains.
Please advise the better way. I am using sql server 2005.