Primary keys - the basics
Single-column primary keys
9. Primary key data type
Multicolumn primary keys
Auto-increment columns and sequences
Revision

Instruction

Excellent! Now, you may wonder if the primary key must always be an integer column. The answer is no. You can choose other column types as well. For instance, you could give up the id column in our movie table and use the title of the movie as the primary key.

Are such solutions popular? Well, not really. Such a primary key will work more slowly and is more difficult to handle.

Exercise

Let's experiment. Create the table movie WITHOUT the id column. Put the following columns:

  • title which is a field of up to 64 characters and the primary key,
  • year which is an integer and
  • genre which is a text of up to 10 characters.

Stuck? Here's a hint!

Type

CREATE TABLE movie (
   title varchar(64) PRIMARY KEY,
   year int,
   genre varchar(10)
);

Console

Code editor

Result

TableConsole