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 (https://blog.sqlauthority.com)
38 Comments. Leave new
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
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
ALTER TABLE
ADD CONSTRAINT PRIMARY KEY (COLUMN_NAME_1, [COLUMN_NAME_2], [COLUMN_NAME_n])
where n is the nth number, as a Primary Key can have more than one Column
ALTER TABLE TABLE_NAME
ADD CONSTRAINT CONSTRAINT_NAME PRIMARY KEY (COLUMN_NAME_1, [COLUMN_NAME_2], [COLUMN_NAME_n])
where n is the nth number, as a Primary Key can have more than one Column
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
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.
Hi Kash,
you can delete child table records first then parent table through constraint on delete cascade constraints.
You can set this constraints at the same time while creating relationship between two tables.
Thanks
KENSAi
Hi Pinal,
I have one Query,
How to List out of all indexes in particular table and database ??
Hi Prasad,
To get the list of all indexes of a table use the below query:
SELECT ind.*
FROM sys.indexes ind INNER JOIN sys.tables tbl
ON ind.object_id = tbl.object_id
WHERE tbl.name = ‘yourTableName’
To get the list of all index in a database use the sys.indexes table.
Regards,
Pinal Dave
I have two tables one of them(table A) has 2 Primary keys (Composite keys) and another one (Table B) has own primary key and a Foreign key which is one of the existing primary key of the Table A.
I am going to make a relation between table A and Table B in this relation Table A is my Primary key Table and Table B is my Foreign key table,and in Foreign key table i select one primary key of Table A which should be set as a primary key field in Table A as Primary key table. when i want to make this relation i get this Error : ‘ The column in the table don’t match an Existing primary key or UNIQUE constraint.
Help me please
@Azadeh
Please post some SQL.
IIUC, you need to ADD a UNIQUE CONSTRAINT to Table B on the PK and FK.
I want to know how to find the primary key for a specific given table.
how to insert primary key after table is created
Hemadri
Right click on the specified table choose design option. Right click on the column to which you need to create primary key. You will find set primary key option. Just select that.
For sec I thought I should call you god. But I think praying god to give you long life and everything you want in life is more appropriate :-) you are simply awesome buddy!!