In MariaDB 5.5 and before there could only be one TIMESTAMP column per
table that had CURRENT_TIMESTAMP defined as its default value. This
limit has no longer applied since MariaDB 10.0.
Source: https://mariadb.com/kb/en/mariadb/timestamp/
I have the following table :
CREATE TABLE oauth_client_details (
client_id VARCHAR(255) PRIMARY KEY,
resource_ids VARCHAR(255),
client_secret VARCHAR(255),
scope VARCHAR(255),
authorized_grant_types VARCHAR(255),
web_server_redirect_uri VARCHAR(255),
authorities VARCHAR(255),
access_token_validity INTEGER,
refresh_token_validity INTEGER,
additional_information VARCHAR(4096),
autoapprove VARCHAR(255),
version bigint(20) UNSIGNED DEFAULT '1',
creation_datetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modification_datetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
active tinyint(1) UNSIGNED DEFAULT '1'
);
I need:
the creation datetime to be filled with current timestamp on insertion
the modification datetime to be updated with current timestamp on update.
I did an insert and here are the values I got:
creation_datetime : '2016-12-11 20:03:24'
modification_datetime : '2016-12-11 13:03:24'
I was expecting these values to be the same, so why is it now different ?
I would like to store only UTC time and translated it to user locale
at the last moment.
My Spring server use UTC time.
MariaDB :
SELECT ##global.time_zone, ##global.system_time_zone;
|SYSTEM|UTC
It's now 20:03 in my country, so I suppose the wrong value is the modification_datetime field.
I use mariadb:10.1.17 official docker image without any modification to the image.
This is my insert statement :
==> Preparing: insert into oauth_client_details(client_id,authorized_grant_types,access_token_validity,additional_information,active,client_secret,creation_datetime,autoapprove,modification_datetime,resource_ids,web_server_redirect_uri,authorities,refresh_token_validity,scope,version) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
==> Parameters: bo(String), authorization_code,refresh_token(String), 1800(Integer), {"companyId":1,"companyName":"testing"}(String), true(Boolean), $2a$10$h/0tc3qnZj/ZfRrNXf.oiuypEyYXJAJZdHPgy2ZMc1XMAntAMPK(String), null, true(Boolean), null, testinggroup/api(String), http://localhost:8080,http://dev:31735(String), ROLE_CLIENT(String), 3600(Integer), read,write,trust(String), 1(Long)
Does anyone ever had this ?
Edit: I have updated my docker Mariadb from version 10.1.17 to 10.1.19 the latest in date and I still have the same bug.
It appears dates are different because I left in the code a creationDateTime = new DateTime(); in the Model.
It means the server was using DateTime() for creationDateTime and database UTC.
Related
I have seen this question before but it is not answered, so here it goes:
When creating a table, I have a column with the type Date. I want that the default value is the system date, (Sysdate), but for some reason it does not work and it gives an error (in syntax, which is strange because I am following the Mysql syntax).
create table students(
id integer(10),
name varchar (21) NOT NULL,
surname varchar(30),
grade integer check(grade in(1,2,3),
enrollment date default sysdate,
primary key(id) );
And it gives an error in syntax just at the "sysdate". I have tried with sys_date, sys-date, and it is the same.
Instead of sysdate, try CURRENT_TIMESTAMP like:
CREATE TABLE foo (
creation_time DATETIME DEFAULT CURRENT_TIMESTAMP,
modification_time DATETIME ON UPDATE CURRENT_TIMESTAMP
)
I am trying to create a table using the script down below and getting the error
Error Code: 1067. Invalid default value for 'LAST_MODIFIED_TS.
In my understanding after 5.6 you could create more than one timestamp column also it is not necessary to provide default values . Another thing is it is not barfing at Created_TS which is just one line before it.
Also the same script works on windows but not on linux ubuntu , the version of mysql running on both of them is 5.7
CREATE TABLE testdb.test (
ID BIGINT NOT NULL AUTO_INCREMENT,
DESCRIPTION VARCHAR(300) NOT NULL,
CREATED_TS TIMESTAMP NOT NULL,
LAST_MODIFIED_TS TIMESTAMP NOT NULL,
PROPERTY_TYPE VARCHAR(1) DEFAULT 'S',
last_modified timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT test_pk PRIMARY KEY
(ID) ) ENGINE=InnoDB ;
Make sure you don't have NO_ZERO_DATE sql_mode variable set (http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_zero_date) by running the following query:
show variables like 'sql_mode';
CREATE TABLE AlarmHistory
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
value DOUBLE NOT NULL,
startedStamp TIMESTAMP NOT NULL,
finishedStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
);
When trying to create the above table I get the following error: "SQL Error (1293): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause".
My question is this a bug? Because sure, I have two TIMESTAMP columns, but only ONE of them have a default definition. When I remove startedStamp I have no errors.
Per the MySQL manual, version 5.5, Automatic Initialization and Updating for TIMESTAMP
With neither DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP, it is the same as specifying both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP.
CREATE TABLE t1 (
ts TIMESTAMP
);
However,
With a constant, the default is the given value. In this case, the column has no automatic properties at all.
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT 0
);
So, this should work:
CREATE TABLE AlarmHistory
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
value DOUBLE NOT NULL,
startedStamp TIMESTAMP DEFAULT 0 NOT NULL,
finishedStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
fiddle
This is the limitation in MYSQL 5.5 version. You need to update the version to 5.6.
I was getting this error in adding a table in MYSQL
Incorrect table definition; there can be only one TIMESTAMP column
with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause My new MYSQL
table looks something like this.
create table table_name (col1 int(5) auto_increment primary key, col2
varchar(300), col3 varchar(500), col4 int(3), col5 tinyint(2),
col6 timestamp default current_timestamp, col7 timestamp default
current_timestamp on update current_timestamp, col8 tinyint(1)
default 0, col9 tinyint(1) default 1);
After some time of reading about changes in different MYSQL versions and some of the googling. I found out that there was some changes that were made in MYSQL version 5.6 over version 5.5.
This article will help you to resolve the issue.
http://www.oyewiki.com/MYSQL/Incorrect-table-definition-there-can-be-only-one-timestamp-column
I Wanted to initialize default date field in my table with CURRENT_DATE(); well BENCH gives me errors that is not possible i heard about triggers but it seems to be a little over complicatet for that problem so is there any way to make this in such way
CREATE TABLE Sprzedaz (
Id int unsigned primary key auto_increment,
KlientId int not null,
ProduktNumer int not null,
Ilosc int not null,
Cena float not null,
Data date default CURRENT_DATE(),
check (Data >= now()),
....
);
You can initialize a TIMESTAMP column with this:
Data TIMESTAMP DEFAULT CURRENT_TIMESTAMP
or a DATETIME column (MySQL 5.6+):
Data DATETIME DEFAULT CURRENT_TIMESTAMP
but if you want to initialize a DATE column using MySQL 5.5 you need an INSERT TRIGGER:
CREATE TRIGGER setdate_before_insert BEFORE INSERT ON test
FOR EACH ROW
SET NEW.Data = CURDATE();
and maybe you need also an UPDATE trigger? Please see fiddle here.
Another way to go about this, if you do not mind changing the date type, would be to use a TIMESTAMP and initialize it with TIMESTAMP DEFAULT CURRENT_TIMESTAMP. Your definition would then become:
CREATE TABLE Sprzedaz (
Id int unsigned primary key auto_increment,
KlientId int not null,
ProduktNumer int not null,
Ilosc int not null,
Cena float not null,
Data TIMESTAMP DEFAULT CURRENT_TIMESTAMP, <<<==== change this
check (Data >= now()),
....
);
If you have MySQL version 5.6.5 and above, you can use CURRENT_TIMESTAMP instead of CURRENT_DATE
See http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
So you can't do it with dates but you may be able to change your field to be a TIMESTAMP or DATETIME and Bobs your mothers brother!
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 INSERT operation is:
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™');
Now the same table I created 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 operation for MySQL is:
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 :-there can be only one TIMESTAMP column with current_timestamp in default of on update clause
…but I am not able to insert all those values into MySQL. Help me?
In SQLite you have two columns
[date] DATE DEFAULT CURRENT_DATE NULL,
[time] TIME DEFAULT CURRENT_TIME NULL,
while on MySQL you have only one
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NULL,
and you're trying to insert two values on it...
You should try
INSERT INTO tickets VALUES(..., '2010-11-30 17:46:39', ...)
At first glace, your varchar column is size 10, but you are inserting greater than length 10 data into it. Make sure your varchar column is wide enough for your data.
Your MySQL Schema appears to be incorrect for what you're trying to insert.
Excerpt from this post: Should I use field 'datetime' or 'timestamp'?
...Timestamps in MySQL generally used to track changes to records, and are updated every time the record is changed. If you want to store a specific value you should use a datetime field.
Change your MySQL schema to something closer to:
...
phone VARCHAR(12) NULL,
date DATE DEFAULT CURRENT_DATE NULL,
time TIME DEFAULT CURRENT_TIME NULL,
...