SQL SERVER – 2005 – Find Tables With Primary Key Constraint in Database

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)

, ,
Previous Post
SQL SERVER – 2005 – Find Tables With Foreign Key Constraint in Database
Next Post
SQLAuthority News – Interesting Read – Using A SQL JOIN In A SQL UPDATE/Delete Statement – Ben Nadel

Related Posts

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

    Reply
  • 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.

    Reply
  • 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

    Reply
  • How to find the constraint name of a particular Table at SQL SERVER 2005

    Please Help Me

    Reply
  • How to delete records parent and child tables

    waiting for your query

    with regards
    sekar

    Reply
  • 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

    Reply
  • Sanju Bhatia
    June 4, 2008 2:17 pm

    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

    Reply
  • i want to join two table with same column name but different values.

    Reply
  • 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

    Reply
  • I want to know how to write a constraint i.e a primary key in already defined table.Alter syntax with constraints

    Reply
    • 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

      Reply
    • 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

      Reply
  • 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

    Reply
  • 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

    Reply
  • Imran,

    You are correct.

    Regards,
    Pinal Dave ( https://blog.sqlauthority.com/ )

    Reply
  • 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.

    Reply
    • 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

      Reply
  • Hi Pinal,
    I have one Query,
    How to List out of all indexes in particular table and database ??

    Reply
  • 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

    Reply
  • 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

    Reply
    • @Azadeh

      Please post some SQL.

      IIUC, you need to ADD a UNIQUE CONSTRAINT to Table B on the PK and FK.

      Reply
  • I want to know how to find the primary key for a specific given table.

    Reply
  • how to insert primary key after table is created

    Reply
  • 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.

    Reply

Leave a Reply