What is the reason for Mysql error code 1293 - mysql

Here is the message of mysql error 1293:
SQL Error (1293): Incorrect table definition; there can be only one
TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
what is the reason for mysql only allows one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause per table.

Only one TIMESTAMP field can default to "now"
I should say first of all, if you are trying to define more than one MySQL TIMESTAMP fields using CURRENT_TIMESTAMP or "default now", unfortunately that is bad, you can't do it in MySQL
I just got this MySQL TIMESTAMP error when trying to create a table like this:
create table users (
id int unsigned auto_increment not null primary key,
username varchar(50) not null unique,
password varchar(40) not null,
email_address varchar(128) not null unique,
email_sent timestamp not null,
last_login timestamp not null default now()
) ENGINE = InnoDB;
When I first solved this problem I thought MySQL required the "CURRENT_TIMESTAMP (default now)" field to be declared before any other TIMESTAMP fields, so I solved my problem like this:
create table users (
id int unsigned auto_increment not null primary key,
username varchar(50) not null unique,
password varchar(40) not null,
email_address varchar(128) not null unique,
last_login timestamp not null default now(),
email_sent timestamp not null
) ENGINE = InnoDB;

Related

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 'PRIMERY KEY

I used the following code to create a table on MySQL:
CREATE TABLE IF NOT EXISTS auth (
id INT AUTO_INCREMENT,
email VARCHAR(50) UNIQUE NOT NULL PRIMERY KEY,
password VARCHAR(250) NOT NULL,
username VARCHAR(50) UNIQUE,
admin BOOLEAN NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
But I got this error:
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 'PRIMERY KEY,
password VARCHAR(250) NOT NULL,
username VARCHAR(50) UNIQUE,
What is the problem?
EDIT: The problem was because of a typo in PRIMARY KEY, but after fixing that, I faced another error:
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
The typo of PRIMERY is an obvious misspelling of PRIMARY. This is something you should have been able to answer yourself.
The second issue is that an auto-increment column must be the first column of a key. Most people say it must be the primary key, but technically it can be any key of the table.
The following table definition works:
CREATE TABLE IF NOT EXISTS auth (
id INT UNIQUE AUTO_INCREMENT,
email VARCHAR(50) PRIMARY KEY,
password VARCHAR(250) NOT NULL,
username VARCHAR(50) UNIQUE,
admin BOOLEAN NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Adding UNIQUE to the id column makes it a key, which satisfies the requirement that the auto-increment column be the first column of a key.
I removed the UNIQUE NOT NULL keywords on the email column, because that's implied by being PRIMARY KEY. It's redundant, and unfortunately MySQL will create an extra unnecessary unique index for email if you include both keywords.
If I'm not mistaken you typo'd PRIMERY KEY so it should be PRIMARY KEY
CREATE TABLE IF NOT EXISTS auth (
id INT AUTO_INCREMENT,
email VARCHAR(50) UNIQUE NOT NULL PRIMARY KEY,
password VARCHAR(250) NOT NULL,
username VARCHAR(50) UNIQUE,
admin BOOLEAN NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
EDIT:
The issue you are getting is because you have an auto_increment and primary key on different columns. You can use both on 1 column but not on 2 different ones because there can only be 1 auto increment per table.
New script:
CREATE TABLE IF NOT EXISTS auth (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(250) NOT NULL,
username VARCHAR(50) UNIQUE,
admin BOOLEAN NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Mysql automated date field, syntax errors

I'm trying to create a mysql table which contains two fields that are automatically given the current date when data is first put into them
CREATE TABLE IF NOT EXISTS `database`.`characters` (
`character_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`ckey` VARCHAR(30) NOT NULL,
`character_name` VARCHAR(26) NOT NULL,
`birth` DATE NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_seen` DATE NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`character_id`),
UNIQUE INDEX `character_id_UNIQUE` (`character_id` ASC) VISIBLE)
this is not working, i get invalid default value error, which is progress
Everything else i've tried throws error 1064, sql syntax
In place of current timestamp I have tried...
GET_DATE()
GET_DATE
GETDATE
CURDATE
CURDATE()
CURRENT_DATE
CURRENT_DATE()
All of these are throwing the same syntax error, there is no error if i remove the default value.
I am almost entirely clueless about database code, this is a real newbie project, any help would be appreciated
Use datetime instead of date.
CREATE TABLE IF NOT EXISTS `characters` (
`character_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`ckey` VARCHAR(30) NOT NULL,
`character_name` VARCHAR(26) NOT NULL,
`birth` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_seen` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`character_id`),
UNIQUE INDEX `character_id_UNIQUE` (`character_id` ASC) VISIBLE)

MySql table have two or more columns have default date vaule CURRENT_TIMESTAMP,any Solutions?

if i have a table with two columns create_time and update_time,the data type is timestamp,then have default value CURRENT_TIMESTAMP,the sql code of created table is:
CREATE TABLE `t_activity` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`STARTDATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ENDDATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
);
but it prompt error:1293,there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or UPDATE clause.
I am not sure what you're trying to accomplish having both the STARTDATE and ENDDATE populated with the CURRENT_TIMESTAMP. However to fix your code try changing your data types to DATETIME like this:
**CREATE TABLE `t_activity` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`STARTDATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ENDDATE` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
);**
http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html
This limitation in Mysql:
Previously, at most one TIMESTAMP column per table could be
automatically initialized or updated to the current date and time.
This restriction has been lifted. Any TIMESTAMP column definition can
have any combination of DEFAULT CURRENT_TIMESTAMP and ON UPDATE
CURRENT_TIMESTAMP clauses. In addition, these clauses now can be used
with DATETIME column definitions. For more information, see Automatic
Initialization and Updating for TIMESTAMP and DATETIME.
Check This for more detail
http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-5.html

Error Code: 1067. Invalid default value for 'last_visit'

A keep getting this error in my mysql statement even though I'm not specifying a default value. Previous responses said adding a NOT NULL would fix it, but I still get the error. What is odd is if I switch the lines of date_joined and last_visit, I get the same error, except on date_joined. Any ideas why its giving this error? Thanks
CREATE TABLE user_info(
user_id serial,
oauth_provider VARCHAR(255) NOT NULL,
oauth_id VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
date_of_birth date NOT NULL,
email VARCHAR(255) NOT NULL,
gender char NOT NULL,
date_joined timestamp NOT NULL,
last_visit timestamp NOT NULL,
account_enabled boolean NOT NULL,
CONSTRAINT user_info_pkey PRIMARY KEY (user_id)) ENGINE=InnoDB;
I use your code and me all work:
If you are using MySQL < 5.6 you cant have 2 timestamp column with same default values.
For MySQL 5.5 check this http://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.
So if you want to have 2 column with timestamp try this.
CREATE TABLE user_info(
user_id serial,
oauth_provider VARCHAR(255) NOT NULL,
oauth_id VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
date_of_birth date NOT NULL,
email VARCHAR(255) NOT NULL,
gender char NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMPL,
last_visit TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
account_enabled boolean NOT NULL,
CONSTRAINT user_info_pkey PRIMARY KEY (user_id)
) ENGINE=InnoDB;

Resetting mysql integer value

I have MySQL table name Order_Ticket_Number as following:
CREATE TABLE order_ticket_number (
id_store_ticket int(10) NOT NULL,
id_order_ticket int(3) ZEROFILL AUTO_INCREMENT NOT NULL,
id_date_order timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
id_order int(10) unsigned NOT NULL default 0,
PRIMARY KEY (id_store_ticket,id_order_ticket,id_date_order)
)
ENGINE=myisam DEFAULT CHARSET=utf8;
I want to restart the value of id_order_ticket each time I open a store from the Python application I am developing. id_order_ticket is starting with 001, 002 ... and I want every day to start with 001 when I am making SOD(start-of-day). There will be not conflict to have two time ex.id_order_ticket = 001 because id_date_order is also PK and Engine=myisam.
Thank you
If I understand correctly, you need to change the order of columns in the primary key and use a DATE column, not timestamp:
PRIMARY KEY (id_store_ticket, id_date, id_order_ticket)
Example, tested in SQL-Fiddle:
CREATE TABLE order_ticket_number
( id_store_ticket int(10) NOT NULL,
id_date DATE NOT NULL,
id_order_ticket int(3) ZEROFILL AUTO_INCREMENT NOT NULL,
id_date_order timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
id_order int(10) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (id_store_ticket, id_date, id_order_ticket)
)
ENGINE = MyISAM ;
Dates cannot have a DEFAULT CURRENT_TIMESTAMP property, so yuo have to ensure that the date and timestamp columns have consistent values.
Mysql documentation states that it is impossible:
To change the value of the AUTO_INCREMENT counter to be used for new
rows, do this:
ALTER TABLE t2 AUTO_INCREMENT = value; You cannot reset the counter to
a value less than or equal to any that have already been used.
reference