Table constraints - FOREIGN KEY
58.5
Exercise

FOREIGN KEY - 2

The table courses below consists of columns id, name and abbreviatedName with id as PRIMARY KEY. There’s another table called teachers, which has columns id and name, with id being the PRIMARY KEY.

Write a set of statements to:

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

    column data type constraint
    id INTEGER NOT NULL, PRIMARY KEY
    name TEXT NOT NULL
    courseId INTEGER FOREIGN KEY on id in courses
    teacherId INTEGER FOREIGN KEY on id in teachers
  2. add these record rows to the created table students, in the given order.

    id (INTEGER) name (TEXT) courseId (INTEGER) teacherId (INTEGER)
    1 Grant 4 1
    2 Henry 7 3