Question

I am currently trying to retrieve the table names and primary key of each table by calling the following SQL in C#. This is what I've tried so far:

SELECT t.TABLE_NAME As 'Table Name',
       Keys.COLUMN_NAME AS 'Primary Key'
FROM INFORMATION_SCHEMA.TABLES t 
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS Constraints
         on  t.TABLE_NAME = Constraints.Table_name 
         and t.Table_Schema = Constraints.Table_Schema 
left outer join INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS Keys 
         ON  Constraints.TABLE_NAME = Keys.TABLE_NAME 
         and Constraints.CONSTRAINT_NAME = Keys.CONSTRAINT_NAME 
         and Constraints.CONSTRAINT_TYPE = 'PRIMARY KEY'

The problem is that I would like the result to just contain a "null" entry if a table does not have a primary key. While it is working, for some reason the result contains a duplicate of the "Salary" table as shown below. The Salary and Employees tables both have primary keys, and the "Test" table does not:

How can I get rid of the 3rd record?

EDIT: I've tried this now, and it seems to work. Are there any disadvantages to doing so?

SELECT T.TABLE_NAME As 'Table Name'
    ,       (
            Select K1.COLUMN_NAME
            From INFORMATION_SCHEMA.TABLE_CONSTRAINTS As C1
                Join INFORMATION_SCHEMA.KEY_COLUMN_USAGE As K1
                    On C1.TABLE_SCHEMA = K1.TABLE_SCHEMA
                        And C1.TABLE_NAME = K1.TABLE_NAME 
                        And C1.CONSTRAINT_NAME = K1.CONSTRAINT_NAME 
            Where C1.CONSTRAINT_TYPE = 'PRIMARY KEY'
                And T.TABLE_SCHEMA = C1.TABLE_SCHEMA 
                And T.TABLE_NAME = C1.TABLE_NAME 
            )As PrimaryKeyColumns
FROM INFORMATION_SCHEMA.TABLES As T 
Was it helpful?

Solution

Try replace this condition Constraints.CONSTRAINT_TYPE = 'PRIMARY KEY' :

SELECT t.TABLE_NAME As 'Table Name',
       Keys.COLUMN_NAME AS 'Primary Key'
FROM INFORMATION_SCHEMA.TABLES t 
left outer join INFORMATION_SCHEMA.TABLE_CONSTRAINTS Constraints
         on  t.TABLE_NAME = Constraints.Table_name 
         and t.Table_Schema = Constraints.Table_Schema  
         and Constraints.CONSTRAINT_TYPE = 'PRIMARY KEY'       
left outer join INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS Keys 
         ON  Constraints.TABLE_NAME = Keys.TABLE_NAME 
         and Constraints.CONSTRAINT_NAME = Keys.CONSTRAINT_NAME 

OTHER TIPS

You should account for your PK having multiple columns. You can do this by changing to a subquery:

SELECT T.TABLE_NAME As 'Table Name'
    ,   Stuff(
            (
            Select ', ' + K1.COLUMN_NAME
            From INFORMATION_SCHEMA.TABLE_CONSTRAINTS As C1
                Join INFORMATION_SCHEMA.KEY_COLUMN_USAGE As K1
                    On C1.TABLE_SCHEMA = K1.TABLE_SCHEMA
                        And C1.TABLE_NAME = K1.TABLE_NAME 
                        And C1.CONSTRAINT_NAME = K1.CONSTRAINT_NAME 
            Where C1.CONSTRAINT_TYPE = 'PRIMARY KEY'
                And T.TABLE_SCHEMA = C1.TABLE_SCHEMA 
                And T.TABLE_NAME = C1.TABLE_NAME 
            For Xml Path(''), type
            ).value('.', 'nvarchar(max)')
            , 1, 2, '') As PrimaryKeyColumns
FROM INFORMATION_SCHEMA.TABLES As T 

Btw, if you really want to use Joins to the TABLES view, you need to nest the joins. The problem is that you have two outer joins and the columns from the foreign keys are generating rows in your output. It should be noted that the downside to the following approach over the earlier mentioned subquery approach is that you will get multiple rows if a primary key consists of multiple columns. That said, you can nest joins in SQL Server so that it processes the inner, inner join first and then the outer join:

SELECT T.TABLE_NAME As 'Table Name'
    , Keys.COLUMN_NAME AS 'Primary Key'
FROM INFORMATION_SCHEMA.TABLES As T
    Left Join( INFORMATION_SCHEMA.TABLE_CONSTRAINTS Constraints
        Join INFORMATION_SCHEMA.KEY_COLUMN_USAGE As Keys 
            On  Constraints.TABLE_SCHEMA = Keys.TABLE_SCHEMA 
                And Constraints.TABLE_NAME = Keys.TABLE_NAME 
                And Constraints.CONSTRAINT_NAME = Keys.CONSTRAINT_NAME 
                And Constraints.CONSTRAINT_TYPE = 'PRIMARY KEY' )
        On T.TABLE_SCHEMA = Constraints.TABLE_SCHEMA 
            And T.TABLE_NAME = Constraints.TABLE_NAME 

In this query, the Inner Join between TABLE_CONSTRAINTS and KEY_COLUMN_USAGE is processed first and the results are combined in a Left Join to TABLES view.

I see you have already accepted your answer, but here's a pure C# answer in case someone is interested:

static void Main(string[] args)
{
    Server server = new Server("serverName");
    Database db = server.Databases["DatabaseName"];

    string tableName = "TableName";

    Table table = db.Tables[tableName];

    if (table != null)
    {
        Console.WriteLine("Table:  {0}", tableName);
        if (table.Columns.Count > 0)
        {
            Console.WriteLine("  Primary Key Columns:");
            foreach (Column column in table.Columns)
            {
                if (column.InPrimaryKey)
                {
                    Console.WriteLine(string.Format("    {0}", column.Name));
                }
            }
        }
        else
        {
            Console.WriteLine("  No primary key.", tableName);
        }
    }
    Console.WriteLine("Press ENTER to exit...");
    Console.ReadLine();
}

You will need to add references to the Microsoft.SqlServer.Management.Sdk.Sfc and Microsoft.SqlServer.Smo assemblies and import the appropriate namespaces.

select
  TABLE_SCHEMA, 
  TABLE_NAME 
from 
  INFORMATION_SCHEMA.TABLES
where 
  objectproperty(object_id(table_name),'TableHasPrimaryKey')=0
ORDER BY 
  TABLE_NAME
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top