Pages

Thursday, March 22, 2012

How to list columns with identity, primary key and foreign key flags?


Execute the following Microsoft SQL Server T-SQL script for listing table column meta data including column names, TRUE/FALSE (1/0) flags for IsIdentity, IsPrimaryKey and IsForeignKey properties.
-- SQL table column meta data - SQL table columns - int identity - Primary Key constraints
-- SQL ISPRIMARYKEY - SQL ISFOREIGNKEY - SQL ISIDENTITY - SQL INFORMATION_SCHEMA views
-- SQL columnproperty - SQL data dictionary - Foreign Key constraints
USE AdventureWorks2008;
SELECT   c.TABLE_CATALOG                            AS DatabaseName,
         c.TABLE_SCHEMA                             AS SchemaName,
         c.TABLE_NAME                               AS TableName,
         c.COLUMN_NAME                              AS ColumnName,
         Columnproperty(Object_id(c.TABLE_SCHEMA + '.' + c.TABLE_NAME),
                        c.COLUMN_NAME,'ISIDENTITY') AS IsIdentity,
         CASE
           WHEN CONSTRAINT_NAME IN (SELECT NAME
                                    FROM   sys.objects
                                    WHERE  TYPE = 'PK') THEN 1
           ELSE 0
         END AS IsPrimaryKey,
         CASE
           WHEN CONSTRAINT_NAME IN (SELECT NAME
                                    FROM   sys.objects
                                    WHERE  TYPE = 'F') THEN 1
           ELSE 0
         END AS IsForeignKey
FROM     INFORMATION_SCHEMA.TABLES t
         INNER JOIN INFORMATION_SCHEMA.COLUMNS c
             ON c.TABLE_CATALOG = t.TABLE_CATALOG
              AND c.TABLE_SCHEMA = t.TABLE_SCHEMA
              AND c.TABLE_NAME = t.TABLE_NAME
         LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE u
           ON c.TABLE_CATALOG = u.TABLE_CATALOG
              AND c.TABLE_SCHEMA = u.TABLE_SCHEMA
              AND c.TABLE_NAME = u.TABLE_NAME
              AND c.COLUMN_NAME = u.COLUMN_NAME
WHERE TABLE_TYPE='BASE TABLE'
ORDER BY SchemaName,
         TableName,
         c.ORDINAL_POSITION
GO

-- Partial results 

DatabaseNameSchemaNameTableNameColumnNameIsIdentityIsPrimaryKeyIsForeignKey
AdventureWorks2008SalesSalesOrderDetailSalesOrderID010
AdventureWorks2008SalesSalesOrderDetailSalesOrderID001
AdventureWorks2008SalesSalesOrderDetailSalesOrderDetailID110
AdventureWorks2008SalesSalesOrderDetailCarrierTrackingNumber000
AdventureWorks2008SalesSalesOrderDetailOrderQty000
AdventureWorks2008SalesSalesOrderDetailProductID001
AdventureWorks2008SalesSalesOrderDetailSpecialOfferID001
AdventureWorks2008SalesSalesOrderDetailUnitPrice000
AdventureWorks2008SalesSalesOrderDetailUnitPriceDiscount000
AdventureWorks2008SalesSalesOrderDetailLineTotal000
AdventureWorks2008SalesSalesOrderDetailrowguid000
AdventureWorks2008SalesSalesOrderDetailModifiedDate000
AdventureWorks2008SalesSalesOrderHeaderSalesOrderID110
AdventureWorks2008SalesSalesOrderHeaderRevisionNumber000
AdventureWorks2008SalesSalesOrderHeaderOrderDate000
AdventureWorks2008SalesSalesOrderHeaderDueDate000
AdventureWorks2008SalesSalesOrderHeaderShipDate000
AdventureWorks2008SalesSalesOrderHeaderStatus000
AdventureWorks2008SalesSalesOrderHeaderOnlineOrderFlag000
AdventureWorks2008SalesSalesOrderHeaderSalesOrderNumber000
AdventureWorks2008SalesSalesOrderHeaderPurchaseOrderNumber000
AdventureWorks2008SalesSalesOrderHeaderAccountNumber000
AdventureWorks2008SalesSalesOrderHeaderCustomerID001
AdventureWorks2008SalesSalesOrderHeaderSalesPersonID001
AdventureWorks2008SalesSalesOrderHeaderTerritoryID001
AdventureWorks2008SalesSalesOrderHeaderBillToAddressID001
AdventureWorks2008SalesSalesOrderHeaderShipToAddressID001
AdventureWorks2008SalesSalesOrderHeaderShipMethodID001
AdventureWorks2008SalesSalesOrderHeaderCreditCardID001
AdventureWorks2008SalesSalesOrderHeaderCreditCardApprovalCode000
AdventureWorks2008SalesSalesOrderHeaderCurrencyRateID001
AdventureWorks2008SalesSalesOrderHeaderSubTotal000
AdventureWorks2008SalesSalesOrderHeaderTaxAmt000
AdventureWorks2008SalesSalesOrderHeaderFreight000
AdventureWorks2008SalesSalesOrderHeaderTotalDue000
AdventureWorks2008SalesSalesOrderHeaderComment000
AdventureWorks2008SalesSalesOrderHeaderrowguid000
AdventureWorks2008SalesSalesOrderHeaderModifiedDate000

No comments:

Post a Comment