Creating table within mysql db, ERROR 1064 (42000)? - mysql

**Am working with MySQL 8.0 Command Line Client
I am trying to create a general table in mysql db but keep getting the following 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 'AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am I' at line 2"
I have look through the manual but haven't found anything that helps with this issue; have also tried removing the index (id) column and assigning it as the KEY
CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (ManufacturerPTNO),
);

CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (id)
);
Primary key should be id
Updated answer as per you comment
CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (ManufacturerPTNO),
UNIQUE KEY (id)
);

CREATE TABLE quantity (
id INT NOT NULL AUTO_INCREMENT,
ManufacturerPTNO VARCHAR(40) NOT NULL,
QuantityAvailable12am INT,
QuantityAvailable01am INT,
PRIMARY KEY (ManufacturerPTNO), <--- remove comma from here
);
and make PRIMARY KEY(id);

I had the same error message, after a long time I realized that I was trying to create a table by name "order" and that was the problem.As soon as I changed the name it worked!!!

Related

Why am I getting an error in my SQL command?

mysql>
CREATE TABLE twitter(
username varchar(255),
created_at varchar(45),tweet text,
retweet_count int(11),
location varchar(100),
place varchar(100),
PRIMARY KEY int(11));
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(11))' at line 1mys
The PRIMARY KEY clause is wrong.
You need to specify a name for your primary key column.
So, that would be ... id int(11), PRIMARY KEY (id));
You can not define PRIMARY KEY like that. The syntax for PRIMARY KEY is
PRIMARY KEY(Id)
Where you'll also have to create a column named Id
Id int(11)
try this
CREATE TABLE twitter (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(255),
created_at varchar(45),
tweet text,
retweet_count int(11),
location varchar(100),
place varchar(100),
PRIMARY KEY (id)
);

Error creating table in MySQL 5.7

Can some one help me in correcting the syntax according to MySQL 5.7?
Table :
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
PRIMARY KEY (ID)
);
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 '(ID)
You forgot comma at the end of this line:
ADDRESS VARCHAR(20) NOT NULL
You dont have a column named ID, you may want EMPID?
Replace
ADDRESS VARCHAR(20) NOT NULL
By
ADDRESS VARCHAR(20) NOT NULL,
There are two problems here:
A primary key clause is it's own clause, and needs to be separated from the previous column definition by a comma.
You don't have an id column - your primary key should (probably) be empid:
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
PRIMARY KEY (EMPID)
);
I guess this is what you are looking for:
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
);

I'm trying to create a database but i keep getting the same error, and according to me and my fellow students the code is fine

This is just an example, from the first 2 tables but i get the same error:
MySQL said: Documentation
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 ')' at line 10
CREATE TABLE Tallas
(
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla),
)
CREATE TABLE Modelo
(
IDModelo int NOT NULL AUTO_INCREMENT,
IDTalla int NOT NULL,
modeloNombre varchar(45) NOT NULL,
precio varchar(45) NOT NULL,
informacion varchar(45) NOT NULL,
PRIMARY KEY (IDModelo),
FOREIGN KEY (IDTalla) REFERENCES Modelo(IDTalla),
)
Remove the coma at PRIMARY KEY (IDTalla), to make it PRIMARY KEY (IDTalla)
and the same with REFERENCES Modelo(IDTalla),
Remove the comma -> (IDTalla),) (IDTalla))
Looks like you put an extra comma in your create table statement just after primary key.
CREATE TABLE Tallas (
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla),
)
This should be:
CREATE TABLE Tallas (
IDTalla int NOT NULL AUTO_INCREMENT,
descripcion varchar(45) NOT NULL,
PRIMARY KEY (IDTalla)
)
Same with the second statement.

learning sql creating table gives error

I am learning to use phpmyadmin and there is a database tshirtshop which has a table department now I want to create a category table but the sql in phpmyadmin is giving me error following is SQL query
CREATE TABLE 'category' (
'category_id' INT NOT NULL AUTO_INCREMENT,
'department_id' INT NOT NULL,
'name' VARCHAR(100) NOT NULL,
'description' VARCHAR(1000),
PRIMARY KEY ('category_id'),
KEY 'idx_category_department_id' ('department_id')
) ENGINE=MyISAM;
and following is the error
#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
''category' ( 'category_id' INT NOT NULL AUTO_INCREMENT, 'department_id' INT NOT ' at line 1
what should I do to eliminate the error?
Correct query is
CREATE TABLE `category` (
`category_id` INT NOT NULL AUTO_INCREMENT,
department_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
description VARCHAR(1000),
PRIMARY KEY (`category_id`),
KEY `idx_category_department_id` (`department_id`)
) ENGINE=MyISAM;
you cannot use single quote to enclose field name or table name, either use tilt ` or don't use anything. in my query I try to use both just to show you.
Try change the escape char from ' to ` . MySQL thinks that youre object names are strings.
CREATE TABLE `category` (
`category_id` INT NOT NULL AUTO_INCREMENT,
`department_id` INT NOT NULL,
`name` VARCHAR(100) NOT NULL,
`description` VARCHAR(1000),
PRIMARY KEY (`category_id`),
KEY `idx_category_department_id` (`department_id`)
) ENGINE=MyISAM;

Auto increment column

I want to make an AUTO_INCREMENT column in a database table,here is the syntax i write:
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int auto_increment
);
and i get the following error:
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
then i made it as a primary key:
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int primary_key auto_increment
);
and i get the following 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 'primary_key auto_increment,name varchar(20),mail varchar(30),comment varchar(100' at line 1
what is wrong???
It is PRIMARY KEY without the underscore.
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int primary key auto_increment
);
or
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int auto_increment,
primary key(`com_no`)
);
create table comments(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int auto_increment,
PRIMARY KEY (com_no)
);
(as per on-line MySQL manual).
create table comments
(
name varchar(20),
mail varchar(30),
comment varchar(100),
com_no int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (com_no)
);
ref.
Use primary key in place of primary_key
The proper syntax goes like this for example:
CREATE TABLE `admin` (
`id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`userid` VARCHAR(50) NULL DEFAULT '0',
`pwd` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
In MySQL, there can be only one auto increment column (that is generally known as identity column) and it should be also defined as a unique key. For example:
create table comments
(
com_no int NOT NULL AUTO_INCREMENT,
name varchar(20),
mail varchar(30),
comment varchar(100),
PRIMARY KEY (com_no)
);
Please see MySQL auto increment documentation for more details.