Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 678
Ollie Avatar asked Jun 07 '26 08:06

Ollie


1 Answers

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)
);
like image 174
Thorsten Kettner Avatar answered Jun 10 '26 02:06

Thorsten Kettner