MySQL 插入相同的行
MySQL inserting same rows
有一个sqltable这样的
这样的索引
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(4,6,2);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(4,6,1);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(4,6,0);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(5,6,0);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(8,7,1);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(8,9,2);
I want to a unique index is together with iduser,idword.
How to avoid inserting query with same values together index columns?
For example: Bloking row inserting like 13 and 14 ( idusers_words ) rows
SELECT * FROM users_words;
> idusers_words | iduser | idword | status_of_word |
12 4 6 2
13 4 6 1
14 4 6 0
15 5 6 0
16 8 7 1
17 8 9 2
编辑:
I tried this code. But comes that error "#1062 - Duplicate entry '1-1' for key 'my_unique'"
ALTER TABLE users_words
ADD CONSTRAINT my_unique UNIQUE(iduser,idword);
您的唯一索引应该只在 iduser 和 idword 列上以满足您的要求。
您在 idusers_words,iduser and idword.
上有索引,这意味着所有三列的组合应该是唯一的。
因为idusers_words是自动递增的,这个条件总是会被满足,即使idusers和idword是重复的,记录也会被插入。
您可以像这样对一组字段施加约束:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
)
有一个sqltable这样的
这样的索引
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(4,6,2);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(4,6,1);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(4,6,0);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(5,6,0);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(8,7,1);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(8,9,2);
I want to a unique index is together with iduser,idword. How to avoid inserting query with same values together index columns? For example: Bloking row inserting like 13 and 14 ( idusers_words ) rows
SELECT * FROM users_words;
> idusers_words | iduser | idword | status_of_word |
12 4 6 2
13 4 6 1
14 4 6 0
15 5 6 0
16 8 7 1
17 8 9 2
编辑:
I tried this code. But comes that error "#1062 - Duplicate entry '1-1' for key 'my_unique'"
ALTER TABLE users_words
ADD CONSTRAINT my_unique UNIQUE(iduser,idword);
您的唯一索引应该只在 iduser 和 idword 列上以满足您的要求。
您在 idusers_words,iduser and idword.
上有索引,这意味着所有三列的组合应该是唯一的。
因为idusers_words是自动递增的,这个条件总是会被满足,即使idusers和idword是重复的,记录也会被插入。
您可以像这样对一组字段施加约束:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
)