I noticed that I can insert multiple NULL values into a table with a UNIQUE key :
INSERT INTO table (autoincrementkey, uniquekey, id) VALUES (NULL, NULL, 0), (NULL, NULL, 1), (NULL, NULL, 2)
This works fine as I get the 3 lines inserted (and yes, the uniquekey is NULL = Yes)
However, when I try to use the Load Data from a file with the same query, it only inserts 1 NULL value, which is the first.
LOAD DATA LOCAL INFILE $file.csv INTO TABLE table FIELDS TERMINATED BY ',' ENCLOSED BY '"'
Is there any way I can insert multiple NULL values on a UNIQUE column, with the LOAD DATA INTO please ?
Thanks in advance!
=============
[UPDATE]
I noticed that uniquekey gets a 0 instead of a NULL, which is why only the 1st entry gets to the database.
In the CSV file it looks like this :
,,1
,,2
,,3
But uniquekey is set to NULL by default, I don't get why it gets 0 :
ALTER TABLE `table` CHANGE `uniquekey ` `uniquekey ` INT(10) DEFAULT NULL;
[UPDATE 2]
OK I got it!!
I thought (,,1) would insert NULL, NULL, 1.
But it's not true, it has to be (NULL,NULL,1) to get what you see.
Related
I have a CSV file with three columns of "movieId", "imdbId", "tmdbId". The column "tmdbId" contains multiple empty rows.(movieId is a froeign key referring to a primary key in another table)
When I read this data frame into R, the empty rows are treated as NA values. If I import this CSV file into mysql DB using the following command, the rows with NA values don’t get inserted in the table, even though I allow NULL values. I should also mention that, I do not get any errors.
Beside the following command, I also tried importing the dataset using MySQL workbench, but it did not work.
any suggestion?
LOAD DATA LOCAL INFILE 'links.csv' INTO TABLE links
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(movieId, imdbId, tmdbId);
I know NULL and NA values are not the same, but I do not understand why R treats empty rows as NA. I tried to replace NA with NULL, but R does not support this operation.
The TABLE
CREATE TABLE links (
movieId int NOT NULL,
imdbId int DEFAULT NULL,
tmdbId int DEFAULT NULL,
KEY movieId (movieId),
CONSTRAINT links_ibfk_1 FOREIGN KEY (movieId) REFERENCES movieId_title (movieId)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
The CSV file looks like this:
enter image description here
Here is an example of an empty row for the third column:
enter image description here
As #Alec suggested, you can do set foreign_key_checks = 0. Then, you can replace zeros with NULL using the following command:
UPDATE table_name
SET col_name= NULL
WHERE col_name = 0;
Is it possible to LOAD DATA a csv into mysql without having to add empty values for non existing columns at the end?
All my optional columns are sorted at the end of the schema:
CREATE TABLE `person` (
id int(20) NOT NULL AUTO_INCREMENT,
firstname varchar(30) NOT NULL,
lastname varchar(30) NOT NULL,
optional1 varchar DEFAULT NULL,
optional... varchar DEFAULT NULL,
optional50 varchar DEFAULT NULL,
PRIMARY KEY (`id`)
) engine=innodb AUTO_INCREMENT=0;
sample.csv:
1;john;doe
2;jabe;doe;;;opt val3;;;;;;opt val9;;;;;;...
Important: I don't want to explicit list all the columns in my LOAD DATA INFILE sql statement (I know that this would work by using a combination of IFNULL and #var).
But can't I just load into the table, telling mysql to ignore any missing fields at the end of each line?
The documentation of MySQL LOAD DATA syntax provides the following information:
By default, when no column list is provided at the end of the LOAD DATA statement, input lines are expected to contain a field for each table column. If you want to load only some of a table's columns, specify a column list.
[...]
If an input line has too few fields, the table columns for which input fields are missing are set to their default values. For numeric types, the column is set to 0.
[...]
An empty field value is interpreted different from a missing field: for string types, the column is set to the empty string.
So given your sample data:
1;john;doe
2;jabe;doe;;;opt val3;;;;;;opt val9;;;;;;...
Record with id 1 will have all optional columns set to NULL (ie their default). For id2, optional string columns will be set to the empty string. .
I cannot tell whether this would be OK for your use case or not. If you do want consistent values in the optional columns, available options would be:
input pre-processing: use SET to set to NULL columns that contains an empty string
LOAD DATA INFILE 'file.txt' INTO TABLE t1
SET
optional1 = NULLIF(optional1, ''),
optional2 = NULLIF(optional1, ''),
...
set up a BEFORE INSERT trigger on the table that sets to NULL empty values
run an update on the table after it was populated
UPDATE t1 SET optional1 = NULLIF(option1, ''), optional2 = NULLIF(optional1, '')
WHERE '' IN (optional1, optional2, ...)
I found out it works as expected if adding the IGNORE keyword to the LOAD DATA statement:
LOAD DATA INFILE 'sample.csv' IGNORE INTO TABLE persons
Thereby, I can define all my optional columns as DEFAULT NULL, and if values are missing, they are set to NULL during import.
I ahve excel sheet with the following columns
Sam/Simon Date Store Customer name Original Order Number
Simon 09/11/2014 Bristol Cr Car 20089691/ 26089697
I need to store these infomration in 2 rows in tables
Simon 09/11/2014 Bristol Cr Car 20089691/
Simon 09/11/2014 Bristol Cr Car 26089697
I need to know the table structure. Split comma separated values from one column to 2 rows in the results and exporting them from excel to mysql.
the actual table structure is as follows.
CREATE TABLE "tblOrderR" (
"intOrderRemedialId" int(10) unsigned NOT NULL AUTO_INCREMENT,
"intOrderId" int(10) unsigned NOT NULL,
"intOrderRemedialGivenPence" int(10) unsigned NOT NULL,
"intRequestedById" smallint(5) unsigned DEFAULT NULL,
"intAuthorizedById" smallint(5) unsigned DEFAULT NULL,
PRIMARY KEY ("intOrderRemedialId"),
KEY "tblOrderRemedial" ("intOrderId"),
KEY "tblOrderRemedial_ibfk_2" ("intRequestedById"),
KEY "tblOrderRemedial_ibfk_3" ("intAuthorizedById"),
CONSTRAINT "tblOrderRemedial_ibfk_1" FOREIGN KEY ("intOrderId") REFERENCES "tblOrder" ("intOrderId"),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Order Remedial Information';
One way to approach this would be:
1) Split out "Original Order Number" Excel column into two separate columns using TEXT TO COLUMNS
2) Save data in csv format
3) Load data into a staging table that looks something like this
samSimon,date,store,customerName,originalOrder1,originalOrder2
4) Run two inserts into your final table: one with originalOrder1 and a second one with originalOrder2. For example:
insert into tblOrderR (columns)
select samSimon,date,store,customerName,originalOrder1 from stagingTable;
insert into tblOrderR (columns)
select samSimon,date,store,customerName,originalOrder2 from stagingTable;
Very pseudo-code answer but hopefully you get the gist of it!
Just import two times.
First time (example code, you might have to adjust the one or the other thing):
LOAD DATA LOCAL INFILE '/path/to/file/excel.csv'
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(column1, column2, #my_variable)
SET column3 = SUBSTRING(#my_variable FROM 1 FOR LOCATE('/', #my_variable));
Second time:
LOAD DATA LOCAL INFILE '/path/to/file/excel.csv'
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
(column1, column2, #my_variable)
SET column3 = SUBSTRING(#my_variable FROM LOCATE('/', #my_variable) + 1);
Relevant manual pages:
string functions like substring() and locate()
load data infile
My problem is the following:
I have a table foo_table with id column ID as bigint (primary key), name column NAME as varchar(80) and code COLUMN code as varchar(20). I have a unique constraint on the CODE column.
If I fill this table with only "number" strings in the code column, and then one day decide to add a row with a, surprise, "string" string in that code column, say putting "My Code", it works fine ! The error comes when I tried updating one of my previous records, one that has a CODE value being a number; in this case I get the famous error saying:
ERROR 1292 (22007): Truncated incorrect DOUBLE value: 'My Code'.
I'm not even touching that record ! I presume MySQL is trying to check the unique constraint and try to convert every value in the CODE column to double, but that's insane, and very unprofessional.
Anyone has a solution ? Like turning off string<->number conversion in MySQL ?
Thanks in advance.
Edit:
Ok I have the sample code, but while creating it I saw how to fix the problem, which is still a problem in MySQL as far as I'm concerned:
CREATE TABLE foo_table (
ID bigint(20) NOT NULL DEFAULT '0',
NAME varchar(80) DEFAULT NULL,
CODE varchar(20) NOT NULL DEFAULT '',
UNIQUE KEY foo_table_uk (CODE)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
insert into foo_table (id, name, code) values (1, 'one', '1');
insert into foo_table (id, name, code) values (2, 'two', '2');
insert into foo_table (id, name, code) values (3, 'three', 'three');
update foo_table set name='a-one', code='1' where code=1; <-- This update throws the error
update foo_table set name='a-one', code='1' where code='1'; <-- this update works fine
I have a php form with three text boxes (webmeasurementsuiteId, webmeasurementsId, Id) and the values in the text boxes are retrieved from other tables of the database. Now my task is to submit the retrieved values in this php form named (mapping) to the database. I have created the table with the following syntax:
CREATE TABLE `mapping` (
`webmeasurementsuiteId` INT NOT NULL,
`webmeasurementsId` INT NOT NULL,
`Id` INT NOT NULL,
PRIMARY KEY (Id)
);
But I am getting an sql error as follows:
INSERT INTO mapping(webmeasurementsuiteId,webmeasurementsId,Id) values ('','','7')
ERROR: Incorrect integer value: '' for column 'webmeasurementsuiteId' at row 1
Can anyone correct my error?
all your columns are INT that means numbers while your insert statement is inserting STRINGs (text) remove the ' around the values in the INSERT-statement and it should work
example:
INSERT INTO mapping(webmeasurementsuiteId,webmeasurementsId,Id) values (0,0,7)
So you used '' for webmeasurementsuiteId which indicates it as a string. just leave the integers to 0 without the parantheses to indicate them as an integer. values (0,0,7) should probably do it.
you cannot insert blank in the values field .
try 0 instead of ''
INSERT INTO mapping(webmeasurementsuiteId,webmeasurementsId,Id) values (0,0,'7')
or alter the columns to take the null values