我读了很多关于这个问题的题目。但我不解决我的问题。
我创建了两个表:污染者和警报。在警报中,我定义了外键污染者的身份。以下是表的创建代码:
CREATE TABLE IF NOT EXISTS Polluant (
idPol int(3) PRIMARY KEY AUTOINCREMENT,
nom varchar(10) UNIQUE NOT NULL,
unite varchar(10) NOT NULL
);
CREATE TABLE IF NOT EXISTS Alerte(
idAlerte int(3) PRIMARY KEY AUTO_INCREMENT,
unite varchar(10) NOT NULL,
Max1 int(10) NOT NULL,
Max2 int(10) NOT NULL,
Max3 int(10) NOT NULL,
type boolean NOT NULL,
idPol int(3),
FOREIGN KEY (idPol) REFERENCES Polluant(idPol)
);以下是参赛作品:
insert into polluant(nom,unite) values ("testPol","g/m3");
insert into alerte values (1,"test",1,2,3,true,1);
insert into alerte values (2,"autretest",2,4,40,false,1); !! this one isn't OK目前正在试用mysql,但它将尽快在sqllite上运行。
我用id=1在污染者表中添加了一个条目,在idPol =1.
我在table表中添加了第二个条目,其idPol =1。MySQL高兴地告诉我:
1062 -“idPol”的重复条目“1”
嗯哼。为什么?它必须是独一无二的,在餐桌污染的ofc,它是,但它不需要提醒,是吗?嗯,我不明白遗嘱,对我来说很好。有人有什么主意吗?Thinqs
发布于 2014-03-28 15:37:34
不要问我为什么,我仍然不明白,我删除了所有的数据库,并再次创建它--我只是在create表中添加了一个约束,它就可以工作了.thinqs @jmail作为你的时间。
CREATE TABLE IF NOT EXISTS Alerte(
idAlerte int(3) AUTO_INCREMENT,
unite varchar(10) NOT NULL,
Max1 int(10) NOT NULL,
Max2 int(10) NOT NULL,
Max3 int(10) NOT NULL,
type boolean NOT NULL,
idPol int(3),
PRIMARY KEY(idAlerte),
CONSTRAINT fk_pol FOREIGN KEY (idPol) REFERENCES Polluant(idPol));
发布于 2014-03-28 11:34:50
引用Polluant表,因此插入值不能重复。
FOREIGN KEY (idPol) REFERENCES Polluant(idPol)
很可能您的列被设置为唯一的列,并且您正在尝试使用已经存在于表中的ID输入行。
您可能试图插入ID (或其他字段)1集的记录,而该记录已经存在于表中。作为主键的字段必须具有每个记录的唯一值。
Setting the column to auto_increment and not inserting a value when inserting the row (letting it auto populate) would be the best fix. Or you could see the last ID in your table though, and increment it by one for your value
https://stackoverflow.com/questions/22711516
复制相似问题