Add primary key to existing table

后端 未结 10 1537
生来不讨喜
生来不讨喜 2020-12-07 11:40

I have an existing table called Persion. In this table I have 5 columns:

  • persionId
  • Pname
  • PMid
  • Pdescription
  • Pa
相关标签:
10条回答
  • 2020-12-07 12:31

    Necromancing.
    Just in case anybody has as good a schema to work with as me...
    Here is how to do it correctly:

    In this example, the table name is dbo.T_SYS_Language_Forms, and the column name is 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 
    
    0 讨论(0)
  • 2020-12-07 12:32

    Please try this-

    ALTER TABLE TABLE_NAME DROP INDEX `PRIMARY`, ADD PRIMARY KEY (COLUMN1, COLUMN2,..);
    
    0 讨论(0)
  • 2020-12-07 12:33

    I think something like this should work

    -- 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
    
    0 讨论(0)
  • 2020-12-07 12:35
    ALTER TABLE TABLE_NAME ADD PRIMARY KEY(`persionId`,`Pname`,`PMID`)
    
    0 讨论(0)
提交回复
热议问题