How to initialize date FIELD IN MYSQL - mysql

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!

Related

When i Create table with Month he only given 0000-00-00

I create table for hour
CREATE TABLE hour (
Name1 varchar(25) not null,
Datee Datetime not null DEFAULT (CURRENT_TIMESTAMP()),
Monthh date not null DEFAULT (MONTH(CURRENT_DATE()))
);
Mysql only give me 0000-00-00 not Name where i use button in php. What is wrong with this and how correct? In my opinion my phpadmin dont have a MONTH function
CREATE TABLE hour ( Name1 varchar(25) not null,
Datee DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
Monthh TINYINT NOT NULL DEFAULT (MONTH(CURRENT_TIMESTAMP))
);
fiddle
PS. HOUR is Reserved word - I'd recommend to rename a table.
Store full timestamp and it's month part in separate columns in same table is absolutely redundant. You can story only datetime and get it's month in select query:
CREATE TABLE Godzina (
Nazwa varchar(25) not null,
Randka Datetime not null DEFAULT CURRENT_TIMESTAMP()
);
INSERT INTO Godzina (Nazwa) VALUES ('TestName');
SELECT
Nazwa,
Randka,
MONTH(Randka) AS Miesiac
FROM Godzina;
Test it on SQLize.online

Type date default sysdate in Mysql

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
)

MySQL Error "There can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT clause" even though I'm doing nothing wrong

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

mysql calculate duration from two dates

Hi I have a log database table in mysql which captures a start date and time and an end date and time.
The start and now the stop time is inserted to the record.
I have a third field which is duration which I would like displayed as hh:mm:ss
The schema looks like this at present
CREATE TABLE IF NOT EXISTS `log` (
`uid` int(20) NOT NULL AUTO_INCREMENT,
`room` int(11) NOT NULL,
`start` datetime NOT NULL,
`stop` datetime NOT NULL,
`duration` datetime DEFAULT NULL,
`participants` int(3) NOT NULL,
`recorded` varchar(3) NOT NULL,
`rec_file` varchar(35) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Log File' AUTO_INCREMENT=106 ;
I'm trying to create a trigger which calculates the duration and writes it to the duration field as data is inserted
Currently the trigger I am trying to debug looks like this
BEGIN
SET NEW.duration = (TIMEDIFF (NEW.start,NEW.stop)) ;
END
The result is the duration field remains set to 0000-00-00 00:00:00.000000
Any suggestions on how to make this work are greatfully recived
Try to add the following :
Change duration type to TIME instead of DATETIME. According to documentation, the result returned by TIMEDIFF() is limited to the range allowed for TIME values.
This is the reason you are receiving all zeros currently.
I assume that stop time will always be later than start time, so I would write the trigger in the following way:
CREATE TRIGGER your_schema.insert_duration BEFORE INSERT ON your_schema.log
FOR EACH ROW SET NEW.duration = TIMEDIFF(NEW.stop, NEW.start);
The result will be in HH:MM:SS format for duration field.

Convert from SQLite to 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 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,
...