SQL syntax error, Can't find it? - mysql

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 ''comments' ( 'id' int(10) unsigned NOT NULL AUTO_INCREMENT, 'article_i' at line 1
CREATE TABLE IF NOT EXISTS 'comments' (
'id' int(10) unsigned NOT NULL AUTO_INCREMENT,
'article_id' int(10) NOT NULL,
'comment' varchar(45) NOT NULL,
'time' datetime NOT NULL,
'name' varchar(45) NOT NULL,
'email' varchar(45) NOT NULL,
PRIMARY KEY ('id'),
KEY 'fk_comments_article'('article_id')
);
Does anyone see the syntax error?

Use backticks instead of single qoutes .Single qoutes are used for string literals.
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`article_id` int(10) NOT NULL,
`comment` varchar(45) NOT NULL,
`time` datetime NOT NULL,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
KEY fk_comments_article(`article_id`)
);
backticks ` are used to enable identifires be used a column name / table name if they happen to be Keywords in MySQL .
It is the recommended way as it is highly unlikely that we know all the keywords beforehand and may end up using one of keyword as our name for column/table like you have done for column time in your CREATE satement .
But you should avoid using keywords known to you as identifires.

Remove the single quotes. Try this:
CREATE TABLE IF NOT EXISTS comments (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
article_id int(10) NOT NULL,
comment varchar(45) NOT NULL,
time datetime NOT NULL,
name varchar(45) NOT NULL,
email varchar(45) NOT NULL,
PRIMARY KEY (id),
KEY 'fk_comments_article'(article_id)
);
or try with back ticks:
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`article_id` int(10) NOT NULL,
`comment` varchar(45) NOT NULL,
`time` datetime NOT NULL,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
KEY fk_comments_article(`article_id`)
);
You are getting this error because when you write 'id' then it is treated as a string not a column which you intend

Remove Single quotes, tested below query in SQL Fiddle:
CREATE TABLE IF NOT EXISTS comments (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
article_id int(10) NOT NULL,
comment varchar(45) NOT NULL,
time datetime NOT NULL,
name varchar(45) NOT NULL,
email varchar(45) NOT NULL,
PRIMARY KEY (id),
KEY fk_comments_article(article_id)
);

Remove '
CREATE TABLE IF NOT EXISTS comments (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
article_id int(10) NOT NULL,
comment varchar(45) NOT NULL,
time datetime NOT NULL,
name varchar(45) NOT NULL,
email varchar(45) NOT NULL,
PRIMARY KEY (id),
KEY fk_comments_article(article_id)
);

Replace all of the single quotes ' with back ticks or simply remove the single quotes.
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`article_id` int(10) NOT NULL,
`comment` varchar(45) NOT NULL,
`time` datetime NOT NULL,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_comments_article`(`article_id`)
);

You have used single quotes which is worng and used for string only not for enclosing column names. Removethat and use backticks since you have some keywords like TIME, NAME:
Try:
CREATE TABLE IF NOT EXISTS `comments` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`article_id` INT(10) NOT NULL,
`comment` VARCHAR(45) NOT NULL,
`time` DATETIME NOT NULL,
`name` VARCHAR(45) NOT NULL,
`email` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
KEY fk_comments_article(`article_id`)
);

Related

MySQL assigning primary key for id is not working while creating table

Hi i am trying to create table like this but it is not working:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(70) NOT NULL,
`email` varchar(70) NOT NULL,
);
Where as when i create like this it is working fine
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(70) NOT NULL,
`email` varchar(70) NOT NULL,
PRIMARY KEY (`id`)
);
I'm confused what is wrong in my first query.?
It's not the primary key what is causing your trouble the syntax is correct.
The last line has a syntax error, the comma on the last line is wrong.
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(70) NOT NULL,
`email` varchar(70) NOT NULL,
)
should be
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(70) NOT NULL,
`email` varchar(70) NOT NULL
)

Sql error syntaxes with phpmyadmin

Good day,
I have an issue using the phpmyadmin for my database, I'm in new in this and this is the mysql structure from previewmysql:
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM(0) NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15) NULL,
`marital_status` ENUM(0) NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM(0) NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM(0) NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
The error 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 '0) NOT NULL , `email` VARCHAR(20) NOT NULL , `phone` INT(15) NULL , `marital_sta' at line 1
And also attached is my phpmyadmin table structure.
Will appreciate any help.
Try it.Hope errors goes boom.I just fixed your errors. But your table structure is not good enough. give time, then day by day you will also be expert on it.
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM('0','1') NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15),
`marital_status` ENUM('0','1') NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM('0','1') NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM('0','1') NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
Modify ENUM declaration as ENUM ('male', 'female') for gender column and others also as shown in your table. It will not accept ENUM(0).
ENUM(0) is wrong format , if you want that for gender roles then you can use :-
ENUM('Male', 'Female') i.e you can run this query :-
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM('Male', 'Female') NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15) NULL,
`marital_status` ENUM('Single','Married','divorced') NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM('no','yes') NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM('Cape Town','Woodstock') NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
You have used ENUM data type in your table. And provided 0 as argument but
An enumeration value must be a quoted string literal
You can refer the mySql documentation for more information
http://dev.mysql.com/doc/refman/5.7/en/enum.html

Error trying to create table in MySQL via terminal

