SQL Server UNIQUE失效的问题
CREATE TABLE dbo.Code_Index
(
code SMALLINT IDENTITY(1,1) CONSTRAINT PK_Code_Index_code PRIMARY KEY CLUSTERED (code ASC) WITH(PAD_INDEX=ON,FILLFACTOR=80,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON),
item VARCHAR(20) NOT NULL CONSTRAINT UQ_Code_Index_code UNIQUE NONCLUSTERED (code ASC) WITH(PAD_INDEX=ON,FILLFACTOR=80,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)
)
以上命令建立的表格,依旧能够输入重复数据为什么?
注:输入重复数据是,提示提交数据错误,此数据为只读,表中依旧有两条记录.
SQL?Server
唯一
------解决方案--------------------你写错了
CREATE TABLE dbo.Code_Index
(
code SMALLINT IDENTITY(1,1) CONSTRAINT PK_Code_Index_code PRIMARY KEY CLUSTERED (code ASC) WITH(PAD_INDEX=ON,FILLFACTOR=80,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON),
item VARCHAR(20) NOT NULL CONSTRAINT UQ_Code_Index_code UNIQUE NONCLUSTERED (item ASC) WITH(PAD_INDEX=ON,FILLFACTOR=80,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)
)
INSERT INTO Code_Index(item)
VALUES ('a')
INSERT INTO Code_Index(item)
VALUES ('a')
SELECT * FROM Code_Index
------解决方案--------------------CREATE TABLE dbo.Code_Index
(
code SMALLINT IDENTITY(1,1) CONSTRAINT PK_Code_Index_code PRIMARY KEY CLUSTERED (code ASC) WITH(PAD_INDEX=ON,FILLFACTOR=80,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON),
item VARCHAR(20) NOT NULL CONSTRAINT UQ_Code_Index_code UNIQUE NONCLUSTERED (
code ASC) WITH(PAD_INDEX=ON,FILLFACTOR=80,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)
)
红字部分如果你是用code,那么由于是自增,是不会重复的,应该改成item
------解决方案--------------------try this,
CREATE TABLE dbo.Code_Index
(
code SMALLINT IDENTITY(1,1) CONSTRAINT PK_Code_Index_code PRIMARY KEY CLUSTERED (code ASC)
WITH(PAD_INDEX=ON,FILLFACTOR=80,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON),
item VARCHAR(20) NOT NULL CONSTRAINT UQ_Code_Index_code UNIQUE NONCLUSTERED (item ASC)
WITH(PAD_INDEX=ON,FILLFACTOR=80,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)
)