I have a pipe delimited file with some NULL date fields. I load this data into a temp table with default values for NULL dates. In my case, I wish to default to 1950-01-01. However, these defaults are seemingly being overwritten and 0000-00-00 is being inserted. Here is a quick bash script I wrote to test:
#!/bin/bash
CMD="CREATE TEMPORARY TABLE IF NOT EXISTS ar_tmp LIKE ar; LOAD DATA INFILE '/some/path/ar.txt' INTO TABLE ar_tmp FIELDS TERMINATED BY '|'; SELECT * FROM ar_tmp;"
mysql -uuser -ppassword db -e "$CMD" > /opt/rxdb/tmp.log
I have grepped my source file and confirmed the expected NULL dates have \N and no where in the file is the pattern 0000-00-00.
Here is the structure of ar_tmp:
Field Type Null Key Default Extra
customer_code int(11) NO PRI 0
activity_date date NO PRI 1950-01-01
delivery_date date NO PRI 1950-01-01
sequence int(11) NO PRI 0
type varchar(10) YES NULL
due_date date YES 1950-01-01
original_charge float YES NULL
prior_alloc float YES NULL
current_alloc float YES NULL
future_alloc float YES NULL
open_balance float YES NULL
shipto_customer int(11) NO MUL 0
reference varchar(10) YES NULL
journal_ref_number int(11) YES NULL
next_month_flag int(11) YES NULL
journal_type varchar(15) YES NULL
activity_num int(11) YES NULL
document_path varchar(100) YES NULL
conditional_eft_approved char(1) NO N
customer_name varchar(30) YES NULL
allow_web_payment char(1) YES NULL
modify_date datetime NO 1950-01-01 12:00:00
The defaults being over road are activity_date, delivery_date and due_date. Can someone explain what is going on?
EDIT:
Three lines from the source file:
5830|2012-02-21|\N|29543|PAYMENT|\N|-600.5|0|0|0|-600.5|5830|505700|4807|0|Cash|0||N|TIME WARNE|N|2012-03-09 07:07:11
8057|2012-03-08|\N|32523|PAYMENT|\N|-1348.74|0|-1348.74|0|0|8057|1486|22|0|Cash|0||N|PACIFIC HA|N|2012-03-09 07:07:11
8103|2012-03-07|\N|32138|PAYMENT|\N|-1382.29|0|-1382.29|0|0|8103|3719|4|0|Cash|0||N|NORTH COUN|N|2012-03-09 07:07:11
Corresponding resultant records:
5830 2012-02-21 0000-00-00 29543 PAYMENT NULL -600.5 0 0 0 -600.5 5830 505700 4807 0 Cash 0 N TIME WARNE N 2012-03-09 07:07:11
8057 2012-03-08 0000-00-00 32523 PAYMENT NULL -1348.74 0 -1348.74 0 0 8057 1486 22 0 Cash 0 N PACIFIC HA N 2012-03-09 07:07:11
8103 2012-03-07 0000-00-00 32138 PAYMENT NULL -1382.29 0 -1382.29 0 0 8103 3719 4 0 Cash 0 N NORTH COUN N 2012-03-09 07:07:11
If
\Nis being transformed into an empty string, then theINSERTcommand will use it, and the DEFAULT value will be ignored.I mean with this:
then
datewill contain0000-00-00. For the default value to be used, nothing should be passed for the date at all: