learning sql creating table gives error - mysql

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;

Related

where is my error in this area, can you edit the code?

DROP TABLE IF EXISTS lang;
CREATE TABLE lang (
ID int( 11 ) NOT NULL auto_increment,
key varchar(255) NOT NULL,
code varchar(8) NOT NULL,
value text NOT NULL,
PRIMARY KEY (ID),
KEY key (key)
);
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 'varchar(255) NOT NULL, code varchar(8) NOT NULL,
value text NOT NULL, ' at line 3
key is a reserved word in mysql it must be escaped:
DROP TABLE IF EXISTS lang;
CREATE TABLE lang (
ID int( 11 ) NOT NULL auto_increment,
`key` varchar(255) NOT NULL,
code varchar(8) NOT NULL,
value text NOT NULL,
PRIMARY KEY (ID),
KEY `key` (`key`)
);
http://sqlfiddle.com/#!9/3b6cd5

MYSQL error #1072 - Key column 'id ' doesn't exist in table in XAMPP, phpmyadmin

I am trying to create a new table using the mysql GUI in phpmyadmin. This is the error I am getting #1072 - Key column 'id ' doesn't exist in table
CREATE TABLE `loginsys`.`groups`(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`permission` TEXT NOT NULL,
PRIMARY KEY(`id `)
) ENGINE = InnoDB
Please correct the extra space in when you're defining PRIMARY KEY(id )
This should be your query :-
CREATE TABLE `loginsys`.`groups`(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`permission` TEXT NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB
simply run the query without "`"
CREATE TABLE loginsys.groups(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
permission TEXT NOT NULL,
PRIMARY KEY(id))

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 ')'

I am using Sequel Pro with MySQL on my Mac. I return the 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 ')' " every time I attempt to run this sql query. The Database is created successfully, but the table is not. Can anyone advise where the error in my syntax is? I simply do not see it:
CREATE DATABASE bamazon
USE bamazon;
CREATE TABLE products
(
`id` int NOT NULL AUTO_INCREMENT,
`product_name` varchar(45) NOT NULL,
`department_name` varchar(45) NOT NULL,
`price` int NOT NULL,
`stock_quantity` int NOT NULL,
PRIMARY KEY (id),
);
Please remove the ',' after PRIMARY KEY (id). It should be like this
CREATE TABLE products
(
id int NOT NULL AUTO_INCREMENT,
product_name varchar(45) NOT NULL,
department_name varchar(45) NOT NULL,
price int NOT NULL,
stock_quantity int NOT NULL,
PRIMARY KEY (id)
);
The query does not work because there is an extra comma (,) at the end. Remove that and it will work. The correct syntax will be:
CREATE TABLE products
(
id int NOT NULL AUTO_INCREMENT,
product_name varchar(45) NOT NULL,
department_name varchar(45) NOT NULL,
price int NOT NULL,
stock_quantity int NOT NULL,
PRIMARY KEY (id)
);

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.

ERROR 1064 (42000): You have an error in your SQL syntax;

I have a MySQL commands:
CREATE DATABASE IF NOT EXISTS courses;
USE courses
CREATE TABLE IF NOT EXISTS teachers(
id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VAR_CHAR(50) NOT NULL,
addr VAR_CHAR(255) NOT NULL,
phone INT NOT NULL,
);
When I run it, I get an 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 'VAR_CHAR(50) NOT NULL, addr VAR_CHAR(255) NOT
NULL, phone INT NOT NULL, )' at line 3
It is varchar and not var_char
CREATE DATABASE IF NOT EXISTS courses;
USE courses;
CREATE TABLE IF NOT EXISTS teachers(
id INT(10) UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
addr VARCHAR(255) NOT NULL,
phone INT NOT NULL
);
You should use a SQL tool to visualize possbile errors like MySQL Workbench.
Try this:
Use back-ticks for NAME
CREATE TABLE `teachers` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`addr` varchar(255) NOT NULL,
`phone` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Use varchar instead of VAR_CHAR and omit the comma in the last line i.e.phone INT NOT NULL
);. The last line during creating table is kept "comma free".
Ex:- CREATE TABLE COMPUTER
(
Model varchar(50)
);
Here, since we have only one column ,that's why there is no comma used during entire code.