English 中文(简体)
三. 更换服务器主角
原标题:Change primary key column in SQL Server

<>>>>>

这里是因询问而产生的制约因素。

SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME =  history 

CONSTRAINT_NAME   COLUMN_NAME  ORDINAL_POSITION
PK_history        userKey       1
PK_history        name          2

这里是询问的结果。

SELECT * 
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
WHERE TABLE_NAME =  history 

CONSTRAINT_NAME   CONSTRAINT_TYPE  IS_DEFERRABLE  INITIALLY_DEFERRED
PK_history        PRIMARY KEY      NO             NO

http://www.ohchr.org。

我的东道国通过伙伴关系向我的服务器-DB提供接口。 NET 企业经理。

页: 1 表:

  • userId (key, int, NULL not allowed)
  • name (key, string, NULL not allowed)
  • id (not key, int, NULL allowed)

我想把id栏作为唯一关键。

为此,我认为我需要:

  1. Make sure there are no NULLs in that column for any row
  2. Set the column to not allow NULLs
  3. Add the column as a primary key
  4. Remove the other 2 columns as keys

然而,当我使用《国际调查》时,它从未奏效。 有时,它只是想做一些事情,但在我重新审视各栏时,它从未改变过。 它偶尔会形成一个诱惑表,像它试图做某些行动一样,但从未像我试图改变的原始表格那样复制/篡改。

当我尝试使用询问时,这些变化也没有显示。 这里,我认为我需要:

    SELECT * from history WHERE id is NULL     <---- This shows 0 results

    ALTER TABLE history
    ALTER COLUMN id int NOT NULL

    ALTER TABLE history ADD PRIMARY KEY (id)

    ALTER TABLE history
    DROP CONSTRAINT userId
    DROP CONSTRAINT name
    GO

我只想去掉民族解放军,并补充id一栏的主要钥匙。 似乎没有工作。 谁能把我引向正确方向? 感谢!

问题回答

假设你目前的主要制约因素被称为“k”。

ALTER TABLE history ADD PRIMARY KEY (id)

ALTER TABLE history
DROP CONSTRAINT userId
DROP CONSTRAINT name

ALTER TABLE history DROP CONSTRAINT pk_history

ALTER TABLE history ADD CONSTRAINT pk_history PRIMARY KEY (id)

如果你不知道PK的名称是什么,你可以发现它有以下疑问:

SELECT * 
  FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
 WHERE TABLE_NAME =  history 

Necromancing.
It looks you have just 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 
        -- 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)
            ;

            -- Adding foreign key
            IF 0 = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME =  FK_T_ZO_SYS_Language_Forms_T_SYS_Language_Forms ) 
                ALTER TABLE T_ZO_SYS_Language_Forms WITH NOCHECK ADD CONSTRAINT FK_T_ZO_SYS_Language_Forms_T_SYS_Language_Forms FOREIGN KEY(ZOLANG_LANG_UID) REFERENCES T_SYS_Language_Forms(LANG_UID); 
        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 




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签