Certainly! Below are the SQL CREATE TABLE statements for the Ecole schema in PostgreSQL, based on the provided specifications. These statements define the necessary tables, their columns with appropriate data types, primary keys (PK), and foreign keys (FK) to establish relationships between tables.
1. Create the Schema
First, create the Ecole schema if it doesn't already exist:
CREATE SCHEMA IF NOT EXISTS Ecole;
2. Create the Tables
It's important to create the tables in an order that respects their dependencies. Here's the recommended sequence:
- Departement
- Enseignant
- Etudiant
- Matiere
- Matiere_Etudiant
a. Departement Table
CREATE TABLE Ecole.Departement (
Code INTEGER PRIMARY KEY,
Nom VARCHAR(100) NOT NULL,
Domaine VARCHAR(100) NOT NULL
);
- Code: Primary Key.
- Nom: Name of the department.
- Domaine: Domain or field of the department.
b. Enseignant Table
CREATE TABLE Ecole.Enseignant (
ID INTEGER PRIMARY KEY,
DepartementCode INTEGER NOT NULL,
Prenom VARCHAR(100) NOT NULL,
Nom VARCHAR(100) NOT NULL,
Courriel VARCHAR(100) UNIQUE NOT NULL,
CONSTRAINT fk_enseignant_departement
FOREIGN KEY (DepartementCode)
REFERENCES Ecole.Departement(Code)
ON UPDATE CASCADE
ON DELETE RESTRICT
);
- ID: Primary Key.
- DepartementCode: Foreign Key referencing
Departement(Code).
- Prenom: First name of the teacher.
- Nom: Last name of the teacher.
- Courriel: Email address (unique).
Constraints:
- Foreign Key (
DepartementCode): Ensures that each teacher is associated with a valid department.
- ON UPDATE CASCADE: Updates
DepartementCode in this table if it changes in Departement.
- ON DELETE RESTRICT: Prevents deletion of a department if it's referenced by a teacher.
c. Etudiant Table
CREATE TABLE Ecole.Etudiant (
ID INTEGER PRIMARY KEY,
Prenom VARCHAR(100) NOT NULL,
Nom VARCHAR(100) NOT NULL,
Courriel VARCHAR(100) UNIQUE NOT NULL
);
- ID: Primary Key.
- Prenom: First name of the student.
- Nom: Last name of the student.
- Courriel: Email address (unique).
d. Matiere Table
CREATE TABLE Ecole.Matiere (
ID INTEGER PRIMARY KEY,
EnseignantID INTEGER NOT NULL,
DepartementCode INTEGER NOT NULL,
Nom VARCHAR(100) NOT NULL,
Duree INTEGER NOT NULL,
CONSTRAINT fk_matiere_enseignant
FOREIGN KEY (EnseignantID)
REFERENCES Ecole.Enseignant(ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT fk_matiere_departement
FOREIGN KEY (DepartementCode)
REFERENCES Ecole.Departement(Code)
ON UPDATE CASCADE
ON DELETE RESTRICT
);
- ID: Primary Key.
- EnseignantID: Foreign Key referencing
Enseignant(ID).
- DepartementCode: Foreign Key referencing
Departement(Code).
- Nom: Name of the subject.
- Duree: Duration of the subject (e.g., hours).
Constraints:
- Foreign Key (
EnseignantID): Associates the subject with a teacher.
- Foreign Key (
DepartementCode): Ensures the subject belongs to a valid department.
- ON DELETE SET NULL: If a teacher is deleted, sets
EnseignantID to NULL (ensure the column is nullable if this is desired).
Note: If you want to prevent a subject from existing without an associated teacher, consider using ON DELETE RESTRICT instead of SET NULL and ensure EnseignantID is NOT NULL.
e. Matiere_Etudiant Table
CREATE TABLE Ecole.Matiere_Etudiant (
MatiereID INTEGER NOT NULL,
EtudiantID INTEGER NOT NULL,
Note INTEGER,
PRIMARY KEY (MatiereID, EtudiantID),
CONSTRAINT fk_matiere_etudiant_matiere
FOREIGN KEY (MatiereID)
REFERENCES Ecole.Matiere(ID)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT fk_matiere_etudiant_etudiant
FOREIGN KEY (EtudiantID)
REFERENCES Ecole.Etudiant(ID)
ON UPDATE CASCADE
ON DELETE CASCADE
);
- MatiereID: Foreign Key referencing
Matiere(ID).
- EtudiantID: Foreign Key referencing
Etudiant(ID).
- Note: Grade or score of the student in the subject.
Constraints:
- Primary Key: Composite key (
MatiereID, EtudiantID) ensures that each student-subject pair is unique.
- Foreign Keys:
MatiereID: Links to the subject.
EtudiantID: Links to the student.
- ON DELETE CASCADE: Deleting a subject or student will automatically remove related records in this table.
3. Complete SQL Script
For your convenience, here's the complete SQL script combining all the above statements:
-- 1. Create Schema
CREATE SCHEMA IF NOT EXISTS Ecole;
-- 2a. Create Departement Table
CREATE TABLE Ecole.Departement (
Code INTEGER PRIMARY KEY,
Nom VARCHAR(100) NOT NULL,
Domaine VARCHAR(100) NOT NULL
);
-- 2b. Create Enseignant Table
CREATE TABLE Ecole.Enseignant (
ID INTEGER PRIMARY KEY,
DepartementCode INTEGER NOT NULL,
Prenom VARCHAR(100) NOT NULL,
Nom VARCHAR(100) NOT NULL,
Courriel VARCHAR(100) UNIQUE NOT NULL,
CONSTRAINT fk_enseignant_departement
FOREIGN KEY (DepartementCode)
REFERENCES Ecole.Departement(Code)
ON UPDATE CASCADE
ON DELETE RESTRICT
);
-- 2c. Create Etudiant Table
CREATE TABLE Ecole.Etudiant (
ID INTEGER PRIMARY KEY,
Prenom VARCHAR(100) NOT NULL,
Nom VARCHAR(100) NOT NULL,
Courriel VARCHAR(100) UNIQUE NOT NULL
);
-- 2d. Create Matiere Table
CREATE TABLE Ecole.Matiere (
ID INTEGER PRIMARY KEY,
EnseignantID INTEGER NOT NULL,
DepartementCode INTEGER NOT NULL,
Nom VARCHAR(100) NOT NULL,
Duree INTEGER NOT NULL,
CONSTRAINT fk_matiere_enseignant
FOREIGN KEY (EnseignantID)
REFERENCES Ecole.Enseignant(ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT fk_matiere_departement
FOREIGN KEY (DepartementCode)
REFERENCES Ecole.Departement(Code)
ON UPDATE CASCADE
ON DELETE RESTRICT
);
-- 2e. Create Matiere_Etudiant Table
CREATE TABLE Ecole.Matiere_Etudiant (
MatiereID INTEGER NOT NULL,
EtudiantID INTEGER NOT NULL,
Note INTEGER,
PRIMARY KEY (MatiereID, EtudiantID),
CONSTRAINT fk_matiere_etudiant_matiere
FOREIGN KEY (MatiereID)
REFERENCES Ecole.Matiere(ID)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT fk_matiere_etudiant_etudiant
FOREIGN KEY (EtudiantID)
REFERENCES Ecole.Etudiant(ID)
ON UPDATE CASCADE
ON DELETE CASCADE
);
4. Additional Recommendations
-
Data Integrity: Ensure that the foreign key constraints align with your data integrity requirements. For example, decide whether deleting a department should restrict deletion if teachers are associated or cascade the deletion.
-
Indexes: Consider adding indexes on foreign key columns (DepartementCode, EnseignantID, etc.) to improve query performance, especially for large datasets.
-
Serial IDs: If you want PostgreSQL to automatically generate IDs for Enseignant, Departement, Matiere, and Etudiant, you can use SERIAL or GENERATED AS IDENTITY instead of manually assigning INTEGER. For example:
ID SERIAL PRIMARY KEY
or
ID INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
-
Constraints on Matiere.EnseignantID: If EnseignantID should never be NULL, ensure that the ON DELETE behavior aligns with your requirements. Using SET NULL will require the column to be nullable, which conflicts with NOT NULL.
5. Example Adjustments for Matiere.EnseignantID
If you prefer that a subject must always have an associated teacher and prevent deletion of a teacher who is assigned to a subject, modify the foreign key constraint as follows:
CONSTRAINT fk_matiere_enseignant
FOREIGN KEY (EnseignantID)
REFERENCES Ecole.Enseignant(ID)
ON UPDATE CASCADE
ON DELETE RESTRICT
And ensure that the EnseignantID column is NOT NULL:
EnseignantID INTEGER NOT NULL,
6. Applying the Script
To execute the script:
- Connect to PostgreSQL using your preferred client (e.g.,
psql, pgAdmin).
- Run the script in the SQL query interface.
- Verify the tables and constraints have been created as intended.
7. Verification
After running the script, you can verify the creation of tables and their constraints using the following SQL queries:
-
List Tables:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'Ecole';
-
Check Columns and Constraints:
SELECT
table_name,
column_name,
data_type,
is_nullable,
column_default
FROM
information_schema.columns
WHERE
table_schema = 'Ecole'
ORDER BY
table_name,
ordinal_position;
-
View Foreign Keys:
SELECT
tc.table_name,
kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name,
tc.constraint_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE
tc.constraint_type = 'FOREIGN KEY' AND
tc.table_schema = 'Ecole';
This comprehensive setup should provide a robust foundation for your Ecole database schema in PostgreSQL.