Worked example 1
Write a DDL statement to create a table named Tutor to store information about school tutors. The table should include a unique ID, the tutor's first name, last name, and subject. The ID should be the primary key, and the names cannot be empty. Define appropriate data types.
Show solution outline
CREATE TABLE Tutor (
TutorID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Subject VARCHAR(100)
);
Explanation:
- CREATE TABLE Tutor (...): This is the DDL command to create a new table named Tutor.
- TutorID INT PRIMARY KEY: Defines a column TutorID of type Integer and sets it as the PRIMARY KEY. This ensures every tutor has a unique, non-null ID.
- FirstName VARCHAR(50) NOT NULL: Defines a column for the first name, allowing up to 50 characters. The NOT NULL constraint ensures this field must have a value.
- LastName VARCHAR(50) NOT NULL: Same as above for the last name.
- Subject VARCHAR(100): Defines a column for the subject, allowing up to 100 characters. This can be left NULL if a tutor's subject is not yet assigned.