MySql tables, creating new - mysql

it seems like my syntax for the following command is incorrect. The error message points to the default current timestamp. Can someone point out where am I going wrong? Your help is much appreciated as I'm working on my first app. My code is the following:
CREATE TABLE ingredients (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
ingredient VARCHAR(100),
created_at DEFAULT CURRENT_TIMESTAMP,
updated_at DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

You forgot the Data Type ;)
The correct Query is following:
CREATE TABLE ingredients (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
ingredient VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Check out the docs: MySQL Docs | Create Table
You can check your syntax with following tool: SQL Validator

Related

Mysql File Import Issue When Use CURRENT_TIMESTAMP in column

I have a table which use CURRENT_TIMESTAMP as a default value on column. table working fine. but when export the database and then again import the exported mysql file, then the CURRENT_TIMESTAMP column replace all date with current today datetime.
This is the table srtructure:
CREATE TABLE a (
id int NOT NULL AUTO_INCREMENT,
col0 varchar(5) NOT NULL ,
col1 varchar(10) NOT NULL,
col2 varchar(20) ,
col3 varchar(20) NOT NULL,
createDateTime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id))
ENGINE=InnoDB
AUTO_INCREMENT=7430
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci
Just replace createDateTime timestamp with createDateTime datetime
I can say that the problem is here:
CREATE TABLE a (
id int NOT NULL AUTO_INCREMENT,
col0 varchar(5) NOT NULL ,
col1 varchar(10) NOT NULL,
col2 varchar(20) ,
col3 varchar(20) NOT NULL,
createDateTime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, <--- **Here
PRIMARY KEY (id))
ENGINE=InnoDB
AUTO_INCREMENT=7430
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci;
Since you define the createDateTime column with CURRENT_TIMESTAMP as default, whenever there's changes on the row data, the column would update itself.
I experienced this once when I was doing an app and I put my createdDate column as CURRENT_TIMESTAMP default similar as you. Then I realize that every time I made changes on some information through the app, the column also get updated to the current timestamp; which ultimately made my data all messed up!
I think what you can do from the exported file (sql dump) is:
Open the dump file using a text editor - (caveat: if the dump file is too large, might not be easy to open.)
Locate the CREATE TABLE syntax in the dump file then change the following:
CREATE TABLE a (
id int NOT NULL AUTO_INCREMENT,
col0 varchar(5) NOT NULL ,
col1 varchar(10) NOT NULL,
col2 varchar(20) ,
col3 varchar(20) NOT NULL,
createDateTime timestamp NOT NULL, <--- **Here
PRIMARY KEY (id))
ENGINE=InnoDB
AUTO_INCREMENT=7430
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci;
Save the dump file and try importing it.
If that doesn't work, I suggest:
Create another table using the CREATE TABLE with modified createDateTime column above - name it as a1 or a_copy etc.
Run INSERT query from table A like :
INSERT INTO a_copy SELECT * FROM a;
Check if data matches between the two table - I usually run a LEFT JOIN query for a quick check like this:
SELECT a.id, a_copy.id FROM a
LEFT JOIN a_copy
ON a.id=a_copy.id
AND a.col0=a_copy.col0
AND a.col1=a_copy.col1
AND a.col2=a_copy.col2
AND a.col3=a_copy.col3
AND a.createDateTime=a_copy.createDateTime
WHERE a_copy.id IS NULL;
*The ON condition can be just ON a.id=a_copy.id AND a.createDateTime=a_copy.createDateTime and WHERE a_copy.id IS NULL is just simply showing any result that doesn't match.
Once you're satisfied, export and import the a_copy table.

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
)

Proper workaround - there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

I have this table:
CREATE TABLE IF NOT EXISTS news (
id int(11) NOT NULL AUTO_INCREMENT,
data text,
date_published timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
date_edited timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
Giving me this error:
#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause.
There are older posts regarding this problem, but i want a proper workaround, due to current database structure.
Version: libmysql - 5.5.41
Running on: 5.5.41-ubuntu.14.04.1
It's a hard limitation. Best bet is making date_edited use CURRENT_TIMESTAMP and managing date_published in your application.
date_published timestamp NULL DEFAULT NULL,
date_edited timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Triggers might be an alternative option, but I try not to rely on stuff like that that can't easily be version-controlled.

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

Set a datetime column with a SQL trigger

I using a MySQL server (5.5.27 - Community Server). I have a table with this definition:
CREATE TABLE IF NOT EXISTS tbl_messages (
`msg_id` VARCHAR(50) NOT NULL ,
`msg_text` VARCHAR(50) NULL ,
PRIMARY KEY (`msg_id`);
I write a trigger that, when I do an insert, the server sets the msg_id column with the current time including microseconds with this format "yyyymmddhhnnssuuuuuu". "u" is for microseconds.
I created a trigger:
create trigger tbl_messages_trigger
before insert on tbl_messages
for each row
BEGIN
SET NEW.msg_id = DATE_FORMAT(NOW(),'%Y%m%d%H%i%s%f');
END;$$
But the msg_id column only gets values like this: 20130302144818*000000*, with microseconds in zero. ¿Is it possible capture the microseconds?
TIA,
From the code provided I guess that you are trying to use microsecond to minimize probability of getting same msg_id for different rows.
Also, msg_id is the primary key, which should not present any object-specific data but only be unique. There is a good link about: http://en.wikipedia.org/wiki/Surrogate_key
The best way to deal with primary keys in MySql is AUTO_INCREMENT column attribute. If you need insert time for messages, you may provide column for it:
CREATE TABLE tbl_messages
(
`msg_id` bigint(20) NOT NULL AUTO_INCREMENT,
`msg_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`msg_text` VARCHAR(50) NULL,
PRIMARY KEY (`msg_id`)
);