Skip to content
Ameen Abozaid
Planned lessonJNR-034-L02

Unique and check constraints

  • Lesson 2 of 5
  • Junior
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

write the main constraint types.

Planned topics

  • not null: the value must exist.
  • unique: no two rows share the value (or combination).
  • foreign key: the referenced row must exist; choose the delete behavior on purpose.
  • check: a rule on the row (price >= 0).
  • Name constraints (ux_product_sku) so error messages and handlers are clear.
Draft outline details

Demonstration

sql
alter table product
  add constraint ux_product_sku unique (sku),
  add constraint ck_product_price check (price >= 0);
alter table order_line
  add constraint fk_line_order foreign key (order_id) references orders(id);

Check your understanding

Which constraint stops a negative price?

Show answer

A check constraint.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.