I made a table which name is 'test' in mysql in my PC like belows.
create table test(
telnum varchar(20) not null,
reg_date datetime not null default '0000-00-00 00:00:00',
remarks text,
primary key(telnum)
);
And I uploaded a file named 1.txt into table 'test'.
1.txt's contents are like belows :
01011112222
01022223333
01033334444
And 'load data infile' syntax are like belows :
load data infile "c:/temp/1.txt"
ignore into table test;
But there is a problem.
Every phone numbers were cut like below.
Query OK, 3 rows affected, 3 warnings (0.00 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 3
mysql> select * from test;
+-------------+---------------------+---------+
| telnum | reg_date | remarks |
+-------------+---------------------+---------+
|12222 | 0000-00-00 00:00:00 |
|23333 | 0000-00-00 00:00:00 |
|34444 | 0000-00-00 00:00:00 |
+-------------+---------------------+---------+
3 rows in set (0.00 sec)
Only 5 characters are remained from 11 characters.
6 characters disappeared.
And second problem is 'warnings'. Reason of warnings is like below.
mysql> show warnings;
+---------+------+---------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------+
| Warning | 1264 | Out of range value for column 'reg_date' at row 1 |
| Warning | 1264 | Out of range value for column 'reg_date' at row 2 |
| Warning | 1264 | Out of range value for column 'reg_date' at row 3 |
+---------+------+---------------------------------------------------+
3 rows in set (0.00 sec)
I did'nt put anything into the reg_date field.
But the message says out of range value for column 'reg_date'.
What are the reasons and how can I solve these problems?
Try to change the line delimiter. On Windows it's usually \r\n rather then \n which is default if you omit LINES TERMINATED BY clause
LOAD DATA INFILE "/tmp/1.txt"
IGNORE INTO TABLE test
LINES TERMINATED BY '\n'
Related
Data inside column "ward_code" can not be seen with command while in web server view they can be seen.
The above data were imported to the database with below command
MariaDB [samaritan]> LOAD DATA LOCAL INFILE "C:////kino.txt"
-> INTO TABLE ward
-> COLUMNS TERMINATED BY "\t";
Query OK, 21 rows affected, 6 warnings (0.107 sec)
Records: 21 Deleted: 0 Skipped: 0 Warnings: 6
MariaDB [samaritan]>
I am just curious why in the terminal the data inside ward_code is not visible? Sorry for my English, Thanks In Advance.
After adding SHOW WARNINGS; immediately after LOAD DATA, I got the below
MariaDB [samaritan]> LOAD DATA LOCAL INFILE "C:///kino.txt"
-> INTO TABLE ward
-> COLUMNS TERMINATED BY "\t";
Query OK, 21 rows affected, 6 warnings (0.083 sec)
Records: 21 Deleted: 0 Skipped: 0 Warnings: 6
MariaDB [samaritan]> show warnings;
+---------+------+-------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------+
| Warning | 1265 | Data truncated for column 'ward_name' at row 7 |
| Warning | 1265 | Data truncated for column 'ward_name' at row 17 |
| Warning | 1261 | Row 21 doesn't contain data for all columns |
| Warning | 1261 | Row 21 doesn't contain data for all columns |
| Warning | 1261 | Row 21 doesn't contain data for all columns |
| Warning | 1261 | Row 21 doesn't contain data for all columns |
+---------+------+-------------------------------------------------+
6 rows in set (0.000 sec)
MariaDB [samaritan]>
Recently updated to mysql Ver 8.0.19 for osx10.15 on x86_64 (Homebrew)
I am trying to load data from a tsv file to add entries to an existing table. Everything seems to be working fine, except the values which are "NULL" (as in it literally says that, it's not empty) are being converted to 0000-00-00 00:00:00. There are no errors but I do get the warning"Data truncated for column 'beginexp' at row 2".
Here's what I've tried, to no avail:
ALTER TABLE infomegatask
ALTER beginexp SET DEFAULT NULL; # same for field 'endhit'
ALTER TABLE infomegatask MODIFY COLUMN beginexp DATETIME NULL; #even though it was already nullable to begin with when I checked the schema
#also set sql_mode="NO_ZERO_DATE"
Here is a redacted snippet of what the tsv looks like (sensitive data has been obscured):
uniqueid assignmentid workerid hitid ipaddress browser platform language cond counterbalance codeversion beginhit beginexp endhit bonus status mode datastring
XXXXX:YYYYY ZZZZZ AAAAA BBBBB CCCCC chrome windows UNKNOWN 0 0 4.2 2020-04-22 16:32:24 NULL NULL 0 4 live
mysql> LOAD DATA LOCAL INFILE 'test/DB/tsv/infomegatask_round_9g.tsv' INTO TABLE infomegatask FIELDS TERMINATED BY '\t' ENCLOSED BY '' LINES TERMINATED BY '\n' IGNORE 1 LINES;
Query OK, 24 rows affected, 13 warnings (0.21 sec)
Records: 24 Deleted: 0 Skipped: 0 Warnings: 13
mysql> warnings
Show warnings enabled.
mysql> show warnings
-> ;
+---------+------+------------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------------+
| Warning | 1265 | Data truncated for column 'beginexp' at row 2 |
| Warning | 1265 | Data truncated for column 'endhit' at row 2 |
| Warning | 1265 | Data truncated for column 'beginexp' at row 4 |
| Warning | 1265 | Data truncated for column 'endhit' at row 4 |
| Warning | 1265 | Data truncated for column 'beginexp' at row 7 |
| Warning | 1265 | Data truncated for column 'endhit' at row 7 |
| Warning | 1265 | Data truncated for column 'beginexp' at row 9 |
| Warning | 1265 | Data truncated for column 'endhit' at row 9 |
| Warning | 1265 | Data truncated for column 'beginexp' at row 10 |
| Warning | 1265 | Data truncated for column 'endhit' at row 10 |
| Warning | 1265 | Data truncated for column 'endhit' at row 14 |
| Warning | 1265 | Data truncated for column 'beginexp' at row 20 |
| Warning | 1265 | Data truncated for column 'endhit' at row 20 |
+---------+------+------------------------------------------------+
13 rows in set (0.00 sec)
I mean the workaround I'm going with for now is to just update these zero datetimes to NULL, but it's a bit annoying and I would just like to figure out what is going wrong.
In the LOAD DATA syntax you can convert columns.
So in your case:
LOAD DATA
INTO TABLE xx ( .., #beginexp , ... )
...
SET beginexp = IF(#beginexp='NULL', NULL, #beginexp),
...
Create sample database and table to use with.
create database `test`;
use `test`;
create table `test` (`value` float(10,2) null);
insert into test (value) values (null);
select value from test;
+-------+
| value |
+-------+
| NULL |
+-------+
1 row in set (0.00 sec)
Now i want to create a txt file ,when you load it ,null data can be loaded into the test table.
delete from test;
Here is my data file named test.txt showed in vim(:set list).
Load the file into table.
LOAD DATA LOCAL INFILE "test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n' \w;
select value from test;
+-------+
| value |
+-------+
| 0.00 |
| 0.00 |
| 0.00 |
| 0.00 |
| 0.00 |
+-------+
How to load null data for value field with file in my case?
How to create this kind of data file,the value field is null after you load it?
The runtime environment:win10+mysql-5.7.
select version();
+-----------+
| version() |
+-----------+
| 5.7.22 |
+-----------+
1 row in set (0.03 sec)
Show test.txt file in vim with set list.
\N$
\N^I$
case:without set
Load it without (value) SET value = (CASE WHEN #value IS NULL THEN NULL ELSE #value END).
LOAD DATA LOCAL INFILE "f:/test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n' \W;
OUTPUT:
Warning (Code 1265): Data truncated for column 'value' at row 1
Warning (Code 1262): Row 2 was truncated; it contained more data than there were input columns
select * from test;
+-------+
| value |
+-------+
| 0.00 |
| NULL |
+-------+
case:with set
Load it with (value) SET value = (CASE WHEN #value IS NULL THEN NULL ELSE #value END).
delete from test;
LOAD DATA LOCAL INFILE "f:/test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
(value)SET value = (CASE WHEN #value IS NULL THEN NULL ELSE #value END) \W;
OUTPUT:
Warning (Code 1265): Data truncated for column 'value' at row 1
Warning (Code 1262): Row 2 was truncated; it contained more data than there were input columns
select * from test;
+-------+
| value |
+-------+
| NULL |
| NULL |
+-------+
Thank #Schwern.
LOAD DATA LOCAL INFILE "f:/test.txt"
INTO TABLE test
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\r\n' \W;
Either works fine with no warnings.
You can use \N.
The problem is all your input lines have an extra empty field. Tab terminates the field, like a comma. You've used it as if it encloses it like quotes. For example, your first line ^Inull^I is three fields. The first is empty. The second contains null. And the third is empty.
MySQL should have said something like
Query OK, 5 rows affected, 8 warnings (0.01 sec)
Records: 5 Deleted: 0 Skipped: 0 Warnings: 8
You can read those warnings with show warnings.
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1265 | Data truncated for column 'value' at row 1 |
| Warning | 1262 | Row 1 was truncated; it contained more data than there were input columns |
| Warning | 1265 | Data truncated for column 'value' at row 2 |
| Warning | 1262 | Row 2 was truncated; it contained more data than there were input columns |
| Warning | 1265 | Data truncated for column 'value' at row 3 |
| Warning | 1265 | Data truncated for column 'value' at row 4 |
| Warning | 1262 | Row 4 was truncated; it contained more data than there were input columns |
| Warning | 1265 | Data truncated for column 'value' at row 5 |
+---------+------+---------------------------------------------------------------------------+
All you need is this:
\N$
Note that lines terminated by is sensitive to Unix vs Windows newlines. From the docs...
If you have generated the text file on a Windows system, you might have to use LINES TERMINATED BY '\r\n' to read the file properly, because Windows programs typically use two characters as a line terminator.
This explains the warnings you are getting.
MySQL documentation says that since 5.0, varchar lengths refer to character units, not bytes. However, I recently came across an issue where I was getting truncated data warnings when inserting values that should have fit into the varchar column it was designated.
I replicated this issue with a simple table in v5.1
mysql> show create table test\G
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`string` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
I then inserted multiple 10 characters values with differing amounts of UTF8 characters
mysql> insert into test (string) values
-> ('abcdefghij'),
-> ('ãáéíçãáéíç'),
-> ('ãáéíç67890'),
-> ('éíç4567890'),
-> ('íç34567890');
Query OK, 5 rows affected, 4 warnings (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 4
mysql> show warnings;
+---------+------+---------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------+
| Warning | 1265 | Data truncated for column 'string' at row 2 |
| Warning | 1265 | Data truncated for column 'string' at row 3 |
| Warning | 1265 | Data truncated for column 'string' at row 4 |
| Warning | 1265 | Data truncated for column 'string' at row 5 |
+---------+------+---------------------------------------------+
mysql> select * from test;
+------------+
| string |
+------------+
| abcdefghij |
| ãáéíç |
| ãáéíç |
| éíç4567 |
| íç345678 |
+------------+
5 rows in set (0.00 sec)
I think that this shows that the varchar size is still defined in bytes or at least, is not accurate in character units.
The question is, am I understanding the documentation correctly and is this a bug? Or am I misinterpreting the documentation?
It's true that VARCHAR and CHAR sizes are considered in characters, not bytes.
I was able to recreate your issue when I set my connection character set to latin1 (single byte).
Ensure that you set your connection character set to UTF8 prior to running the insertion query with the following command:
SET NAMES utf8
If you don't do this, a two-byte UTF8 character will get sent as two single-byte characters.
You might consider changing your default client character set.
Hey guys I created a database column in my regular LAMP stack that seems to work great, the trouble is when migrating this into CPanel, it seems that my Default values in enum revert to ' ' or whitespace?
the command I used to create this column was
`status` ENUM('0','1','2') NOT NULL DEFAULT '0',
But it seems this doesn't actually happen.....
Is there an error in my syntax? A stupidity of CPanel?
What's going on here?
EDIT
It looks like it has something to do with the input button
submitting a blank value? Anyone heard of this before?
MariaDB [test]> create table settest(attrib set('bold','italic','underline') DEF
AULT 'bold',color enum('red','green','blue') DEFAULT 'blue');
MariaDB [test]> INSERT INTO settest VALUES('a','s');
Query OK, 1 row affected, 2 warnings (0.14 sec)
MariaDB [test]> SHOW WARNINGS;
+---------+------+---------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------+
| Warning | 1265 | Data truncated for column 'attrib' at row 1 |
| Warning | 1265 | Data truncated for column 'color' at row 1 |
+---------+------+---------------------------------------------+
2 rows in set (0.00 sec)
MariaDB [test]> SELECT * FROM settest;
+--------+-------+
| attrib | color |
+--------+-------+
| | |
| | |
+--------+-------+
Looks like the answer to get a default is NOT NULL DEFAULT 1 as per 1.3. ENUM