Mysql : Insert null timestamp into NOT NULL column - mysql

Mysql version is 5.1.73,there can be only one timestamp column with current_timestamp in deafault or on update clause,so I tried to create table:
create table `temp`(
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`created_date` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`last_updated_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
And after I executed this sql:
insert into `temp`(id,created_date, last_updated_date) values(1,null, null);
I got this document:
1 2017-02-10 18:17:16 2017-02-10 18:18:13
Here is my question:
I set created_date NOT NULL,why did the insert sql work well?Should it return Error: Column 'xx' cannot be null?
Why did created_date column get a value and it is not the default value?
Thanks.

That's how the MySql's TIMESTAMP work, if you want to get an error on NULL the way to go is to use DATETIME instead.
So if you create the following table
create table `temp`(
`id` INTEGER NOT NULL auto_increment PRIMARY KEY,
`created_date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
`last_updated_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
And then execute the below query you'll get the error.
insert into `temp`(id,created_date, last_updated_date) values(1,null, null);
Now you'll start getting this error #1048 - Column 'created_date' cannot be null.

Related

Retrieve varchar autogenerated primary key through triggers using JDBCTemplate

I am using MySQL Database and Spring JdbcTemplates to access the DB. I have a table called logs which has the following fields:-
`id` varchar(100) NOT NULL DEFAULT '11111111111111111',
`user_id` varchar(100) NOT NULL,
`service_id` int(11) NOT NULL,
`start_time` timestamp NOT NULL,
`creation_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`modification_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
I have a trigger on top of it:-
CREATE TRIGGER trigger_logs_insert BEFORE INSERT ON logs
FOR EACH ROW
SET NEW.creation_time = UTC_TIMESTAMP(), NEW.modification_time = UTC_TIMESTAMP(), NEW.id = (SELECT UUID());
I want the Spring jdbcTemplate code which retrieves the Id generated by the Trigger on the insert of a record.
I tried KeyHolder(GeneratedKeyHolder) but that is meant for numbers.
I want a code that can instantaneously retrieve the id.

MySql table have two or more columns have default date vaule CURRENT_TIMESTAMP,any Solutions?

if i have a table with two columns create_time and update_time,the data type is timestamp,then have default value CURRENT_TIMESTAMP,the sql code of created table is:
CREATE TABLE `t_activity` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`STARTDATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ENDDATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
);
but it prompt error:1293,there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or UPDATE clause.
I am not sure what you're trying to accomplish having both the STARTDATE and ENDDATE populated with the CURRENT_TIMESTAMP. However to fix your code try changing your data types to DATETIME like this:
**CREATE TABLE `t_activity` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`STARTDATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ENDDATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
);**
http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html
This limitation in Mysql:
Previously, at most one TIMESTAMP column per table could be
automatically initialized or updated to the current date and time.
This restriction has been lifted. Any TIMESTAMP column definition can
have any combination of DEFAULT CURRENT_TIMESTAMP and ON UPDATE
CURRENT_TIMESTAMP clauses. In addition, these clauses now can be used
with DATETIME column definitions. For more information, see Automatic
Initialization and Updating for TIMESTAMP and DATETIME.
Check This for more detail
http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html

What is the reason for Mysql error code 1293

Here is the message of mysql error 1293:
SQL Error (1293): Incorrect table definition; there can be only one
TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
what is the reason for mysql only allows one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause per table.
Only one TIMESTAMP field can default to "now"
I should say first of all, if you are trying to define more than one MySQL TIMESTAMP fields using CURRENT_TIMESTAMP or "default now", unfortunately that is bad, you can't do it in MySQL
I just got this MySQL TIMESTAMP error when trying to create a table like this:
create table users (
id int unsigned auto_increment not null primary key,
username varchar(50) not null unique,
password varchar(40) not null,
email_address varchar(128) not null unique,
email_sent timestamp not null,
last_login timestamp not null default now()
) ENGINE = InnoDB;
When I first solved this problem I thought MySQL required the "CURRENT_TIMESTAMP (default now)" field to be declared before any other TIMESTAMP fields, so I solved my problem like this:
create table users (
id int unsigned auto_increment not null primary key,
username varchar(50) not null unique,
password varchar(40) not null,
email_address varchar(128) not null unique,
last_login timestamp not null default now(),
email_sent timestamp not null
) ENGINE = InnoDB;

INSERT works in SQLite but not MySQL

This table I created in a SQLite database:
CREATE TABLE [tickets] (
[id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[coupon_id] INTEGER NULL,
[size] FLOAT NULL,
[phone] VARCHAR(10) NULL,
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
[product] TEXT NULL
);
Now I convert this table in mysql
CREATE TABLE tickets (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
coupon_id INTEGER NULL,
size FLOAT NULL,
phone VARCHAR(10) NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
product TEXT NULL
);
INSERT INTO "tickets" VALUES(429,9,18.16,'949-893-5032','2010-11-30','17:46:39','Kids’ Kups Berry Interesting™');
INSERT INTO "tickets" VALUES(430,9,12.04,'847-188-1359','2010-11-25','10:54:00','Raspberry Collider™');
INSERT INTO "tickets" VALUES(431,9,14.1,'204-682-5560','2010-12-08','15:34:07','Celestial Cherry High™');
When I am inserting those values I got an error ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause.
I am able to insert all those values in SQLite, but I am not able to insert all those values into MySQL. Help me?
You might want to convert
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
to
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
and concatenate the values from SQLite, or alter their type
date date DEFAULT NULL,
time time DEFAULT NULL,
and add the current date/time with a trigger.
As far as I know, MySQL will not allow this. If you want to insert the current time into the second field, consider using a trigger to update the record when inserting.
DELIMITER $$
CREATE TRIGGER curtime BEFORE INSERT ON tickets FOR EACH ROW
BEGIN
SET NEW.time = CURRENT_TIMESTAMP();
END;
$$
DELIMITER ;

I thought curtime(), now() and current_timestamp are valid default datetime values in MySql?

I'm using the latest version of MySql and I tried altering a table to use curtime() or now() or current_timestamp as the default value for a datetime column (created_at, updated_at). For all three, the alter failed, saying these are invalid default values. Any ideas what's an appropriate default value to generate the current time in a datetime field?
ALTER TABLE `music_library_development`.`albums`
MODIFY COLUMN `created_at` DATETIME NOT NULL DEFAULT current_timestamp,
MODIFY COLUMN `updated_at` DATETIME NOT NULL DEFAULT current_timestamp;
ALTER TABLE `music_library_development`.`albums`
MODIFY COLUMN `created_at` DATETIME NOT NULL DEFAULT now(),
MODIFY COLUMN `updated_at` DATETIME NOT NULL DEFAULT now();
ALTER TABLE `music_library_development`.`albums`
MODIFY COLUMN `created_at` DATETIME NOT NULL DEFAULT curtime(),
MODIFY COLUMN `updated_at` DATETIME NOT NULL DEFAULT curtime();
Try using the TIMESTAMP column type:
MODIFY COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT current_timestamp;
alter table music_library_development change created_at created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
alter table music_library_development change updated_at updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP