This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 1 year ago.
I'm trying to create a table in my schema (mydb) called employee, but whenever I try running the script to create the table I get error #1064.
I'm new to SQL so I was looking at examples on how to create tables and went off those examples to create my own until I bumped into this error. Could anyone explain why I am getting this error message please?
MySQL Workbench Community 8.0 Version 8.0.26:
CREATE TABLE 'mydb'.'employee'
(
'emp#' INT PRIMARY KEY,
'firstname' VARCHAR(20) NOT NULL,
'surname' VARCHAR(20) NOT NULL,
'salary' INT default 0,
'city' VARCHAR(20),
UNIQUE(firstname, surname)
);
Error:
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 ''mydb'.'employee' ( 'emp#' INT Primary key, 'firstname' VARCHAR(20) NOT NULL, 's' at line 1
Your issue is the because you are adding ' single quotes in your CREATE TABLE statement, you need to do it like this:
CREATE TABLE mydb.employee
(
emp INT PRIMARY KEY,
firstname VARCHAR(20) NOT NULL,
surname VARCHAR(20) NOT NULL,
salary INT default 0,
city VARCHAR(20),
UNIQUE(firstname, surname)
);
If you need the quotes in Mysql you need to use the curly single quote like this:
CREATE TABLE `mydb`.`employee`
(
`emp_id` INT PRIMARY KEY,
`firstname` VARCHAR(20) NOT NULL,
`surname` VARCHAR(20) NOT NULL,
`salary` INT default 0,
`city` VARCHAR(20),
UNIQUE(firstname, surname)
);
Also instead of using #, use _id or no you can check the following reference about MySQL Naming Rules
Related
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 1 year ago.
I'm using MariaDB and MySQL and I'm trying to create a table using the command prompt but I'm getting a syntax error and I am unsure what is wrong. Below is the error:
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 'Order INT NOT NULL,
FName VARCHAR(255),
LName VARCHAR(255),
PhoneNum VARCH...' at line 3
Here is what I have:
CREATE DATABASE projecttestDB;
USE projecttestDB;
DROP TABLE IF EXISTS Staff;
CREATE TABLE Staff (
Year INT NOT NULL,
Order INT NOT NULL,
FName VARCHAR(255),
LName VARCHAR(255),
PhoneNum VARCHAR(255),
Salary INT,
CHECK(Salary>=0),
PRIMARY KEY (Year, Order)
);
order is a reserved word in SQL. If you absolutely have to use it, you can escape the column name using backticks:
CREATE TABLE Staff (
Year INT NOT NULL,
`Order` INT NOT NULL, -- Here!
FName VARCHAR(255),
LName VARCHAR(255),
PhoneNum VARCHAR(255),
Salary INT,
CHECK(Salary>=0),
PRIMARY KEY (Year, `Order`) -- And here
);
But it's probably a better idea to rename it to something that isn't a reserved word, such as order_num:
CREATE TABLE Staff (
Year INT NOT NULL,
Order_Num INT NOT NULL,
FName VARCHAR(255),
LName VARCHAR(255),
PhoneNum VARCHAR(255),
Salary INT,
CHECK(Salary>=0),
PRIMARY KEY (Year, Order_Num)
);
Order is a system key word. If you change it to something else it will work.
I am trying to create a table in MySQL with the query
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
rank TINYINT NOT NULL,
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, rank),
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
but seems like it is throwing error everytime I made updates too. I don't know what is going wrong with it.
Error coming up is
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 'rank TINYINT NOT NULL, groupName
VARCHAR at line 3
MySQL 8.0.2 added support for the window rank function, making it a reserverd word.
You could escape it using backticks (`):
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
`rank` TINYINT NOT NULL, -- Here
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, `rank`), -- And here
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
But it may be a better idea to just use a name that isn't a reserved word, such as rosterRank instead of rank:
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
rosterRank TINYINT NOT NULL, -- Here
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, rosterRank), -- And here
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
I am getting a SQL query syntax error while creating a table. Here is the SQL query:
CREATE TABLE ACCOUNT (
ACCNO NUMBER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BA L NUMBER(8,2) NOT NULL,
CREATION-DT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
Here is the error:
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 'NUMBER(5) NOT NULL.
What is wrong with my syntax?
Using SQLfiddle you can see this is not valid.
You have multiple problems with your syntax; invalid data types, invalid column names, etc. You can encapsulate the invalid names in backticks but then you will have to remember to encapsulate them later.
Instead of Number you probably meant numeric but i would suggest trying Integer instead.
Or just try this:
CREATE TABLE ACCOUNT (
ACCNO INTEGER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BAL INTEGER(8) NOT NULL,
CREATIONDT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
NUMBER is not a valid datatype in MySQL, you should choose the one from the list (maybe you've meant NUMERIC).
You should also escape some identifiers (in your case, column names BA L, which includes a space within it, and CREATION-DT, which includes a dash) with backticks ` in MySQL:
CREATE TABLE `ACCOUNT` (
`ACCNO` NUMERIC(5) NOT NULL,
`NAME` VARCHAR(20) NOT NULL,
`BA L` NUMERIC(8,2) NOT NULL,
`CREATION-DT` DATE NOT NULL,
PRIMARY KEY ( `ACCNO` )
);
SQLFiddle
This question already has answers here:
Equivalent of MSSQL IDENTITY Column in MySQL
(2 answers)
Closed 9 years ago.
I have a sql-server query and i want to run it (or create an equivalent query) on mysql. But currently i'm getting syntax error. Can anyone help me to create a mysql equivalent of the below mentioned sql-server query?
create table Emp(EmpName varchar(20) not null,keyword varchar(20) not null,
DOB datetime not null,Comments text(65535),EmpId int primary key IDENTITY(1,1));
Following is the 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 'IDENTITY(1,1))' at line 1
CREATE TABLE Emp (
EmpId INT AUTO_INCREMENT,
EmpName VARCHAR(20) NOT NULL,
KEYWORD VARCHAR(20) NOT NULL,
DOB DATETIME NOT NULL,
Comments TEXT(65535), PRIMARY KEY (EmpId)
);
You can use auto_increment .By default it will increase by 1.If you want any other increment you can specify your own.
It should be defined as auto_incremented primary key instead. For example:
CREATE TABLE Emp (
EmpId INT AUTO_INCREMENT,
EmpName VARCHAR(20) NOT NULL,
KEYWORD VARCHAR(20) NOT NULL,
DOB DATETIME NOT NULL,
Comments TEXT(65535),
PRIMARY KEY (EmpId)
);
You can use an auto-increment field
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.
When I try to run this script I am getting an error at "range" if I remove or change the name I can run the script however the application need this column to store data. Any idea how to insert this into MySQL?
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 '"range" CHAR(5) NOT NULL, range_max_value NUMERIC(18,3)' at line 22
CREATE TABLE My_table (
chart_id INTEGER NOT NULL,
u_range CHAR(5),
l_range CHAR(5),
"range" CHAR(5) NOT NULL,
range_max_val NUMERIC(18,3),
range_min_val NUMERIC(18,3),
PRIMARY KEY (chart_id)
);
Range is a reserved keyword that needs to be escaped with backticks.
CREATE TABLE My_table (
chart_id INTEGER NOT NULL,
u_range CHAR(5),
l_range CHAR(5),
`range` CHAR(5) NOT NULL,
range_max_val NUMERIC(18,3),
range_min_val NUMERIC(18,3),
PRIMARY KEY (chart_id)
);
You should take a look at the reserved words list. Range is one of those words. You need to escape it with tick marks:
CREATE TABLE My_table (
chart_id INTEGER NOT NULL,
u_range CHAR(5),
l_range CHAR(5),
`range` CHAR(5) NOT NULL,
range_max_val NUMERIC(18,3),
range_min_val NUMERIC(18,3),
PRIMARY KEY (chart_id)
);
RANGE is a reserved word. http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html