Ollie
Ollie

Reputation: 57

mysql create a composite key with different data types

So let's say I am trying to create a table in MYSQL that contains 3 columns, Id, Name and Price. How would I go about creating a composite key between the Id and Name, given that they are different data types? I have tried the following query but am presented with an error.

CREATE TABLE name_price (
id int NOT NULL,
name Varchar NOT NULL,
price int,
PRIMARY KEY (id, name)

);

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,
    price int,
    PRIMARY KEY (id, name)
)' at line 3

Any help would be appreciated.

Upvotes: 3

Views: 2350

Answers (1)

Thorsten Kettner
Thorsten Kettner

Reputation: 94913

You are simply missing the varchar length for the name. E.g.:

CREATE TABLE name_price (
  id int NOT NULL,
  name varchar(100) NOT NULL,
  price int,
  PRIMARY KEY (id, name)
);

Upvotes: 3

Related Questions