In case you haven't finished the previous excercises, do that before starting up with these.
Last time we transformed the ER-diagrams to relations and defined the fields. This time we'll create the actual relations using SQL.
CREATE TABLE Postal_code (
Postal_code CHAR(5) NOT NULL,
Town VARCHAR(64) NOT NULL
)
The names of the fields, the datatypes and wether they are required or not are already written down in Excel.
If the field is not required just skip the definition NOT NULL.
CREATE TABLE Postal_code (
Postal_code CHAR(5) NOT NULL,
Town VARCHAR(64) NOT NULL,
CONSTRAINT Postal_code_PK
PRIMARY KEY (Postal_code)
)
CONSTRAINT gives the primary key an unique name which is used when if refer to it later on.
Try running the code again and see if the table appears inTables.
CREATE TABLE Faculty (
FacultyID INTEGER NOT NULL,
Name VARCHAR(64) NOT NULL,
CONSTRAINT Faculty_PK
PRIMARY KEY (FacultyID)
)
Run the SQL-code. Save it as 02_Create_Faculty.
CREATE TABLE Department (
DepartmentID INTEGER NOT NULL,
Name VARCHAR(64) NOT NULL,
Faculty INTEGER NOT NULL,
CONSTRAINT Department_PK
PRIMARY KEY (DepartmentID)
)
Save the code as 03_Create_Department.
CREATE TABLE Department (
DepartmentID INTEGER NOT NULL,
Name VARCHAR(64) NOT NULL,
Faculty INTEGER NOT NULL,
CONSTRAINT Department_PK
PRIMARY KEY (DepartmentID),
CONSTRAINT Department_FK_F
FOREIGN KEY (Faculty)
REFERENCES Faculty (FacultyID)
)
Make sure it works.
CREATE TABLE Department (
DepartmentID INTEGER NOT NULL,
Name VARCHAR(64) NOT NULL,
Faculty INTEGER NOT NULL,
CONSTRAINT Department_PK
PRIMARY KEY (DepartmentID),
CONSTRAINT Department_FK_F
FOREIGN KEY (Faculty)
REFERENCES (FacultyID)
ON UPDATE CASCADE
ON DELETE NO ACTION
)
It doesn't, however, work in Access so you have to skip the cascades now and define them later by
the other tools in Access.
CREATE TABLE Student (
ID CHAR(11) NOT NULL,
Email VARCHAR(64),
First_name VARCHAR(32) NOT NULL,
Last_name VARCHAR(64) NOT NULL,
Street VARCHAR(64) NOT NULL,
Postal_code CHAR(5) NOT NULL,
Starting_year SMALLINT NOT NULL,
Department INTEGER NOT NULL,
CONSTRAINT Sturent_PK
PRIMARY KEY (ID),
CONSTRAINT Student_FK_D
FOREIGN KEY (Department)
REFERENCES Department (DepartmentID),
CONSTRAINT Student_FK_P
FOREIGN KEY (Postal_code)
REFERENCES Postal_code (Postal_code)
)
Save the code as 04_Create_Student and check if it works.
If you have a plenty of time and enthusiasm left, go on and create the soccer database using SQL.