How to fix this mysql create table syntax error [duplicate] - mysql

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 getting a mysql create table syntax error and hoping someone can spot it before I loose my sanity.
create table buyer_representations (
Key int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Start_Date date,
End_Date date,
Property_Type varchar(255),
Geographic_Location varchar(255),
Commission varchar(255),
Alternate_Commission varchar(255),
Lease_Commission varchar(255),
Holdover varchar(255),
Clauses varchar(2000),
primary key (Key)
);
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 'int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Star' at line 2
mysql Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using EditLine wrapper
I tried adding 'primary key' at the end of line 2, as well a default value, and switching up 'not null' with 'auto_increment' .. but nothing is working.
Edit:
I also tried changing Key to KeyId, and putting Key in quotes .. but the error remains the same.
Final Edit:
putting Key inside backquotes finally worked
create table buyer_representations (
`Key` int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Start_Date date,
End_Date date,
Property_Type varchar(255),
Geographic_Location varchar(255),
Commission varchar(255),
Alternate_Commission varchar(255),
Lease_Commission varchar(255),
Holdover varchar(255),
Clauses varchar(2000),
primary key (`Key`)
);

Key is a reserve keyword, rename that and you should be golden.
A complete list of keywords and reserved words can be found in section 10.3 Keywords and Reserved Words. Below
https://dev.mysql.com/doc/refman/8.0/en/keywords.html
in your case, this should work (I name it test, but rename it to whatever fits your situation)
create table buyer_representations (
test int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Start_Date date,
End_Date date,
Property_Type varchar(255),
Geographic_Location varchar(255),
Commission varchar(255),
Alternate_Commission varchar(255),
Lease_Commission varchar(255),
Holdover varchar(255),
Clauses varchar(2000),
primary key (test)
);

The problem is the column name you have used in your table.
The first column name is Key.
Key is considered as a reserved word in Mysql and it has a special meaning. That is why there is an error.
If you want to use the same column name you can do it by writing the column name inside ``.
For eg.
create table buyer_representations (
`Key` int not null auto_increment,
This will work fine.

Related

What is problem in this MySQL Query, it is giving error 1064 (42000)? [duplicate]

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 28 days ago.
the query is
CREATE TABLE order(
order_id int primary key,
customer_name varchar(30) not null,
product_name varchar(20) not null,
date_ordered date,
quantity int,
unit_price float,
phone_no varchar(20)
);
the error is :
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 'order(
order_id int primary key,
customer_name varchar(30) not null,
' at line 1
i am using MySQL verson 8.0.32
Because order is part of the SQL syntax (to sort queried records using ORDER BY) it wont work.
You have to rename your table to something different like for example orders then it will work:
CREATE TABLE orders (
order_id int primary key,
customer_name varchar(30) not null,
product_name varchar(20) not null,
date_ordered date,
quantity int,
unit_price float,
phone_no varchar(20)
);
ORDER is part os mysql syntax just use another name for your table like orders:
CREATE TABLE orders (
order_id int primary key,
customer_name varchar(30) not null,
product_name varchar(20) not null,
date_ordered date,
quantity int,
unit_price float,
phone_no varchar(20)
);
order is a keyword you cannot use it.To know the list of other keywords check the below link
https://dev.mysql.com/doc/refman/8.0/en/keywords.html

SQL syntax error; "check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Order INT NOT NULL' [duplicate]

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.

Getting error in my mysql code: You have an error in your SQL syntax

I am trying to solve error but i dont have any idea how to solve please help me.
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 'create table jobseeker(j_id first_name varchar(20),last_name varchar(20),phone_' at line 5
create table admin(primary key,admin_name varchar(20),admin_password varchar(20));
create table jobseeker(j_id int primary key,first_name varchar(20),last_name varchar(20),phone_no int(11),email_id varchar(20),address1 varchar(30), address2 varchar(30),qualification varchar(20),birthday_date date,gender varchar(10),field varchar(50),year of passing int(11), board varchar(20),zip int(11),city varchar(20),state varchar(20),username varchar(20),password varchar(20));
create table job seeker job(id int primary key,j_id int(10),c_name varchar(20),post varchar(20),foreign key references(j_id int foregin key references job seeker(id)));
create table company news(n_id int primary key,c_id int(11)primary key,information varchar(50),link varchar(50));
create table company(c_id int primary key,c_name varchar(20),phone_no int(11),address varchar(20),email_id varchar(20),password varchar(20));
Well, you have some errors, but as Bill Karwin said, run SQL statemets one at a time.
In your first statement you dont have a name and a type for your primary key.
create table admin(id int primary key,admin_name varchar(20),admin_password varchar(20));
In your second statement you have a column named 'year of passing'. It's not allowed, you should use underscore.
create table jobseeker(j_id int primary key,first_name varchar(20),last_name varchar(20),phone_no int(11),email_id varchar(20),address1 varchar(30), address2 varchar(30),qualification varchar(20),birthday_date date,gender varchar(10),field varchar(50),year_of_passing int(11), board varchar(20),zip int(11),city varchar(20),state varchar(20),username varchar(20),password varchar(20));
Again, check every statement and I recommend you to use some editor with syntax highlighting.
Because j_id did not have the data type. Which is int.
Also, the year of passing int(11) can not have spaces. Well, it can f that is what you really want to do. I will recomend you do not. Rather use underscore "_"
Write it like below if you need your spaces for your column name: year of passing` int(11)
create table jobseeker(
j_id int primary key,
first_name varchar(20),
last_name varchar(20),
phone_no int(11),
email_id varchar(20),
address1 varchar(30),
address2 varchar(30),
qualification varchar(20),
birthday_date date,
gender varchar(10),
field varchar(50),
`year of passing` int(11),
board varchar(20),
zip int(11),
city varchar(20),
state varchar(20),
username varchar(20),
password varchar(20)
);
notice that I also have spaces for year of passing, this is not recommended. I recommend that you one of the following for that column.
year_of_passing int(11)
or
yearOfPassing int(11)

what is wrong with the sql query?

CREATE TABLE 1
(text longtext(4,294,967,295),
date VARCHAR(50),
time VARCHAR(50),
id INT(11 ) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY ( `id` )
What is wrong with the above SQL statement? I keep getting an 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 '(4,294,967,295), date VARCHAR(50), time VARCHAR(50), id INT( 11 ) NOT NULL AU' at line 3
1 is a lousy name for a table. If you use it, you need to escape the name. Also, commas aren't allow in lengths for character fields and longtext doesn't need a length anyway:
CREATE TABLE `1`
(text longtext,
date VARCHAR(50),
time VARCHAR(50),
id INT(11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY
)
And, you don't "add" a primary key. You just declare it.

MySQL issue with column name "range" [duplicate]

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