Column constraints - DEFAULT and CHECK
55.4
Exercise

CHECK Constraint

Write a set of SQL statements together to:

  1. create a new table courses with the following columns, data types and constraints.

    column data type constraint
    id INTEGER UNIQUE, NOT NULL
    name TEXT UNIQUE, NOT NULL
    abbreviation TEXT Length should not be more than 3 characters
    type TEXT Should be either 'Language' or 'Framework'
  2. add these record rows to the created table courses

    id (INTEGER) name (TEXT) abbreviation (TEXT) type (TEXT)
    1 Ruby rb Language
    2 Rails rls Framework

Hint:

  1. To ensure a column name should have values which are of length less than a value n, simply include the expression LENGTH(name) < n in the CHECK constraint.

  2. To ensure a column name contains values which are either a, b or c, simply include the expression name IN ('a', 'b', 'c') in the CHECK constraint.