CodeBlanks Logo Icon CodeBlanks

Column definitions

Learn to give a column name and type.

PRIMARY KEY

With PRIMARY KEY you make a column the primary key. Its value is unique and cannot be null.

CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(100)
);

Id is the primary key; unique and not null.

The table products is created.

PRIMARY KEY — details

Each table has at most one primary key. It creates an index automatically. It is usually given to the id column.

Practice PRIMARY KEY

Create customers with id INT PRIMARY KEY and name VARCHAR(100).

Starter code

query.sql
-- create table
CREATE TABLE customers

Sample solution

query.sql
CREATE TABLE customers (
  id INT PRIMARY KEY,
  name VARCHAR(100)
)

Expected output

Tablo oluşturuldu

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Primary key

Question
CREATE TABLE products (
  id INT ____,
  name VARCHAR(100)
);
Answer
CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(100)
);

Explanation: PRIMARY KEY is the right answer.

2 Which two properties does a PRIMARY KEY have?

  • Unique and not null Correct answer
  • Empty and repeated
  • Numbers only
  • Text only

Explanation: PRIMARY KEY is unique and NOT NULL.

3 Column definitions: match the terms

Term Meaning
PRIMARY KEY primary
UNIQUE unique
NOT NULL not null
INT integer

All lessons in Beginner