Question

Is there something wrong with my syntax?

Posted October 30, 2014 2.4k views

Below is my script to create a table. I keep getting a syntax error.

CREATE TABLE products (
id int(11) NOT NULL autoincrement,
product
name varchar(255) NOT NULL,
price varchar(16) NOT NULL,
details text NOT NULL,
stock int() NOT NULL,
category varchar(16) NOT NULL,
subcategory varchar(16) NOT NULL,
dateadded date NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY product
name (product_name)
) ;

The is the syntax error I get.
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, category varchar(16) NOT NULL, subcategory varchar(16) N’ at line 6.

Can someone please tell me what I am doing wrong. I don’t see it. And reading the error message obviously isn’t helping.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Here’s the correct code:

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `productname` varchar(255) NOT NULL,
  `price` varchar(16) NOT NULL,
  `details` text NOT NULL,
  `stock` int(11) NOT NULL,
  `category` varchar(16) NOT NULL,
  `subcategory` varchar(16) NOT NULL,
  `dateadded` date NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `productname` (`productname`)
);

stock column needs a length

(I presume the other answerer is pointing out the same thing..)

Submit an Answer