0

I am new to MySQL and am messing with a database I created just to learn.

I created my first table and it worked OK:

mysql> CREATE TABLE Pizzas(ItemID INTEGER PRIMARY KEY, Size INTEGER, Price DECIMAL);
Query OK, 0 rows affected (0.90 sec)

then I tried to create another table using CONSTRAINT (I was simply copying the way foreign keys were created in a class I am taking) and I got an error:

mysql> CREATE TABLE Customers(CustomerID PRIMARY KEY, Name CHAR(20), ItemID INTEGER, CONSTRAINT FOREIGN KEY (ItemID) REFERENCES Pizzas (ItemID));

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 'PRIMARY KEY, Name CHAR(20), ItemID INTEGER, CONSTRAINT FOREIGN KEY (ItemID) REFE' at line 1

I'm using a linux computer and my MySQL version is 5.5. Why am I getting this problem?

1
  • 1
    You don't have a data type (for example INTEGER) for CustomerID. Also, some advice: use VARCHAR(20) instead of CHAR(20) for the Name column, as CHAR(20) will be padded with spaces at the end. You won't see them, but they'll be there and they'll mess you up. Commented Oct 2, 2014 at 18:44

1 Answer 1

2

your table definition is not correct. you are missing the datatype in CustomerID PRIMARY KEY. It should be

CREATE TABLE Customers(CustomerID int not null PRIMARY KEY,
                                  <-- Here 
Name CHAR(20), ItemID INTEGER, 
CONSTRAINT FOREIGN KEY (ItemID) REFERENCES Pizzas (ItemID));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.