//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   ASP.NET 2.0
   Classic ASP 1.0
   Databases
      Access DB & ADO
      General SQL Server & Access Articles
      MySQL
      Other Articles
      Schema Tutorials
      Sql Server 2000
      Sql Server 2005
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
databases.aspfaq.com

ASP FAQ Tutorials :: Databases :: Schema Tutorials :: Schema: How do I show all the primary keys in a database?


Schema: How do I show all the primary keys in a database?

SQL Server 
 
This query will return all of the primary keys, including those with multiple columns (and will return those in the order they are listed in the key definition): 
 
SELECT  
    T.TABLE_NAME,  
    T.CONSTRAINT_NAME,  
    K.COLUMN_NAME,  
    K.ORDINAL_POSITION  
FROM  
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS T 
    INNER JOIN 
    INFORMATION_SCHEMA.KEY_COLUMN_USAGE K 
    ON T.CONSTRAINT_NAME = K.CONSTRAINT_NAME  
WHERE 
    T.CONSTRAINT_TYPE = 'PRIMARY KEY'  
    -- AND T.TABLE_NAME = 'table_name' 
ORDER BY 
    T.TABLE_NAME, 
    K.ORDINAL_POSITION
 
You can also use the following methods: 
 
EXEC sp_pkeys '<tablename>' 
EXEC sp_helpconstraint '<tablename>' 
 
sp_pkeys will return a row for each column that participates in the primary key for <tablename>. The columns you are likely most interested in are COLUMN_NAME and PK_NAME. 
 
sp_helpconstraint will list all constraints for <tablename>, including foreign keys that reference the table. In the first recordset, there will only be a column called Object Name (kind of useless, since that's what you passed in). In the second resultset, there will be the following columns: constraint_type, constraint_name, and constraint_keys. 
 
Microsoft Access 
 
This code uses ADOX to iterate through all the primary keys: 
 
<%  
    Set conn = CreateObject("ADODB.Connection")  
    Set cat = CreateObject("ADOX.Catalog")  
    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _  
        "Data Source=<path to db>" 
    Set cat.ActiveConnection = conn  
 
    Response.Write "<table border=1><tr>" & _ 
        "<th>Table/Column</th>" & _ 
        "<th>Key Name</th></tr>" 
 
    For Each tbl in cat.Tables  
        if left(tbl.Name, 4) <> "MSys" then 
            For Each key in tbl.Keys  
                If key.Type = 1 Then 
                    For Each col in key.Columns  
                        Response.Write "<tr><td>" & tbl.Name & "." & _  
                            col.Name & "</td><td>" & _  
                            key.Name & "</td></tr>"  
                    Next  
                End If  
            Next  
        End If 
    Next  
 
    Response.Write "</table>" 
 
    Set cat = Nothing  
    conn.Close : Set conn = Nothing  
%>

Related Articles

Schema: How do I list all the indexes in a database?
Schema: How do I list the databases on my server?
Schema: How do I show all the triggers in a database?
Schema: How do I show the columns for a table?
Schema: How do I show the description property of a column?
Schema: How do I show the parameters for a function or stored procedure?
Schema: How do I show the stored procedures in a database?
Schema: How do I show the tables in a database?
Schema: How do I show the user-defined functions (UDFs) in a database?
Schema: How do I show the views in a SQL Server database?

 

 


Created: 2/25/2001 | Last Updated: 3/24/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (58)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...