Data definition language & constraints

Data Definition Language

Peer instruction

What result would the following query obtain?

image-20240923104841433

A)

image-20240923105127973

B)

image-20240923105224770

C)

image-20240923105026786

What result would the following query obtain?

image-20240923105419498

A)

image-20240923105438226

B)

image-20240923105654619

C)

Error

What result would the following query obtain?

image-20250929094421338

A)

image-20250929110747757

B)

image-20250929110803411

C)

Error

Example: Two tables with primary keys and foreign key constraints

Exercise 1

Reference: https://www.mysqltutorial.org/mysql-basics/mysql-check-constraint/

Implement the following constraints in the departments and employees DDL script

  • A department number should auto increment from 1 to n.

  • A department name and location cannot be null.

  • An employee must be hired after 1980.

  • An employee cannot have a negative salary, but they can receive 0 (In the case of an intern).

  • The default value for an employees manager is 7839.

  • If a department number in the departments table is changed, it is also changed for the relevant employees in the employee table.

Create a new table projects:

  • Each project has an auto-incrementing project id, a project name (not null), a budget (must be greater than 1000), and a department number.

  • Ensure that if a department is deleted, all its projects are also deleted automatically.

Try to insert two rows for each of the constraints / rules. One valid and one invalid.

Verify that all your constraints work as intended.

Exercise 2

In this exercise you will create a script such as this (or use Workbenchs tools):

The script has to drop all tables if they exists, create new tables and insert test data.

Requirements

Create a table named "students" with the following columns:

  • student_id (INT, primary key)

  • first_name (VARCHAR, 50)

  • last_name (VARCHAR, 50)

  • birthdate (VARCHAR,10)

  • enrollment_date (DATETIME)

  • status (VARCHAR, 15)

Modify the "students" table by adding a new column named email of type VARCHAR(100).

Modify a Column Change such that the data type of the birthdate column in the "students" table to DATE.

Create a new table named "courses" with the following columns:

  • course_id (INT, primary key)

  • course_name (VARCHAR, 100)

  • instructor_id (INT, foreign key referencing an "instructors" table)

    • An instructor has a name, email & auto incrementing id

Add a default value of 'Active' for the status column in the "students" table.

Create a table named "books" with the following columns:

  • book_id (INT, primary key, auto incrementing)

  • title (VARCHAR, 100)

  • author (VARCHAR, 100)

  • isbn (VARCHAR, 13, unique)

Populate the tables with at least 5 entities for each table.

(Advanced optional)

  • Modify the database such that 1 book is used on each course.

Last updated