#1064 You have an error in your SQL syntax; - mysql

I'm getting the following error when I try to create a TABLE in SQL.
The version of phpMyAdmin I'm using it 4.2.7.1 which says it's up to date.
Can anyone help? I can't spot an error? I have looked in similar questions relating to error 1064, but no joy
CREATE TABLE 'categories' (
'id' SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
'category' VARCHAR(45) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE INDEX 'category_UNIQUE' ('category' ASC)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE 'orders' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'users_id' INT UNSIGNED NOT NULL,
'transaction_id' VARCHAR(45) NOT NULL,
'payment_status' VARCHAR(45) NOT NULL,
'payment_amount' INT UNSIGNED NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
INDEX 'date_created' ('date_created' ASC),
INDEX 'transaction_id' ('transaction_id' ASC),
CONSTRAINT 'fk_orders_users1' FOREIGN KEY ('id')
REFERENCES 'users' ('id')
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE 'pages' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'categories_id' SMALLINT UNSIGNED NOT NULL,
'title' VARCHAR(100) NOT NULL,
'description' TINYTEXT NOT NULL,
'content' LONGTEXT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
INDEX 'date_created' ('date_created' ASC),
INDEX 'fk_pages_categories_idx' ('categories_id')
REFERENCES 'categories' ('id')
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE 'pdfs' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'title' VARCHAR (100) NOT NULL,
'description' TINYTEXT NOT NULL,
'tmp_name' CHAR(63) NOT NULL,
'file_name' VARCHAR(100) NOT NULL,
'size' MEDIUMINT UNSIGNED NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id')
UNIQUE INDEX 'tmp_name_UNIQUE' ('tmp_name' ASC),
INDEX 'date_created' ('date_created' ASC)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE ('users')
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'type' ENUM('member','admin') NOT NULL DEFAULT 'member'
'username' VARCHAR(45) NOT NULL,
'email' VARCHAR(80) NOT NULL,
'pass' VARCHAR(255) NOT NULL,
'first_name' VARCHAR (45) NOT NULL,
'last_name' VARCHAR (45) NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
'date_expires' DATE NOT NULL,
'date_modified' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
UNIQUE INDEX 'username_UNIQUE' ('username' ASC),
UNIQUE INDEX 'email_UNIQUE' ('email' ASC),
INDEX 'login' ('email' ASC, 'pass' ASC)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
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 ''categories' (
'id' SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
'catego' at line 1

Remove single quote characters from field names

You need to use back-ticks ` (next to the 1 key on my keyboard) instead of ' quotes. This works for me:
CREATE TABLE `categories` (
`id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`category` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `category_UNIQUE` (`category` ASC)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;

Replace ' characters that surround table names and field names by `.
There are some more syntax errors that will appear then - for example there are unneeded parentheses around 'users' table.
Also when you create 'orders' table, it has a foreign key on users, but users are declared later.

Related

Recursive Table nullable parent_id

CREATE TABLE IF NOT EXISTS `db_teamup`.`programming_languages` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
`name` VARCHAR(255) NOT NULL COMMENT '',
`count` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '',
`parent_id` INT UNSIGNED NULL COMMENT '',
`icon_path` VARCHAR(255) NOT NULL DEFAULT 'default_icon.svg' COMMENT '',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`, `parent_id`) COMMENT '',
INDEX `fk_programming_languages_programming_language_parent_idx` (`parent_id` ASC) COMMENT '',
CONSTRAINT `fk_programming_languages_programming_language_parent_id`
FOREIGN KEY (`parent_id`)
REFERENCES `db_teamup`.`programming_languages` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I am trying to make a recursive mysql table, but when i execute the script, on workbench, it sets the parent_id to not null, is there a setting or a command that i am not executing before running my script?
The worst part is that it makes the default value 0.
thank you
PRIMARY KEY ('id')
I don't think 'parent_id' should be part of your primary key. It wouldn't be able to be NULL if it was.

Errors Adding Foreign Keys (MySQL) (Error Code 1215)

I am trying to add a foreign key between two different sets of tables, the first set is customer and shopping_cart. I tried looking at the other posts regarding this error but I couldn't get it to work after looking at them.
CREATE TABLE `usale`.`customer` (
CREATE TABLE `usale`.`customer` (
`customer_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`create_date` DATETIME NOT NULL,
`last_update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`student_student_id` BIGINT NOT NULL,
`student_school_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_id`, `student_student_id`, `student_school_id`)),
INDEX `fk_customer_student1_idx` (`student_student_id` ASC,`student_school_id` ASC),
INDEX `fk_customer_shopping_cart1_idx` (`shopping_cart_cart_id` ASC),
CONSTRAINT `fk_customer_student1`
FOREIGN KEY (`student_student_id`)
REFERENCES `usale`.`student` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_customer_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart` (`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;`customer_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`create_date` DATETIME NOT NULL,
`last_update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`student_student_id` BIGINT NOT NULL,
`student_school_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_id`, `student_student_id`, `student_school_id`))
CREATE TABLE IF NOT EXISTS `frankapp`.`shopping_cart` (
`cart_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cart_total` DECIMAL(12,2) NULL,
PRIMARY KEY (`cart_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ALTER TABLE customer ADD CONSTRAINT `fk_customer_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart`(`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I'm getting the error: "ERROR: Cannot add foreign key constraint
Error Code: 1215"
For the second set of tables they are named shopping_cart and shopping_cart_item
CREATE TABLE IF NOT EXISTS `usale`.`shopping_cart_item` (
`shopping_cart_item_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
`inventory_item_inventory_item_id` BIGINT UNSIGNED NOT NULL,
`inventory_item_student_id` BIGINT NOT NULL,
`inventory_item_inventory_type` VARCHAR(45) NOT NULL,
`inventory_item_student_student_id` BIGINT NOT NULL,
`inventory_item_student_customer_id` BIGINT NOT NULL,
`inventory_item_student_school_id` BIGINT NOT NULL,
`inventory_item_edition_edition_id` BIGINT UNSIGNED NOT NULL,
`inventory_item_edition_author_id` BIGINT NOT NULL,
PRIMARY KEY (`shopping_cart_item_id`, `shopping_cart_cart_id`, `inventory_item_inventory_item_id`, `inventory_item_student_id`, `inventory_item_inventory_type`, `inventory_item_student_student_id`, `inventory_item_student_customer_id`, `inventory_item_student_school_id`, `inventory_item_edition_edition_id`, `inventory_item_edition_author_id`),
INDEX `fk_shopping_cart_items_shopping_cart1_idx` (`shopping_cart_cart_id` ASC),
INDEX `fk_shopping_cart_item_inventory_item1_idx` (`inventory_item_inventory_item_id` ASC, `inventory_item_student_id` ASC, `inventory_item_inventory_type` ASC, `inventory_item_student_student_id` ASC, `inventory_item_student_customer_id` ASC, `inventory_item_student_school_id` ASC, `inventory_item_edition_edition_id` ASC, `inventory_item_edition_author_id` ASC))
CREATE TABLE IF NOT EXISTS `frankapp`.`shopping_cart` (
`cart_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cart_total` DECIMAL(12,2) NULL,
PRIMARY KEY (`cart_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ALTER TABLE shopping_cart_item ADD CONSTRAINT `fk_shopping_cart_items_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart`(`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Once again I am getting error 1215: "ERROR: Cannot add foreign key constraint
Error Code: 1215"

Mysql gives me error while running a query

I couldn't find the problem with this query. Would you tell me what is wrong?
CREATE TABLE 'wish' (
'id' int(10) unsigned NOT NULL AUTO_INCREMENT,
'title' varchar(256) NOT NULL,
'issue_number' varchar(10) DEFAULT NULL,
'type_id' int(10) unsigned DEFAULT NULL,
'publication_date' date DEFAULT NULL,
'store_link' varchar(255) DEFAULT NULL,
'notes' text DEFAULT NULL,
'got_it' int(10) unsigned DEFAULT NULL,
PRIMARY KEY ('id'),
KEY 'type_id' ('type_id'),
KEY 'got_it' ('got_it'),
CONSTRAINT 'wish_ibfk_1' FOREIGN KEY ('type_id') REFERENCES 'type' ('id'),
CONSTRAINT 'wish_ibfk_2' FOREIGN KEY ('got_it') REFERENCES 'user' ('id')
) ENGINE=InnoDB;
Mysql gives this 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 ''wish' ( 'id' int(10) unsigned NOT NULL AUTO_INCREMENT, 'title' varchar(256)' at line 1
EDIT:
When tried the query replacing back ticks with single quotes, error occurred:
error: #1005 - Can't create table 'comic-booksdb.wish' (errno: 150)
Thank you
Use backticks(`) instead of single quote:
CREATE TABLE `wish` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(256) NOT NULL,
`issue_number` varchar(10) DEFAULT NULL,
`type_id` int(10) unsigned DEFAULT NULL,
`publication_date` date DEFAULT NULL,
`store_link` varchar(255) DEFAULT NULL,
`notes` text DEFAULT NULL,
`got_it` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `type_id` (`type_id`),
KEY `got_it` (`got_it`),
CONSTRAINT `wish_ibfk_1` FOREIGN KEY (`type_id`) REFERENCES `type` (`id`),
CONSTRAINT `wish_ibfk_2` FOREIGN KEY (`got_it`) REFERENCES `user` (`id`)
) ENGINE=InnoDB;
Side Note:
Back ticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set it is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the quoting issue.
Back ticks are necessary for situations like the following:
SELECT id, `my name`, `another field` , `field,with,comma`
EDIT
To avoid 'errno: 150' when dealing with integer data types, verify that the primary and foreign key columns of interest have the same integer types (size and sign, as indicated above). e.g. if primary key is 'unsigned int' and foreign key is simply 'int', then 'errno: 150' is likely.

Error creating table: You have an error in your SQL syntax near 'order( order_id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1 [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.
I am trying to create 2 tables in the same MySQL database with a PHP-script:
table 'user' with primary key 'user_id' and table 'order' with primary key 'order_id' and foreign key 'user_id' from the 'user' table (1 to many relationship).
Table user creates successfully without problems:
$sql="CREATE TABLE user(
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
type ENUM('member','admin') NOT NULL,
username VARCHAR(30) NOT NULL,
email VARCHAR(80) NOT NULL,
pass VARBINARY(32) NOT NULL,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
date_expires DATE NOT NULL,
date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
date_modified TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (user_id),
UNIQUE (username),
UNIQUE (email)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
However, I am not able to create table order:
$sql="CREATE TABLE order(
order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
transaction_id VARCHAR(19) NOT NULL,
payment_status VARCHAR(15) NOT NULL,
payment_amount DECIMAL(6,2) UNSIGNED NOT NULL,
payment_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (order_id),
FOREIGN KEY (user_id) REFERENCES user (user_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
I get the following error:
Error creating table: 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 UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1
Already checked the syntax and cannot find the mistake. Could you please advise what went wrong? Thanks a lot.
You need to escape reserved words like order with backticks
CREATE TABLE `order` ( ...
or better use another name instead.
order is keyword used by mysql like (select from tbl_name order by id ASC) so for escaping from using keywords you have to use quotes `` to avoid my sql error
so your query should by
$sql="CREATE TABLE `order` (
order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
transaction_id VARCHAR(19) NOT NULL,
payment_status VARCHAR(15) NOT NULL,
payment_amount DECIMAL(6,2) UNSIGNED NOT NULL,
payment_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (order_id),
FOREIGN KEY (user_id) REFERENCES user (user_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
enjoy :D

SQL Syntax error - can't find the error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
CREATE TABLE 'categories'(
'id' SMALLINT NOT NULL AUTO_INCREMENT,
'category' VARCHAR(30) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE KEY 'category' ('category')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE 'orders' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'user_id' INT UNSIGNED NOT NULL,
'transaction_id' VARCHAR(19) NOT NULL,
'payment_status' VARCHAR(15) NOT NULL,
'payment_amount' DECIMAL(6,2) UNSIGNED NOT NULL,
'payment_date_time' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
KEY 'user_id' ('user_id')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE 'pages' (
'id' MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
'category_id' SMALLINT UNSIGNED NOT NULL,
'title' VARCHAR(100) NOT NULL,
'description' TINYTEXT NOT NULL,
'content' LONGTEXT NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
KEY 'category_id' ('category_id'),
KEY 'creation_date' ('date_created')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE 'pdfs' (
'id' SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
'tmp_name' CHAR(40) NOT NULL,
'title' VARCHAR(100) NOT NULL,
'description' TINYTEXT NOT NULL,
'file_name' VARCHAR(40) NOT NULL,
'size' MEDIUMINT UNSIGNED NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
UNIQUE KEY 'tmp_name' ('tmp_name'),
KEY 'date_created' ('date_created')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE 'users' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'type' ENUM ('member','admin') NOT NULL,
'username' VARCHAR(30) NOT NULL,
'email' VARCHAR(80) NOT NULL,
'pass' VARBINARY(32) NOT NULL,
'first_name' VARCHAR(20) NOT NULL,
'last_name' VARCHAR (40) NOT NULL,
'date_expires' DATE NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
'date_modified' TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY ('id'),
UNIQUE KEY 'username' ('username'),
UNIQUE KEY 'email' ('email')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
.............................................................................
I cannot find the SQL error here.
MySQL said:
#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 ''categories' ('id' SMALLINT NOT NULL AUTO_INCREMENT, 'category' VARCHAR(30) NOT' at line 1
Please help. I am using MySQL 4.4.x on a shared server.
You're surrounding your table and column names with ', it should be ` (backtick)
CREATE TABLE `categories`
(`id` SMALLINT NOT NULL AUTO_INCREMENT,
`category` VARCHAR(30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `category` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Get rid of your single quotes or as #Joachim Isaksson said, use backticks `.
CREATE TABLE categories (id SMALLINT NOT NULL AUTO_INCREMENT, category VARCHAR(30) NOT NULL, PRIMARY KEY (id), UNIQUE KEY(category) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Also next time, simplify your problem. You have a problem with the first CREATE TABLE statement. There is no need to print everything else. Also some formatting would be nice too :-).