237

我有一个名为Persion. 在此表中,我有 5 列:

  • persionId
  • 名称
  • PMID
  • 描述
  • 帕姆特

创建此表时,我将PersionIdand设置Pname为主

我现在想在主键中再包含一列 - PMID。我怎样才能写一个ALTER声明来做到这一点?(我的表中已经有 1000 条记录)

4

11 回答 11

230

drop constraint and recreate it

alter table Persion drop CONSTRAINT <constraint_name>

alter table Persion add primary key (persionId,Pname,PMID)

edit:

you can find the constraint name by using the query below:

select OBJECT_NAME(OBJECT_ID) AS NameofConstraint
FROM sys.objects
where OBJECT_NAME(parent_object_id)='Persion'
and type_desc LIKE '%CONSTRAINT'
于 2012-08-03T11:19:23.410 回答
97

我认为这样的事情应该有效

-- drop current primary key constraint
ALTER TABLE dbo.persion 
DROP CONSTRAINT PK_persionId;
GO

-- add new auto incremented field
ALTER TABLE dbo.persion 
ADD pmid BIGINT IDENTITY;
GO

-- create new primary key constraint
ALTER TABLE dbo.persion 
ADD CONSTRAINT PK_persionId PRIMARY KEY NONCLUSTERED (pmid, persionId);
GO
于 2012-08-03T11:33:08.117 回答
43
-- create new primary key constraint
ALTER TABLE dbo.persion 
ADD CONSTRAINT PK_persionId PRIMARY KEY NONCLUSTERED (pmid, persionId);

是一个更好的解决方案,因为您可以控制 primary_key 的命名。


这比仅仅使用更好

ALTER TABLE Persion ADD PRIMARY KEY(persionId,Pname,PMID)

这会产生随机名称,并且在编写脚本或比较数据库时可能会导致问题

于 2015-02-27T13:59:00.640 回答
30

如果添加主键约束

ALTER TABLE <TABLE NAME> ADD CONSTRAINT <CONSTRAINT NAME> PRIMARY KEY <COLUMNNAME>  

例如:

ALTER TABLE DEPT ADD CONSTRAINT PK_DEPT PRIMARY KEY (DEPTNO)
于 2017-02-22T22:55:41.193 回答
22

您的表中已经有一个主键。不能只添加主键,否则会报错。因为sql表只有一个主键。

首先,您必须删除旧的主键。

MySQL:

ALTER TABLE Persion
DROP PRIMARY KEY;

SQL Server / Oracle / MS 访问:

ALTER TABLE Persion
DROP CONSTRAINT 'constraint name';

您必须在表中找到约束名称。如果您在创建表时给出了约束名称,则可以轻松使用约束名称(例如:PK_Persion)。

二、添加主键。

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persion ADD PRIMARY KEY (PersionId,Pname,PMID);

或者下面更好的

ALTER TABLE Persion ADD CONSTRAINT PK_Persion PRIMARY KEY (PersionId,Pname,PMID);

这可以由开发人员设置约束名称。维护表更容易。

当我查看所有答案时,我有点困惑。所以我研究了一些文件来找到每一个细节。希望这个答案可以帮助其他 SQL 初学者。

参考:https ://www.w3schools.com/sql/sql_primarykey.asp

于 2018-12-19T02:55:43.217 回答
6

死灵术。
以防万一有人和我一样有一个很好的模式可以使用......
这是正确的方法:

在本例中,表名是 dbo.T_SYS_Language_Forms,列名是 LANG_UID

-- First, chech if the table exists...
IF 0 < (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_TYPE = 'BASE TABLE'
    AND TABLE_SCHEMA = 'dbo'
    AND TABLE_NAME = 'T_SYS_Language_Forms'
)
BEGIN
    -- Check for NULL values in the primary-key column
    IF 0 = (SELECT COUNT(*) FROM T_SYS_Language_Forms WHERE LANG_UID IS NULL)
    BEGIN
        ALTER TABLE T_SYS_Language_Forms ALTER COLUMN LANG_UID uniqueidentifier NOT NULL 

        -- No, don't drop, FK references might already exist...
        -- Drop PK if exists (it is very possible it does not have the name you think it has...)
        -- ALTER TABLE T_SYS_Language_Forms DROP CONSTRAINT pk_constraint_name 
        --DECLARE @pkDropCommand nvarchar(1000) 
        --SET @pkDropCommand = N'ALTER TABLE T_SYS_Language_Forms DROP CONSTRAINT ' + QUOTENAME((SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
        --WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' 
        --AND TABLE_SCHEMA = 'dbo' 
        --AND TABLE_NAME = 'T_SYS_Language_Forms' 
        ----AND CONSTRAINT_NAME = 'PK_T_SYS_Language_Forms' 
        --))
        ---- PRINT @pkDropCommand 
        --EXECUTE(@pkDropCommand) 
        -- Instead do
        -- EXEC sp_rename 'dbo.T_SYS_Language_Forms.PK_T_SYS_Language_Forms1234565', 'PK_T_SYS_Language_Forms';

        -- Check if they keys are unique (it is very possible they might not be)        
        IF 1 >= (SELECT TOP 1 COUNT(*) AS cnt FROM T_SYS_Language_Forms GROUP BY LANG_UID ORDER BY cnt DESC)
        BEGIN

            -- If no Primary key for this table
            IF 0 =  
            (
                SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
                WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' 
                AND TABLE_SCHEMA = 'dbo' 
                AND TABLE_NAME = 'T_SYS_Language_Forms' 
                -- AND CONSTRAINT_NAME = 'PK_T_SYS_Language_Forms' 
            )
                ALTER TABLE T_SYS_Language_Forms ADD CONSTRAINT PK_T_SYS_Language_Forms PRIMARY KEY CLUSTERED (LANG_UID ASC)
            ;

        END -- End uniqueness check
        ELSE
            PRINT 'FSCK, this column has duplicate keys, and can thus not be changed to primary key...' 
    END -- End NULL check
    ELSE
        PRINT 'FSCK, need to figure out how to update NULL value(s)...' 
END 
于 2015-10-23T09:35:34.427 回答
4

PRIMARY KEY 约束唯一标识数据库表中的每条记录。主键必须包含 UNIQUE 值,列不能包含 NULL 值。

  -- DROP current primary key 
  ALTER TABLE tblPersons DROP CONSTRAINT <constraint_name>
  Example:
  ALTER TABLE tblPersons 
  DROP CONSTRAINT P_Id;


  -- ALTER TABLE tblpersion
  ALTER TABLE tblpersion add primary key (P_Id,LastName)
于 2015-02-18T04:37:14.530 回答
2

请试试这个-

ALTER TABLE TABLE_NAME DROP INDEX `PRIMARY`, ADD PRIMARY KEY (COLUMN1, COLUMN2,..);
于 2018-04-11T14:49:21.720 回答
1
ALTER TABLE TABLE_NAME ADD PRIMARY KEY(`persionId`,`Pname`,`PMID`)
于 2016-12-06T05:35:20.437 回答
0

alter table[Person] add ID int 主键 IDENTITY (1,1)

这将添加主键和标识种子并填充新列。

于 2021-05-27T07:05:47.747 回答
0

尝试使用此代码:

ALTER TABLE `table name` 
    CHANGE COLUMN `column name` `column name` datatype NOT NULL, 
    ADD PRIMARY KEY (`column name`) ;
于 2016-08-11T14:06:22.740 回答