This is my shell script to insert csv data into my mysql table but we are getting the below error.
"ERROR 1452 (23000) at line 1: Cannot add or update a child row: a
foreign key constraint fails (inv_impact.asset_ext_warranty,
CONSTRAINT fk_asset_ext_warranty FOREIGN KEY (vendor_id)
REFERENCES vendor (vendor_id) ON DELETE NO ACTION ON UPDATE NO
ACTION)"
Shell script is like:
#!/bin/bash
IFS=,
while read offering auth_no DOP price extend_from extend_to status vendor_id
do echo "INSERT INTO asset_ext_warranty(offering, auth_no, DOP, price, extend_from, extend_upto, status, vendor_id) VALUES ('$offering','$auth_no','$DOP','$price','$extend_from','$extend_upto','$status','$vendor_id');" done < warr-ext.csv | mysql -uroot -p'pass' dbname;
while the parents table has the vendor_id with the referencing values
vendor_id | vendor_name | person_name | phone_no | mail_id | vendor_detail |
+-----------+----------------------------+--------------------+---------------+---------------------------+------------------------------------------------------------------------------------+
| 1 | nsiah | Hshiwq | 0981002364 | suajs#yahoo.co.in | NULL |
| 2 | Dell International service | Dell | 1800-425-4026 | NULL | NULL |
| 3 | Integral system ltd | Mamata | NULL | NULL | NULL
the child row(vendor_id) in csv file has the same value as parents row .
Related
State Problem
Apart from 1st col in MySQL server which is PK and auto_increment, col "C" is STRING/VARCHAR. How do I use INSERT ... ON DUPLICATE KEY UPDATE on col "C".
Should I also set col "C" as UNIQUE as well?
Example
Assume that my table in MySQL server looks similar to this...
---------------------------
|ID(PK)| A | B | C | D | E |
+------+---+---+---+---+---+
| 1 | | | | | |
| 2 | | | | | |
| 3 | | | | | |
---------------------------
What I expect
The duplicate input must be ignored from the MySQL server.
Can I also send alert pop-up/session pop-up, if the duplicate has been inserted into MySQL server as "This query has already existed."?
Column C must be set to NOT NULL, UNIQUE then SQL will return error against attempt of duplicate value insert. You may handle that error in your backend code. Or you can handle and alter the duplicate value with 'ON DUPLICATE KEY UPDATE' clause:
INSERT INTO t1 (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
ALL,
igor#IgorDellGentoo ~ $ isql myodbc-5.2-test root wasqra
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> use draft;
SQLRowCount returns 0
SQL> ALTER TABLE owners ADD FOREIGN KEY id REFERENCES leagues(id);
[ISQL]ERROR: Could not SQLExecute
SQL>
What am I doing wrong?
Also, for some reason I can't create a foreign key thru the mySQL-Workbench when creating the table.
There is no "Add" button or "+" sign to add this constraint. And there is no reaction on the right click.
Could someone please point me to the right direction?
I have Workbench version 6.3.4.0 on Gentoo Linux.
SQL> show tables;
+-----------------------------------------------------------------+
| Tables_in_draft |
+-----------------------------------------------------------------+
| leagues |
| owners |
+-----------------------------------------------------------------+
SQLRowCount returns 2
2 rows fetched
SQL> SELECT * FROM leagues;
+-----------+-----------------------------------------------------------------------------------------------------+-----------+------------+-----------+-----------+-----------+-------------+
| id | name | drafttype | scoringtype| roundvalue| leaguetype| salary | benchplayers|
+-----------+-----------------------------------------------------------------------------------------------------+-----------+------------+-----------+-----------+-----------+-------------+
+-----------+-----------------------------------------------------------------------------------------------------+-----------+------------+-----------+-----------+-----------+-------------+
seems you are using id as a foreign key use proper column instead
ALTER TABLE owners
ADD COLUMN FOREIGNID INT NOT NULL;
ALTER TABLE owners
ADD FOREIGN KEY (FOREIGNID) REFERENCES leagues(ID);
I'm trying like hours to make a simple MySQL command and it won't work.
I have a database where package IDs for Apps are stored, like 'com.android.package'.
They are stored like so:
| ID | PackageID | PackageDesc |
| 1 | com.android.package | This is a package |
| 2 | com.android.test2pa | This is package 2 |
And so on...
Now I want to insert a new entry, if 'com.android.package' don't exist and if it exists, I want to do nothing...
I've tried following:
INSERT INTO Packages (PackageID) VALUES ('com.android.package') ON DUPLICATE KEY UPDATE PackageID=VALUES(PackageID)
But it still creates new entries, like that:
| ID | PackageID | PackageDesc |
| 3 | com.android.package | |
| 4 | com.android.package | |
| 5 | com.android.package | |
I don't know where's my error.
A proposed Packages table schema for ON DUPLICATE KEY UPDATE to work as expected is the following:
CREATE TABLE Packages (
`ID` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`PackageID` VARCHAR(30) NOT NULL,
`PackageDesc` VARCHAR (100),
UNIQUE(`PackageID`)
);
After this statement is executed:
INSERT INTO Packages (PackageID, PackageDesc) VALUES
('com.android.package', 'This is a package')
ON DUPLICATE KEY UPDATE PackageID=VALUES(PackageID);
Packages contains the following row:
# ID, PackageID, PackageDesc
'1', 'com.android.package', 'This is a package'
If you now execute the following statement:
INSERT INTO Packages (PackageID, PackageDesc) VALUES
('com.android.package', 'This is a package2')
ON DUPLICATE KEY UPDATE PackageID=VALUES(PackageID), PackageDesc=VALUES(PackageDesc);
Packages contains:
# ID, PackageID, PackageDesc
'1', 'com.android.package', 'This is a package2'
This means an UPDATE was performed by the latter INSERT INTO.
Demo here
On a simple table insert MYSQL constnatly fails on the 581st row and throws the following error:
INSERT interface (interface,status,description,deviceID) VALUES("Et34-10G","down","nodesc","1977")
INSERT interface (interface,status,description,deviceID) VALUES("Et35-10G","down","nodesc","1977")
INSERT interface (interface,status,description,deviceID) VALUES("Et36-10G","down","nodesc","1977")
INSERT interface (interface,status,description,deviceID) VALUES("Et37-10G","up","chic-02","1977")
INSERT interface (interface,status,description,deviceID) VALUES("Et38-10G","up","chic-06","1977")
INSERT interface (interface,status,description,deviceID) VALUES("Et39-10G","up","chic-07","1977")
DBD::mysql::db do failed: Cannot add or update a child row: a foreign key constraint fails (`dcss`.`interface`, CONSTRAINT `deviceID` FOREIGN KEY (`id`) REFERENCES `device` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) at ./import_intstate_into_dcssdb.pl line 27.
It always fails on inserting the 581th row.
| 576 | Et34-10G | down | nodesc | 1977 |
| 577 | Et35-10G | down | nodesc | 1977 |
| 578 | Et36-10G | down | nodesc | 1977 |
| 579 | Et37-10G | up | chic-02 | 1977 |
| 580 | Et38-10G | up | chic-06 | 1977 |
+-----+--------------+--------+-------------------------------------------------------------------------
580 rows in set (0.00 sec)
I cannot understand why it was able to insert the previous rows with the same deviceID (1977) but throws at error consistently on the 581st row.
I have tested this and gotten the same results piping in the raw SQL statements.
-bash-4.1$ mysql -u root -p dcss < test.sql
Enter password:
ERROR 1452 (23000) at line 581: Cannot add or update a child row: a foreign key constraint fails (`dcss`.`interface`, CONSTRAINT `deviceID` FOREIGN KEY (`id`) REFERENCES `device` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
I am at a loss to explain this. The parent table has this id in it so there is not reason it should be failing on a constraint issue.
mysql> SELECT * FROM device WHERE id=1977;
+------+-------------------------+---------------+------------------+--------------+--------+--------+--------------+---------------+------+-------+
| id | fqdn | ipv4addr | ipv6addr | datacenterID | typeID | make | model | serial_number | os | shell |
+------+-------------------------+---------------+------------------+--------------+--------+--------+--------------+---------------+------+-------+
| 1977 | ls03.router | 10.10.255.210 | unk | 1 | 2 | ARISTA | DCS-7050S-64 | 1234567 | EOS | unk |
+------+-------------------------+---------------+------------------+--------------+--------+--------+--------------+---------------+------+-------+
1 row in set (0.00 sec)
Mate, can you look closer at the data that you try to insert as row 581?
I guess there is some special symbol there that is preventing the correct way to cast the string "1977" to integer 1977.
Try to insert it like that:
INSERT interface (interface,status,description,deviceID) VALUES("Et39-10G","up","chic-07", 1977);
Tell me how this is working for you.
I am trying to reflect/mirror data from a Postgres table to a MySQL table. I found mysql_fwd and after connection everything to a localhost, I cannot enter data into the postgres FOREIGN TABLE. nor does data entered into MySQL, show up in postgres (backwards test)??
Data from MySQL does not reflect in postgres and if I test the other way (the correct direction) post_sync=# INSERT INTO mysql_sync (user_name, user_id) VALUES ('Joe', 3); ERROR: cannot change foreign table "mysql_sync"
*********** build process **************
CREATE SERVER mysql_svr
FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (address '127.0.0.1', port '3306');
CREATE FOREIGN TABLE mysql_sync (id float4, user_name text, user_id integer)
SERVER mysql_svr OPTIONS ( database 'postgres_link', query 'SELECT * FROM mysql_sync');
CREATE USER MAPPING FOR mysqlLink SERVER mysql_svr OPTIONS (username 'some_dude', pass '');
***************** structure ************
Foreign table "public.mysql_sync"
Column | Type | Modifiers | FDW Options | Storage | Stats target | Description
-----------+---------+-----------+-------------+----------+--------------+-------------
id | real | | | plain | |
user_name | text | | | extended | |
user_id | integer | | | plain | |
Server: mysql_svr
FDW Options: (database 'postgres_link', query 'SELECT * FROM mysql_sync')
Has OIDs: no
******************** mySQL structure *************
mysql> desc mysql_sync;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 92
Current database: postgres_link
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_name | varchar(25) | NO | | NULL | |
| user_id | varchar(10) | NO | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
A whole bunch of different methods to convert, here -> http://wiki.postgresql.org/wiki/Converting_from_other_Databases_to_PostgreSQL