Jurij Jascha Albegger Jurij Jascha Albegger - 1 month ago 8
MySQL Question

I've an error in the following simple MySQL Create Table statement

There is a syntax error in the follwing MySQL Script:

USE supermarket;
CREATE TABLE products(
ISBN int AUTO_INCREMENT,
name varchar(15) UNIQUE,
priceInEuro double(2) NOT NULL,
sales double(2) NOT NULL default(0),
description varchar(100),
PRIMARY KEY(ISBN)
);


Error:

/* SQL 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 ') NOT NULL,
sales double(2) NOT NULL default(0),
description varchar(100),
PR' at line 4 */
/* Affected rows: 0 Found rows: 0 Warnings: 0 Duration for 1 of 2
queries: 0.000 sec. */


Please answer!

Answer

You have two errors in your statement:

  1. the DOUBLE type requires two parameters (documenatation):

    DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]

  2. the DEFAULT keyword requires no parenthesis.

Correcting both mistakes results in the following statement:

CREATE TABLE products(
  ISBN int AUTO_INCREMENT,
  name varchar(15) UNIQUE,
  priceInEuro double(5,2) NOT NULL,
  sales double(5,2) NOT NULL default 0,
  description varchar(100),
  PRIMARY KEY(ISBN)
)