Registered User
Registered User

Reputation: 5361

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?

Upvotes: 0

Views: 145

Answers (2)

Sumit Gupta
Sumit Gupta

Reputation: 2192

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.

Upvotes: 2

aweis
aweis

Reputation: 5596

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; 

Upvotes: 1

Related Questions