Finding a Primary Key Constraint on the fly in SQL Server 2005

前端 未结 3 769
余生分开走
余生分开走 2021-01-14 10:22

I have the following SQL:

 ALTER TABLE dbo.PS_userVariables DROP CONSTRAINT PK_PS_userVariables;
 ALTER TABLE dbo.PS_userVariables ADD PRIMARY KEY (varnumber         


        
3条回答
  •  攒了一身酷
    2021-01-14 11:04

    SELECT 
       A.TABLE_NAME, 
       A.CONSTRAINT_NAME, 
       B.COLUMN_NAME
    FROM 
       INFORMATION_SCHEMA.TABLE_CONSTRAINTS A, 
       INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE B
    WHERE 
          CONSTRAINT_TYPE = 'PRIMARY KEY' 
       AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
    ORDER BY 
       A.TABLE_NAME 
    

    Ref: Pinal Dave @ http://blog.sqlauthority.com/2008/09/06/sql-server-find-primary-key-using-sql-server-management-studio/

提交回复
热议问题