[Err] 23000 - [SQL Server] Unique key creation, insert error

Description of the problem: Creating a unique key is an error message

[Err] 23000 - [SQL Server]
因为发现对象名称 'dbo.t_table' 和索引名称 'username' 有重复的键,
所以 CREATE UNIQUE INDEX 语句终止。
重复的键值为 (201104, <NULL>, 24)。
42000 - [SQL Server]无法创建约束。请参阅前面的错误消息。
01000 - [SQL Server]语句已终止。

Problem handling: The unique key that is set needs to be deduplicated before creating a unique key


Description: After the unique key is successfully created, insert a piece of data that is duplicated with the unique key and the following error will be reported

[Err] 23000 - [SQL Server]违反了 UNIQUE KEY 约束 'uni_1'。
不能在对象 'dbo.t_table' 中插入重复键。
01000 - [SQL Server]语句已终止。

唯一键分为主键(主键索引)和唯一索引(UNIQUE 索引),
意思就是在某一列中不能出现重复值,

但主键索引和唯一索引的区别是,主键索引不允许为空,
而唯一索引允许为空,其中空(null)是可以不唯一的。

设置方法分别如下:

创建主键索引:
alter table tablename add constraint 主键名 primary  key(字段名);

创建唯一索引:

CREATE UNIQUE CLUSTERED INDEX myclumn_cindex ON mytable(mycolumn);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324483307&siteId=291194637