简体   繁体   English

无法插入SQL脚本INSERT INTO值?

[英]SQL script INSERT INTO values unnable to be inserted?

I used a script to make a tables within the same database. 我使用脚本在同一数据库中制作了一个表。 Once both tables were made, I made a seperate script to insert a set of values in the table, however with one of the scripts it doesnt seem to insert any data into the table, but comes up with the error. 制作完两个表后,我制作了一个单独的脚本以在表中插入一组值,但是对于其中一个脚本,它似乎没有在表中插入任何数据,但出现了错误。 These are my tables; 这些是我的桌子;

CREATE TABLE IF NOT EXISTS customers(
customer_id INT UNSIGNED NOT NULL,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
password VARCHAR(40) NOT NULL,
dob DATETIME NOT NULL,
address_line VARCHAR(40) NOT NULL,
postcode VARCHAR(20) NOT NULL,
PRIMARY KEY(customer_id),
FOREIGN KEY(postcode) REFERENCE postcodes(postcode)
);

In a seperate script, 在单独的脚本中,

CREATE TABLE IF NOT EXISTS postcodes(
postcode VARCHAR(20) NOT NULL,
address_line_2 VARCHAR(20),
city VARCHAR(40) NOT NULL,
PRIMARY KEY(postcode)    
);

The scripts to insert data into the tables are here. 将数据插入表的脚本在这里。 This one works without any errors and successfully populates the table. 该程序可以正常运行,并且没有任何错误,并且可以成功地填充表格。

INSERT INTO postcodes(postcode,address_line_2,city)
Values
    ('DH1 568','Forest Lane','Durham'),
    ('DH1 679','Dry Wood','Durham'),
    ('DH1 4AS','North Of the Wall','Westeros'),
    ('DH1 4LA',"Snoop's Crib",'Durham');

And this is the one which comes up with an error message, 这是一则错误消息,

INSERT INTO customers(customer_id,first_name,postcode)
values
    ('1','Zaak','DH1 568'),
    ('2','Matt','DH1 679'),
    ('3','Jon','DH1 4AS'),
    ('4','Zak','DH1 4LA'),
    ('5','Gaz','DH1 7SO');

The error message which appears is, 出现的错误信息是,

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`hardware_store`.`customers`, CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`postcode`) REFER
ENCES `postcodes` (`postcode`))    

You have a foreign key constraint specifying that customers(postcode) refers to a valid postal code in the postcodes table. 您有一个外键约束,指定customers(postcode)引用postcodes表中的有效邮政编码。

Then, you try to insert 'DH1 7SO' and it doesn't work because this postcode is not in postcodes . 然后,您尝试插入'DH1 7SO' ,但由于该邮政编码不在postcodes ,因此它不起作用。

This is how foreign key references work. 这就是外键引用的工作方式。 The database is working exactly as it should and doing exactly what you instructed it to do. 数据库正在按应有的方式工作,并按照您的指示进行操作。

If you want the valid rows to be inserted and the invalid ones ignored, then use the IGNORE option on INSERT (see here ). 如果要插入有效行,而忽略无效行,请在INSERT上使用IGNORE选项(请参见此处 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL 插入,获取插入的 id 并再次插入 - SQL insert, get inserted id and insert again 如何使 php 脚本运行插入 sql 查询,然后从插入的行中获取信息? 我是初学者 - How to make php script to run insert sql Query and then get information from the inserted rows ? I am a Beginner NULL 值未插入到 sql 表中 - NULL values are not inserted into sql table PHP / SQL在将值插入表并将一个值设置为唯一之后,无法再插入其他值 - PHP/SQL After insert value to table and set one value as unique, no more values can be inserted 插入脚本返回成功,但未将任何内容插入数据库 - Insert script returning success but nothing inserted to database 已成功插入值,但如何使用自动增量插入? - Have successfully inserted the values but, how to insert with autoincrement? SQL 脚本不会在 Joomla 中的组件安装中插入值 - SQL-script does not insert values in component installation in Joomla 如何基于表中先前插入的值创建插入前触发器 - How to create a before insert trigger based on previously inserted values in the table 当在主数据库中插入行时,SQL插入行相关 - SQL Insert row in related when a row is inserted in master MySQL INSERT 触发器:SQL 引用插入行时出现语法错误 - MySQL INSERT Trigger: SQL Syntax Error when referencing inserted row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM