I am trying to use the INFILE statement to import data from a tab delimited file. However, one of the data types is a TIMESTAMP. I tried using NULL in the flat file, but the resulting value after executing the statement was '0000-00-00 00:00:00'. What value can I use so the TIMESTAMP will work?
your table definition should be
timestamp_column TIMESTAMP DEFAULT CURRENT_TIMESTAMP
then when inserting the timestamp_column should be time of insert.
Related
mysql is removing those rows which contains null values but I want those.
like i have a table
create table users
(
user_id int Not null primary key,
created_at datetime not null,
company_id int not null,
language varchar(20) not null,
activated_at datetime,
state varchar(20) not null
);
activated_at is the only column which contains null values.
result]1
original data]2
To successfully import your CSV file follow below steps:
First be careful on your date format on the csv file because the MySQL DATETIME is in YYYY-MM-DD hh:mm:ss format.
The DATETIME type is used for values that contain both date and time
parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD
hh:mm:ss' format. The supported range is '1000-01-01 00:00:00' to
'9999-12-31 23:59:59'. Invalid DATE, DATETIME, or TIMESTAMP values are
converted to the “zero” value of the appropriate type ('0000-00-00' or
'0000-00-00 00:00:00'), if the SQL mode permits this conversion.
Copy the data from your excel and paste to notepad++.
Your data should look like below in the notepad++:
user_id;created_at;company_id;language;activated_at;state
0;2021-01-01 10:11:05;15415;ger;2021-01-01 10:11:05;active
1;2021-01-01 10:11:05;15416;ita;;pending
2;2021-01-01 10:11:05;15417;english;2021-01-01 10:11:05;pending
3;2021-01-01 10:11:05;15418;ger;;active
Then save it to a .csv file and copy to your mysql path (usually it is /var/lib/mysql or /var/lib/mysql-files/) location.
Then run LOAD DATA INFILE into your MySQL command line.
LOAD DATA INFILE '/var/lib/mysql/users_tst.csv'
IGNORE INTO TABLE users_tst
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(user_id,created_at,company_id,language,activated_at,state);
And your data should look like:
Your activated_at empty data are in 0000-00-00 00:00:00 format, if you want null you should change table description and instead of leavit empty in your csv file fill it with NULL
Is there any way to create a table with a specific TIMESTAMP format (HH:mm:00) where I need to fix sec to 00.
CREATE TABLE `published` (
`time_pub` timestamp() NULL DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
You can't change the way the timestamp is stored. Only the way it is presented in your select statements.
Another solution would be to store the data in a hour and minute column having an integer data type.
Although you can't specify the time format when create table, you can use date_format function in MySQL to fix the second part to 00 when insert data to database.
insert into TABLE_NAME values (date_format(now(), '%H:%m:00'));
I'm importing CSV file into database using PHPMyAdmin. I have kept null for the columns that I don't want to import values right now. When I import CSV it is giving me an error: #1292 - Incorrect datetime value: '' for column 'updated_at' at row 1.
Is there a way to want to get rid of this error without importing values into 'updated_at' column.
Note: 'updated_at' is a date time column. It is created using following query.
ALTER TABLE labdata1 ADD updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
I have three *date fields in a table defined as VARCHAR(45) with a default value of '00-00-0000'. Each of the fields needs the default value of the current system time when a record is created.
I have changed one of the fields from VARCHAR(45) to TIMESTAMP with default of CURRENT_TIMESTAMP, which works great. I think I can only have a single field with Datatype TIMESTAMP per table.
How do I handle the other 2 fields in the tables? I would like to *CURRENT_TIMESTAMP them too.
Thanks
Use the NOW() function:
INSERT INTO yourtable (field1, field2) VALUES ('blahblah', now());
You can use "NOW()" in a MySQL query to fill in the current time for a timestamp column.
You can write Now() to the fields
INSERT INTO table (right_now)
VALUES (Now()):
Is it possible to define a timestamp column in a MySQL table that will automatically be updated every time a field in the same row is modified? Ideally this column should initially be set to the time a row was inserted.
Cheers,
Don
You can use the timestamp column as other posters mentioned. Here is the SQL you can use to add the column in:
ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;
This adds a column called 'lastUpdated' with a default value of the current date/time. When that record is updated (lets say 5 minutes later) that timestamp will automatically update to the current time.
That is the default functionality of the timestamp column type. However, note that the format of this type is yyyymmddhhmmss (all digits, no colons or other separation).
EDIT: The above comment about the format is only true for versions of MySQL < 4.1... Later versions format it like a DateTime
This is what I have observed (MySql 5.7.11) -
The first TIMESTAMP column in the table gets current timestamp as the default value. So, if you do an INSERT or UPDATE without supplying a value, the column will get the current timestamp.
Any subsequent TIMESTAMP columns should have a default value explicitly defined. If you have two TIMESTAMP columns and if you don't specify a default value for the second column, you will get this error while trying to create the table -
ERROR 1067 (42000): Invalid default value for 'COLUMN_NAME'
A MySQL timestamp is set with creation or update time only if their default value is set as it. ALTER TABLE some_table ADD when TIMESTAMP DEFAULT CURRENT_TIMESTAMP.
Otherwise it works just like a DateTime field, only that it's relative to 1970/01/01 UTC, so it's an absolute point in time not depending on a specific timezone as is DateTime.