InfoparkDailyCAREER TOOLS

SQL · Lesson 7 of 8

Creating tables & constraints

CREATE TABLE, data types, PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT and FOREIGN KEY.

CREATE TABLE defines a table's columns and rules. Constraints protect your data:

  • PRIMARY KEY — unique id for each row.
  • NOT NULL — the value is required.
  • UNIQUE — no two rows may share the value (e.g. email).
  • DEFAULT — value used when none is given.
  • CHECK (salary_lpa > 0) — custom rule.
  • REFERENCES other(id) — a foreign key pointing to another table.

Common types: INTEGER, REAL/DECIMAL, TEXT/VARCHAR(n), DATE, BOOLEAN (names vary slightly between databases).

Try it yourself

Press Run to see the results.

Runs on a small fictional sample database (companies, jobs) that is rebuilt on every run, so you can't break anything.

Key points

  • Constraints stop bad data at the source.
  • Foreign keys link tables.
  • DEFAULT fills in missing values.

Quick check

Which constraint prevents two rows from having the same email?

Practice task

Try inserting a second applicant with the email 'anu@example.com' and read the error message.

Interview corner

What is normalization?

Organising tables to reduce duplication — for example storing company details once in companies and referencing them by id from jobs.

Primary key vs unique key?

A table has one primary key and it can't be NULL. It can have several UNIQUE constraints, and (in most databases) those columns may allow NULL.

Further reading

Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.

Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.