2011-04-05 74 views
1

我想在SQL Server 2000中创建一个表,其中有一个IGNORE_DUP_KEY设置为ON的复合主键。在具有复合主键的Sql Server 2000中的IGNORE_DUP_KEY

我试过在SQL Server Management Studio Express中寻找这个选项,但是我找不到它,所以现在我只能以编程方式创建表。每一个SQL命令,我发现在谷歌或堆栈溢出给我一个错误:

Incorrect syntax near '('.

表应当有4列(A,B,C,d)所有十进制(18)和我需要一个主键,公元前。

如果有人可以发布示例CREATE命令,我将不胜感激。

+0

什么是IGNORE_DUP_KEY主键的含义?它看起来像一个设计缺陷! – jachguate 2011-04-05 01:11:37

+0

我期待这样的评论:),但只要你考虑到所有方面(好与坏),我不认为这是一个设计缺陷 - >这是现在对我的工作。我希望我的项目是唯一的,但我不想在插入过程中验证它们的唯一性。我已经看到它可以使用连接手动完成,但我怀疑它和使用IGNORE_DUP_KEY一样快。如果你愿意,我可以更多地描述我将在哪里使用它。 – 2011-04-05 01:18:47

回答

3
create table MyTable2 (
    [a] decimal(18,2) not null, 
    [b] decimal(18,2) not null, 
    [c] decimal(18,2) not null, 
    [d] decimal(18,2), 
    CONSTRAINT myPK PRIMARY KEY (a,b,c) 
) 

CREATE UNIQUE INDEX MyUniqueIgnoringDups 
ON MyTable2 (a,b,c) 
    WITH IGNORE_DUP_KEY  --SQL 2000 syntax 
    --WITH(IGNORE_DUP_KEY = On) --SQL 2005+ syntax 

--insert some data to test. 
insert into mytable2 (a,b,c,d) values (1,2,3,4);--succeeds; inserts properly 
insert into mytable2 (a,b,c,d) values (1,2,3,5);--insert fails, no err is raised. 
-- "Duplicate key was ignored. (0 row(s) affected)" 

任何有兴趣,这里是什么从Erland Sommarskog on the MSDN forums发生的解释:

When IGNORE_DUP_KEY is OFF, a duplicate key value causes an error and the entire statement is rolled back. That is, if the statement attempted to insert multiple rows, no rows are inserted.

When IGNORE_DUP_KEY is ON, a duplicate key value is simply ignored. The statement completes successfully and any other rows are inserted.

+0

感谢您的回答,但它说'IGNORE_DUP_KEY'不是一个确认的ALTER TABLE选项。有任何想法吗? – 2011-04-05 01:26:36

+0

完美。它正在工作。谢谢!。但是,在您提供的链接中,我发现了一个与性能和兼容性问题相关的有趣链接。他们对我来说没问题,但如果有人使用它,那么你应该检查出这个链接。 – 2011-04-05 01:32:14

+0

是的,我见过:)。实际上,有两个单独的命令,允许我在已经创建的表上运行CREATE INDEX。无论如何,我虽然接受解决方案就足够了:) – 2011-04-05 01:38:53