How to use special characters in MySQL column names? - mysql

Here is my failing MySQL code:
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
'item-name' VARCHAR(255) NOT NULL,
'item-description' TEXT,
'listing-id' VARCHAR(50),
PRIMARY KEY (id)
)
The error is:
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
'item-name' VARCHAR(255) NOT NULL, 'item-description' TEXT, 'listing-id'' at line 3
The documentation says to use quotes... What is wrong?

Use ` instead of ':
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)

You should use the back-quote (`) to quote column names, not the single-quote ('). Look above the tilde key (~).
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)

Why do you use quotes? You should use backticks. Try this:
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)

Please, use normal character in table definition. You can use underscore "_" instead "-".
When you'll query your db, you must always use quote. Impossible for me! :)
If you want follow this road, use the quote
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)

maybe it is coming late. but this worked for me.
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
[item-name] VARCHAR(255) NOT NULL,
[item-description] TEXT,
[listing-id] VARCHAR(50),
PRIMARY KEY (id)
)

Related

MySQL concat with special characters [duplicate]

Here is my failing MySQL code:
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
'item-name' VARCHAR(255) NOT NULL,
'item-description' TEXT,
'listing-id' VARCHAR(50),
PRIMARY KEY (id)
)
The error is:
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
'item-name' VARCHAR(255) NOT NULL, 'item-description' TEXT, 'listing-id'' at line 3
The documentation says to use quotes... What is wrong?
Use ` instead of ':
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
You should use the back-quote (`) to quote column names, not the single-quote ('). Look above the tilde key (~).
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
Why do you use quotes? You should use backticks. Try this:
CREATE TABLE product (
id int NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
Please, use normal character in table definition. You can use underscore "_" instead "-".
When you'll query your db, you must always use quote. Impossible for me! :)
If you want follow this road, use the quote
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
`item-name` VARCHAR(255) NOT NULL,
`item-description` TEXT,
`listing-id` VARCHAR(50),
PRIMARY KEY (id)
)
maybe it is coming late. but this worked for me.
CREATE TABLE product (
id INT NOT NULL AUTO_INCREMENT,
[item-name] VARCHAR(255) NOT NULL,
[item-description] TEXT,
[listing-id] VARCHAR(50),
PRIMARY KEY (id)
)

Mariadb syntax error 1064 (42000)

So I'm getting an error when trying to run this script in MariaDB that reads as follows: "ERROR 1064 (42000):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
CREATE TABLE customers (
customer_id int NOT NULL,
customer_f
at line 1
The weird thing is that MariaDB seems to be reading the first line of the command and then a little bit of the next line as 1 line. The whole script is below: if anyone could help I would greatly appreciate it.
DROP TABLE customers;
DROP TABLE orders;
DROP TABLE products;
DROP TABLE orderitem;
​
CREATE TABLE customers (
customer_id INT NOT NULL AUTO_INCREMENT,
customer_firstname VARCHAR(20) NOT NULL,
customer_lastname VARCHAR(40) NOT NULL,
customer_phone CHAR(10) NOT NULL,
customer_email VARCHAR(60) NOT NULL,
customer_address VARCHAR(40) NOT NULL,
customer_city VARCHAR(40) NOT NULL,
customer_state CHAR(2) NOT NULL,
customer_zip VARCHAR(10) NOT NULL,
customer_aptnum VARCHAR(5) NOT NULL,
customer_pass CHAR(40) NOT NULL,
customer_type VARCHAR(10) NOT NULL,
PRIMARY KEY (customer_id),
INDEX customer_fullname (customer_firstname, customer_lastname),
UNIQUE (customer_email)
);
​
CREATE TABLE orders (
order_id INT NOT NULL AUTO_INCREMENT,
order_datetime DATETIME NOT NULL,
order_trackingnumber VARCHAR(20) NOT NULL,
order_shipdate DATETIME NOT NULL,
order_shipmethod VARCHAR(10) NOT NULL,
order_shipcarrier VARCHAR(10) NOT NULL,
order_totalprice DECIMAL,
customer_id INT NOT NULL,
PRIMARY KEY (order_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
UNIQUE (order_trackingnumber)
);
​
​
CREATE TABLE products (
product_id VARCHAR(30) NOT NULL AUTO_INCREMENT,
product_beginningstockdate DATETIME NOT NULL,
product_endstockdate DATETIME,
product_category VARCHAR(15) NOT NULL,
product_name VARCHAR(60) NOT NULL,
product_availablequantity SMALLINT NOT NULL,
product_totalquantity SMALLINT NOT NULL,
product_price DECIMAL NOT NULL,
product_taxable DECIMAL NOT NULL,
product_itemstatus VARCHAR(15) NOT NULL,
product_discountpercent DECIMAL,
product_soldinstore char(3),
product_soldonwebsite char(3),
PRIMARY KEY (product_id),
UNIQUE (product_name)
);
​
/*INSERT INTO products (product_description, product_beginningstockdate, product_endstockdate, product_category, product_name, product_availablequantity, product_totalquantity, product_price, product_taxable, product_itemstatus, product_discountpercent, product_soldinstore, product_soldonwebsite)
VALUES
(...),
(...),
........ */
​
CREATE TABLE orderitem (
orderitem_id INT NOT NULL AUTO_INCREMENT,
order_id INT NOT NULL,
product_id VARCHAR(30) NOT NULL,
orderitem_priceperunit DECIMAL NOT NULL,
orderitem_quantityordered TINYINT NOT NULL,
PRIMARY KEY (orderitem_id),
FOREIGN KEY (order_id) REFERENCES orders(order_id),
FOREIGN KEY (product_id) REFERENCES orders(product_id)
);
Copy&pasting your code into NotePad++ and then viewing it in the hex editor shows that you have 80 8b 0a in each of those empty lines between the statements.
That byte sequence is the UTF-8 encoded form of the zero-width space character.
See to it that you remove those – then it should work.
(If you’re using NotePad++ and the hex editor plugin, then in hex view you can simply replace e2 80 8b with an empty string. Otherwise, in any other text editor, going to the end of the previous line, selecting everything from there over the empty line until the beginning of the next line, and then replacing the selection by pressing enter should also work.)
I had a similar frustration and discovered I needed to change my delimiter around my procedure. Perhaps it'll work here too.
DELIMITER //
CREATE PROCEDURE
....
BEGIN
END //
DELIMITER ;

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;

MySQL Syntax errors

CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
),
I have this as part of a database I'm setting up, yet whenever I try to run the .sql file that this is included in, 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 '
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(' at line 8
Any ideas what's wrong? Thanks.
Remove the , from the end CREATE TABLE statement
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)
Or use semicolon instead comma:
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
);
You have comma at the end if you remove that it is working fin for me.
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)
We don't use "," at the end to separate two sql queries, you have to use ";" and if you are running a single query then you don't need.
CREATE TABLE Customer(
custID INT NOT NULL AUTO_INCREMENT,
custName VARCHAR(255) NOT NULL,
custAddress VARCHAR(255) NOT NULL,
CONSTRAINT pk_Customer PRIMARY KEY (custID)
)