如果我将两列设置为唯一,然后两次插入相同的值,数据库将如何表现?它会忽略第二个插入还是替换第一个?
问问题
66 次
2 回答
2
于 2013-08-08T22:17:57.803 回答
0
“列”是字段,每个字段都是唯一的。
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 |
+------------------------------+
于 2013-08-08T22:57:21.143 回答