This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I just can't understand why I'm having a syntax error on this table creation:
CREATE TABLE IF NOT EXISTS Tasks (
ID INT UNSIGNED NOT NULL auto_increment,
Name VARCHAR(255) DEFAULT '' NOT NULL,
Description TEXT,
Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Status ENUM('New', 'Assigned', 'In Progress', 'On Hold', 'Done', 'Canceled'),
Priority ENUM('Urgent', 'High', 'Normal', 'Low'),
Creator SMALLINT UNSIGNED DEFAULT '0' NOT NULL,
Assignee SMALLINT UNSIGNED,
Starting DATETIME,
Deadline DATETIME,
Completion DATETIME,
PRIMARY KEY(ID)
) ENGINE = INNODB;
Gives me:
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
'Starting DATETIME,
Deadline DATETIME,
Completion DATETIME,
PRIMARY KEY(ID)
) ENG' at line 10
Looks like its giving me trouble with datetime but why? I've used this in another table and it's working.
Thanks.
STARTING is a reserved keyword, It should be escape with backtick,
CREATE TABLE IF NOT EXISTS Tasks
(
ID INT UNSIGNED NOT NULL auto_increment,
Name VARCHAR(255) DEFAULT '' NOT NULL,
Description TEXT,
Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Status ENUM('New', 'Assigned', 'In Progress', 'On Hold', 'Done', 'Canceled'),
Priority ENUM('Urgent', 'High', 'Normal', 'Low'),
Creator SMALLINT UNSIGNED DEFAULT '0' NOT NULL,
Assignee SMALLINT UNSIGNED,
`Starting` DATETIME,
Deadline DATETIME,
Completion DATETIME,
PRIMARY KEY(ID)
) ENGINE = INNODB;
MySQL Reserved Keyword List
Starting is a reserved keyword. Perhaps you could use a different one.
Related
I am trying to build my table in MySQL following this answer. Hower i keep getting errors in MySQL even launching the same query used as example in the answer
this query is from documentation and works correctly
CREATE TABLE triangle (
sidea DOUBLE,
sideb DOUBLE,
sidec DOUBLE AS (SQRT(sidea * sidea + sideb * sideb)) stored
);
this query comes from the answer i've linked and it gives me an error
CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug text NOT NULL AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
this is the query i am trying to execute based on the answer which is giving me also an error
CREATE TABLE IF NOT EXISTS myTable(
id BIGINT NOT NULL AUTO_INCREMENT,
first VARCHAR(104) NOT NULL DEFAULT '',
second DATETIME NULL,
third DATETIME NULL,
fourth VARCHAR(255) NULL,
table_key varchar(64) NOT NULL AS (SHA2(concat_ws(',',first, second, third, fourth), 256)) stored unique,
PRIMARY KEY (id),
INDEX (first));
can you help me figuring out why it's not working?
Note that SHA2(concat_ws(',',first, second, third, fourth), 256) this works in a normal select statement
EDIT
this is the error i am getting
Query execution failed
Reason:
Errore SQL [1064] [42000]: (conn:28) 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 'AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT ' at line 5
Query is : CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug text NOT NULL AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8
You have to add the NOT NULL after the AS part (computed column definition) of the column like this:
CREATE TABLE IF NOT EXISTS myTable(
id BIGINT NOT NULL AUTO_INCREMENT,
first VARCHAR(104) NOT NULL DEFAULT '',
second DATETIME NULL,
third DATETIME NULL,
fourth VARCHAR(255) NULL,
table_key varchar(64) AS (SHA2(concat_ws(',',first, second, third, fourth), 256)) stored unique NOT NULL,
PRIMARY KEY (id),
INDEX (first)
);
Also have a look at the official document of CREATE TABLE. There you can find the description of the datatype syntax. The definition of the computed column is part of the datatype.
Im very new to mysql and I keep getting the same error whenever i try to make a table
CREATE TABLE tasks {
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT PRIMARY KEY ,
description VARCHAR(64) NOT NULL,
completed TINYINT(2) NOT NULL DEFAULT 0,
created DATETIME NOT NULL DEFAULT NOW(),
last_updated DATETIME NOT NULL DEFAULT NOW() ON UPDATE NOW()
};
I wanted it to make a table but instead I got:
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 '{
id INT NOT NULL PRIMA' at line 1
You have a syntax error. CREATE TABLE uses parentheses not braces.
CREATE TABLE foo(col1 int, col2 varchar(25), col3 timestamp);
Here is the right syntax:
CREATE TABLE tasks (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, description VARCHAR(64) NOT NULL, completed TINYINT(2) NOT NULL DEFAULT 0, created DATETIME NOT NULL DEFAULT NOW(), last_updated DATETIME NOT NULL DEFAULT NOW() ON UPDATE NOW())
CREATE TABLE IF NOT EXISTS `creditors` (
`id` int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(50) NOT NULL,
`route` varchar(255) NOT NULL,
`mobile` int(10) NOT NULL,
`credit_amount` double(100) NOT NULL,
`start_date` date NOT NULL,
`due_date` date NOT NULL,
UNIQUE KEY `idUnique` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
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 ') NOT NULL,
start_date date NOT NULL,
due_date date NOT NULL, ' at line 6
What is my syntax error in this SQL query?
If you set a Column to primary key, it consists only Unique value. Then why did you try to made the same to UNIQUE KEY?
And also Delete the RANGE for Double.
SQLFiddle Example
CREATE TABLE `creditors` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(50) NOT NULL,
`route` varchar(255) NOT NULL,
`mobile` int NOT NULL,
`credit_amount` double NOT NULL,
`start_date` date NOT NULL,
`due_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
The error is with this statement:
`credit_amount` double(100) NOT NULL
Give precision after 100 in double data type.
Solution:
`credit_amount` double(100,0) NOT NULL
Instead of 0 after 100 give number of digits after decimal point.
If you want to use double you must specify both M and D where
M is number digits in total, of which D digits may be after the decimal point, i.e. double(10,0). Otherwise use float which which allows syntax like float(10).
Also, unless you really benefit from this, do not use latin1 charset, but rather UTF-8
> CREATE TABLE If NOT EXISTS HARDWARE
(
'RECORD_ID' int PRIMARY KEY NOT NULL,
'Asset_Number' int(10) unique not null index,
'Type' char(20) not null,
'Name' varchar(50) not null index,
'Device Description' varchar(50) not null,
'Building' char(40) not null index,
'Date Created' date DEFAULT CURDATE() not null,
'Serial Number' varchar(50) index not null,
'Owner' char(50) index,
'User Created' CHAR ON CREATE USER() INDEX,
'Date Modified' date ON UPDATE CURDATE(),
'Purchase Year' int(4),
'Model' char(50),
'User Modified' CHAR(50),
'PO Number' int(20),
'Location' VARCHAR(50) index,
'OS' VARCHAR(20),
'RAM' int(4),
'Notes' text,
'Verified_By_t' char(50) on update user(),
'Internal IP Address' ON UPDATE USER(),
'Verified_Date_d' date ON UPDATE CURDATE(),
'Ethernet MAC' VARCHAR(20) index,
'Wireless MAC' VARCHAR(20) index,
'Usage' VARCHAR(20),
'Funding Source' VARCHAR(20),
'Lock Combo' int(10),
'ServiceLog::Date Created' VARCHAR(20),
'ServiceLog::Issue' VARCHAR(20),
'Ext. Ethernet Adapter' VARCHAR(20),
'AD' bit,
'Updated' date on update CURDATE(),
'Asset LI Hardware::ID Link' VARCHAR(20),
'Hardware' VARCHAR(20),
'Hardware Asset LI::Description' VARCHAR(20),
'Hardware Asset LI::Location' VARCHAR(20),
'Hardware Asset LI::Type' VARCHAR(20),
'Main Menu::Company' VARCHAR(20)
)
and the error I am getting is
Error Code: 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 ''RECORD_ID' int PRIMARY KEY NOT NULL, 'Asset_Number' int unique not null index, ' at line 3
I have tried varying this with:
CREATE TABLE If NOT EXISTS HARDWARE
(
RECORD_ID int PRIMARY KEY NOT NULL,
Asset_Number int unique not null index,
CREATE TABLE If NOT EXISTS HARDWARE
(
Asset_Number int unique not null index,
and other variations.
This is what I currently have and instead of one error it shows 5 errors.
CREATE TABLE If NOT EXISTS HARDWARE
(
RECORD_ID int PRIMARY KEY NOT NULL,
Asset_Number int unique not null index,
Type char(20) not null,
Name varchar(50) not null index,
Device_Description varchar(50) not null,
Building char(40) not null index,
Date_Created date DEFAULT CURDATE() not null,
Serial_Number varchar(50) index not null,
Owner char(50) index,
User_Created CHAR ON CREATE USER() INDEX,
Date_Modified date ON UPDATE CURDATE(),
Purchase_Year int(4),
Model char(50),
User_Modified CHAR(50),
PO_Number int(20),
Location VARCHAR(50) index,
OS VARCHAR(20),
RAM int(4),
Notes text,
Verified_By_t char(50) on update user(),
Internal_IP_Address ON UPDATE USER(),
Verified_Date_d date ON UPDATE CURDATE(),
Ethernet_MAC VARCHAR(20) index,
Wireless_MAC VARCHAR(20) index,
Usage VARCHAR(20),
Funding_Source VARCHAR(20),
Lock_Combo int(10),
AD bit,
Updated_date on update CURDATE()
)
and the error I see for it:
Error Code: 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 'index, Type char(20) not null, Name varchar(50) not null index, Device_Descripti' at line 4
You have numerous errors in your code. Here are some that I've spotted:
The index should be a separate line, not inline.
MySQL supports ON UPDATE DEFAULT CURRENT_TIMESTAMP . . . for one column and that column should be a datetime.
There is no ON UPDATE USER().
The IP Address does not have a type.
There is no ON CREATE.
(Note: Perhaps the most recent version of MySQL does support some of these options, but they don't look familiar to me.)
I would suggest that you create the table starting with the first column -- say by commenting out all the other columns. Then, add one column at a time and fix the errors as you encounter them.
Although i think the syntax of the following mysql code is correct as per documentation, SQLFiddle pops up an error and since i think the syntax of the line that springs up the error is correct, i dont know what to change.
CREATE TABLE students
(
student_id DECIMAL(38) NOT NULL,
username VARCHAR(30),
email VARCHAR(80),
password VARCHAR(30),
f_name VARCHAR(30),
l_name VARCHAR(30),
bio VARCHAR(350),
dp VARCHAR(15),
is_suspended CHAR(1) DEFAULT '0' NOT NULL,
suspension_reason VARCHAR(150),
role_id DECIMAL(38) NOT NULL,
created_on DATETIME(6) DEFAULT SYSDATE() NOT NULL,
updated_on DATETIME(6),
is_active CHAR(1) DEFAULT '1' NOT NULL,
city VARCHAR(15) NOT NULL,
state VARCHAR(15) NOT NULL,
zip VARCHAR(6) NOT NULL,
b_day DATETIME,
CONSTRAINT students_id_pk PRIMARY KEY(student_id),
CONSTRAINT students_role_id_fk FOREIGN KEY(role_id) REFERENCES user_role(role_id),
CONSTRAINT students_username_uq UNIQUE(username),
CONSTRAINT students_email_uq UNIQUE(email)
);
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() NOT NULL,
updated_on DATETIME(6),
is_active CHAR(1) DEFAULT '1' at line 14
Please help me resolve this issue.
As explained in the documentation, synonyms for CURRENT_TIMESTAMP are allowed for the default value. SYSDATE() is not such a synonym. You can use NOW() (or several other constructs).
So, the correct syntax for that column is:
created_on DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,