Date column with DEFAULT CURDATE() throw error - mysql

CREATE TABLE tests(
id INT PRIMARY KEY AUTO_INCREMENT,
day DATE DEFAULT CURDATE());
This code gives me error message: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURDATE()
I do not understand why. It works without DEFAULT CURDATE()
Can someone explains me why?
Thanks you very much!

curdate() is date time function your assigning current date to a date variable
CREATE TABLE tests( id INT PRIMARY KEY AUTO_INCREMENT, `day` datetime DEFAULT now())
run this query

Related

Add timestamp column in mysql

SQL Statement:
ALTER TABLE `Trade`.`details`
ADD COLUMN `datetime` TIMESTAMP(3) NOT NULL DEFAULT TIMESTAMP(3) AFTER `Result`
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(3) AFTER Result' at line 2
i am new to mysql any help.Please
timestamp doesn't need length, also you are defining a default value but provided a datetype instead !!
here is what I mean
ALTER TABLE `Trade`.`details`
ADD COLUMN `datetime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `Result`

Adding new Column with current time in SQL

Im trying to add a new column to my SQL table i want the data type to be TIME and the default value to be CURRENT_TIME. This is my query.
ALTER TABLE tuesday_records ADD cur_time TIME DEFAULT CURRENT_TIME
And this is the error message i get.
Error
SQL query:
ALTER TABLE tuesday_records ADD cur_time TIME DEFAULT CURRENT_TIME
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CURRENT_TIME' at line 1
You can do what you want with generated columns:
create table t (
id int auto_increment primary key,
x int,
t timestamp default now(),
tt time generated always as (time(t))
);
That is, add a timestamp column and then extract the time.
Here is a db<>fiddle.
Although this answers your question, I'm not sure if it is the best approach to your overall problem.

SQL Syntax error when using sysdate

so i'm having an issue using sysdate function as my default data value in MySQL. my code to create the table is as follows:
CREATE TABLE orders
(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
odr_date DATE DEFAULT sysdate() NOT NULL
);
i get the error
[42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sysdate()
)' at line 4
instead of sysdate() try CURRENT_TIMESTAMP for mysql
reference: Type date default sysdate in Mysql
CREATE TABLE orders
(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
odr_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
);

Error with datetime insertion

I'm trying to insert a datetime in my table, but I can't figure why it isn't working. I'm running the following query:
DROP TABLE IF EXISTS dynamic_pricing ;
CREATE TABLE dynamic_pricing(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
time_start TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
time_stop TIMESTAMP NOT NULL,
change_ratio REAL NOT NULL
-- day_of_week INT(1)
);
INSERT INTO dynamic_pricing(id,time_start,time_stop,change_ratio)
VALUES(1,DATETIME('2000-01-01 00:00:00'),DATETIME('2000-01-01 00:00:02'),2);
I tried using this, only to get
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '('2000-01-01 00:00:00'),DATETIME('2000-01-01
00:00:02'),2)' at line 2
What's wrong with this query?
The problem is that your value is defined as DATETIME() but you could use
INSERT INTO dynamic_pricing VALUES ('2017-06-03 21:30:03' )
instead.
Given that you are defaulting two values, you don't need to insert them. I would suggest:
INSERT INTO dynamic_pricing(time_stop, change_ratio)
VALUES('2000-01-01 00:00:02', 2);
Then, the DATETIME() is unnecessary (is it even a function?). Here is a SQL Fiddle example.
INSERT INTO dynamic_pricing(id,time_start,time_stop,change_ratio)
VALUES(1,'2000-01-01 00:00:00','2000-01-01 00:00:02',2);

Wondering if mySQL tables can be named using date functions and mySQL #1064 error

Noob Alert.
Hi. I'm trying to input this event in mySQL through phpMyAdmin. I'm not sure if the table can be named using YEAR() and MONTHNAME() functions. Even before i could tackle that, this #1064 error has sprouted:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 14
Could someone please point out what i'm doing wrong?
Also is it possible to name tables the way I'm trying to?
CREATE EVENT newmonthlytable
ON SCHEDULE EVERY '1' MONTH
STARTS '2012-09-01 00:00:00'
DO
BEGIN
CREATE TABLE IF NOT EXISTS `YEAR()-MONTHNAME()-0-msgs`
(`msgid` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`sender` int(11) NOT NULL,
`reciever` int(11) NOT NULL,
`sendername` varchar(64) NOT NULL,
`msg` varchar(512) NOT NULL,
`tstamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP)
ENGINE = MyISAM, CHARACTER SET = utf8, COLLATE = utf8_bin
END;
There is syntax error, to fix it add ';' at the end of CREATE TABLE statement.
You can name tables as you want, but do you really need it? It is better to think about the db-design, and do not create tables from routines.