Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008: Find out primary/foreign key in Table?

Tags:

sql-server

Does anyone know how I can see which are the primary & foreign keys in a table?

EDIT: Thanks for all the responses. I was looking for a SQL Query to do that. Right now I'm playing around with writing a tool which can list me all Tables of a DB and show the columns. I'd like to display also which of the keys are primary keys.

This is how I read out the Table Catalog:

const string sqlSelectTable = "SELECT  TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE " +
                                      "FROM INFORMATION_SCHEMA.TABLES " +
                                      "WHERE TABLE_TYPE = 'BASE TABLE' " +
                                      "ORDER BY TABLE_TYPE,TABLE_NAME";

And this is how I get the Infos about a Column:

const string sqlSelectTable =
            "SELECT     COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH " +
            "FROM         INFORMATION_SCHEMA.COLUMNS " +
            "WHERE     (TABLE_NAME = @TABLE_NAME) " +
            "ORDER BY ORDINAL_POSITION";

Would I have to create a Inner-Join so see which of the Columns are Primary Key?

Cheers

like image 322
Joseph jun. Melettukunnel Avatar asked Sep 03 '09 13:09

Joseph jun. Melettukunnel


People also ask

How can we identify primary key and foreign key in SQL Server?

If we want to know the table's primary keys and foreign keys. We can simply use an “information_schema. key_column_usage” view, this view will return all of the table's foreign keys and primary keys.

How do I know if a foreign key exists in a table?

You can use the OBJECTPROPERTY() function in SQL Server to check whether or not a table has one or more foreign key constraints. To do this, pass the table's object ID as the first argument, and TableHasForeignKey as the second argument.

How do you identify primary and foreign keys?

The primary key of a table is the column, or combination of columns, that uniquely identify a row in the table. Sometimes a row in a table must reference a row from another table. A foreign key is a column or combination of columns that hold the primary key value for a row in another table so that it can be referenced.


1 Answers

For the primary key on each table, you can use this query:

SELECT
    kc.name,
    c.NAME
FROM 
    sys.key_constraints kc
INNER JOIN 
    sys.index_columns ic ON kc.parent_object_id = ic.object_id
INNER JOIN 
    sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE
    kc.type = 'PK'

and for the foreign key, I believe this query should get you the necessary information:

SELECT
    OBJECT_NAME(parent_object_id) 'Parent table',
    c.NAME 'Parent column name',
    OBJECT_NAME(referenced_object_id) 'Referenced table',
    cref.NAME 'Referenced column name'
FROM 
    sys.foreign_key_columns fkc
INNER JOIN 
    sys.columns c 
       ON fkc.parent_column_id = c.column_id 
          AND fkc.parent_object_id = c.object_id
INNER JOIN 
    sys.columns cref 
       ON fkc.referenced_column_id = cref.column_id 
          AND fkc.referenced_object_id = cref.object_id

Marc

like image 191
marc_s Avatar answered Oct 11 '22 22:10

marc_s