如果我将两列设置为唯一,然后两次插入相同的值,那么数据库将如何表现?它会忽略第二个插入还是替换第一个?
答案 0 :(得分:2)
答案 1 :(得分:0)
'columns'是字段,每个字段单独唯一。
CREATE TABLE `mydb`.`example` (
`rid` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`basecolor` VARCHAR( 12 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`trimcolor` VARCHAR( 12 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
UNIQUE (
`basecolor`
)
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE `example` ADD UNIQUE (
`trimcolor`
)
INSERT INTO example (basecolor, trimcolor) VALUES ('red', 'red'), ('green', 'green'), ('blue', 'green')
#1062 - Duplicate entry 'green' for key 'trimcolor'
SELECT * FROM example
+------------------------------+
| rid | basecolor | trimcolor |
|======|===========|===========|
| 1 | red | red |
| 2 | green | green |
+------------------------------+