sql server表已有主键,将其删掉,重新建主键

--如果您不知道主键constraint的名称,请运行以下脚本

SELECT CONSTRAINT_NAME  
FROM   INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE  TABLE_NAME = '表名'  -- Table Name
       AND TABLE_SCHEMA = 'dbo'  -- change it if table is in some other schema
       AND CONSTRAINT_TYPE = 'PRIMARY KEY'
ALTER TABLE 表名-- Table Name
DROP CONSTRAINT CONSTRAINT_NAME(上面脚本获取的)
ALTER TABLE 表名 AND CONSTRAINT PK_NAME PRIMARY KEY(字段一,字段二)

你可能感兴趣的:(sqlserver,sql)