I am trying to create a table in my database via terminal.
Here is my syntax:
CREATE TABLE `users` (
PRIMARY KEY(id) NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHAR NOT NULL,
`gender` VARCHAR NOT NULL,
`fav_color` VARCHAR NOT NULL,
`birthdate` DATE NOT NULL
);
I am getting 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 'NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHA' at line 2
What am i dong wrong here?
Besides the issues that Jens and Marc pointed out, You have to declare the length of your Varchar fields in order for this statement to work, like so:
CREATE TABLE `test`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR(45) NOT NULL,
`first_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`fav_color` VARCHAR(45) NOT NULL,
`birthdate` DATE NOT NULL,
PRIMARY KEY (`id`));
the syntax of your create statement is wrong:
the correct one is this:
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR(255) NOT NULL,
`first_name` VARCHAR(255) NOT NULL,
`gender` VARCHAR(255) NOT NULL,
`fav_color` VARCHAR(255) NOT NULL,
`birthdate` DATE NOT NULL,
PRIMARY KEY(`id`)
);
For more Information see the offical documentation.
it should be
id int primary key auto_increment not null
You're trying to define a primary key on a field that doesn't exist. Keys cannot be "not null" and definitely cannot be "auto_increment".

Error 1072: Key Column `course_id` does not exist

I'm trying to make a simple table with foreign keys but it seems there's a problem with the id. I've tried everything but still comes up with an error message please help. Thanks.
CREATE TABLE `staff_info`(
`staff_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`office_add` VARCHAR(45) NOT NULL,
`email` INT NULL,
`changed` TIMESTAMP,
PRIMARY KEY (`staff_id`),
FOREIGN KEY(`course_id`)
REFERENCES `course_info`(`course_id`)
)
ENGINE = InnoDB;
CREATE TABLE `course_info`(
`course_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`module` VARCHAR(45) NOT NULL,
`StartDate` TIMESTAMP,
`EndDate` TIMESTAMP,
`course_Update` TIMESTAMP,
PRIMARY KEY(`course_id`),
FOREIGN KEY(`student_id`)
REFERENCES student(`student_id`)
)
ENGINE = InnoDB;
CREATE TABLE `student` (
`student_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`end_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`email` VARCHAR(25) NULL,
`phone` VARCHAR(20) NOT NULL,
`student_update`TIMESTAMP,
PRIMARY KEY(`student_id`)
);
Your table destruct and creation orders are invalid
CREATE TABLE `student` (
`student_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`end_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`email` VARCHAR(25) NULL,
`phone` VARCHAR(20) NOT NULL,
`student_update`TIMESTAMP,
PRIMARY KEY(`student_id`)
);
CREATE TABLE `course_info`(
`course_id` INT NOT NULL AUTO_INCREMENT,
`student_id` INT,
`name` VARCHAR(45) NOT NULL,
`module` VARCHAR(45) NOT NULL,
`StartDate` TIMESTAMP,
`EndDate` TIMESTAMP,
`course_Update` TIMESTAMP,
PRIMARY KEY(`course_id`),
FOREIGN KEY(`student_id`)
REFERENCES student(`student_id`)
)
CREATE TABLE `staff_info`(
`staff_id` INT NOT NULL AUTO_INCREMENT,
`course_id` INT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`office_add` VARCHAR(45) NOT NULL,
`email` INT NULL,
`changed` TIMESTAMP,
PRIMARY KEY (`staff_id`),
FOREIGN KEY(`course_id`)
REFERENCES `course_info`(`course_id`)
)
set CREATE TABLE student before CREATE TABLE course_info
you use student_id when does not exist studen,
and use course_id when does not exist course
In your SQL Query for TABLE student, you are missing
ENGINE = InnoDB
Because course table is creating FOREIGN KEY from table student.
Also you need to create tables in the order.
student
course_info
staff_info

What's wrong with my query

Following is my query:
CREATE TABLE report_invoice(
'ID' INT(10) NOT NULL AUTO_INCREMENT,
'Invoice_No' VARCHAR(30) NOT NULL,
'Invoice_Type' INT(10) NOT NULL,
'Operator' VARCHAR(50) NOT NULL,
'Customer' VARCHAR(50) NOT NULL,
'Invoice_Date' DATE NOT NULL,
'Total' DECIMAL('10,2'),
PRIMARY KEY ('ID'));
I keep getting 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 ''ID'
INT(10) NOT NULL AUTO_INCREMENT,
'Invoice_No' VARCHAR(30) NOT NULL,
'Invoic' at line 2
You're using single quotes around your field names, use backticks instead:
CREATE TABLE report_invoice(
`ID` INT(10) NOT NULL AUTO_INCREMENT,
`Invoice_No` VARCHAR(30) NOT NULL,
`Invoice_Type` INT(10) NOT NULL,
`Operator` VARCHAR(50) NOT NULL,
`Customer` VARCHAR(50) NOT NULL,
`Invoice_Date` DATE NOT NULL,
`Total` DECIMAL(10,2),
PRIMARY KEY (`ID`));
Don't use simple quotes
You may replace them with backticks, or suppress them.
They would be usefull if you want to use reserved keywords as column names (bad idea anyway), or completely numeric column names, or special characters in column names.
So not in your case.
Don't put quotes around decimal scale and precision, too.
So this would do the job.
CREATE TABLE report_invoice(
ID INT(10) NOT NULL AUTO_INCREMENT,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT(10) NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL(10,2),
PRIMARY KEY (ID));
Remove the ticks or replace them with back ticks. Same for numbers.
CREATE TABLE report_invoice(
ID INT(10) NOT NULL AUTO_INCREMENT,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT(10) NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL(10,2),
PRIMARY KEY (ID));
CREATE TABLE report_invoice(
Id INT IDENTITY PRIMARY KEY,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL );