Primary keys - the basics
2. Primary keys - Syntax
Single-column primary keys
Multicolumn primary keys
Auto-increment columns and sequences
Revision

Instruction

Okay, have you noticed the new element in the code? You should be familiar with the instruction:

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

It will create a table called movie with the columns specified in the parentheses. There, we provide column names with their types. However, after the part id int, we put the following piece of information:

PRIMARY KEY

In this part of the course, we're going to examine primary keys closely. What are they? A primary key is a set of columns which is used to identify a given row in the table. Sounds too vague? Don't worry, we're going to explain that soon!

Exercise

We've put some information into the table movie. Take a close look at the contents of the table.

Examine the column id which is the primary key. Each id is unique - the same number only appears once.

Apart from that, we do not have id 7. That's correct - primary keys do not need to follow any specific order. If number 7 is missing, nothing particular happens.

Console

Code editor

Result

TableConsole