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);
Related
I created a table now I want to add date time column but I want to current date time of each row insertion.
I have written syntax like this:
ALTER TABLE particle_photon ADD COLUMN dt_created NOT NULL default CURRENT_TIMESTAMP AFTER humidity;
but I get error:
ERROR 1064 (42000): 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 'NOT NULL default CURRENT_TIMESTAMP AFTER humidity' at line 1
You are missing the declaration of the datatype (I assume TIMESTAMP):
ALTER TABLE particle_photon
ADD COLUMN dt_created TIMESTAMP
NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER humidity;
Demo on DB Fiddle:
CREATE TABLE particle_photon(id INT PRIMARY KEY, humidity INT, lastcol INT);
ALTER TABLE particle_photon
ADD COLUMN dt_created TIMESTAMP
NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER humidity;
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.
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
);
I' m trying to add a timestamp column to this table:
CREATE TABLE `task` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`timecreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`session` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Then I try to execute this code to add another timestamp column with a different default value:
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP)
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 'TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP) AFTER `timecreated`,'
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT (CURRENT_TIMESTAMP + INTERVAL 15 MINUTE) AFTER `timecreated`'
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 '(CURRENT_TIMESTAMP + INTERVAL 15 MINUTE) AFTER `timecreated`,
but non of theese works.
You cannot have two timestamp columns with default values that use CURRENT_TIMESTAMP, however you can use a trigger before insert:
ALTER TABLE task
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00';
DELIMITER //
CREATE TRIGGER task_timeexpiration_default BEFORE INSERT ON task FOR EACH ROW
BEGIN
SET NEW.`timeexpiration` = TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP);
END;//
MySQL versions before 5.6.1 would not let two TIMESTAMP columns in the same table, unless as you rightly noted with out defaults and allowing null.
MySQL 5.6.+ allows two or more TIMESTAMP columns in a table.
How Aman said, it is not possible to use two Timestamp in same table. You could use DATETIME instead. Try something like this,
ALTER TABLE task ADD COLUMN `timeexpiration` DATETIME DEFAULT NULL AFTER timecreated;
Maybe this help.
Hint: you have to remove , end of last field after PRIMARY KEY (id), .
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.