SEC S20W1 || Databases and SQL language - Part 1

mrsokal -

Good night friends, today is Friday 29th Bhadra 1431 A.D., 13th September 2024 A.D., 8th Rabiul Awal 1446 A.H., how are you all, hope everyone is well and having a very nice day. I want to thank @kouba01 to organize this course in SEC season 20 which is named "Databases and SQL language" and today i am participating on the first part. I would also like to invite @sifat420, @yoyopk and @baizid123 to participate in this post.

Image designed in canva

Exercise 01

For each of the following statements, validate each proposition by putting the letter V if it is correct or F if it is incorrect.

A column corresponds to:

Justification: A column maps to an element property of a real-world object as it is the basic vital thing or attribute of that object. This is also an approach to reduce the complexity of real-time objects into a simple and organized data.


Mark the letter V in front of the correct word to complete the following sentence:
A ……….. is a set of data related to the same subject (or entity) and structured in a table format.

Justification: Table is a collection of data that fall under the same subject (or entity), formatted as a table due to table being how we logically organise and display information.


Among the following propositions, which correspond to a primary key?

Justification: A primary key must be unique for each record, ensuring no duplicates and its main role is to uniquely identify and retrieve a specific record. The third one doesn't relate to a primary key, as the primary key applies to all records, not just the first one.


In SQL, to ensure data integrity in a child table after modifying the primary key value of a parent table, we use the clause:

Justification: When a parent table's primary key values change, the foreign key values in the child table will automatically update to reflect those changes while maintaining referential integrity which is known as ON UPDATE CASCADE.


Integrity constraints on a column are:

Justification: PRIMARY KEY ensures unique, non-null values. CHECK applies the specified conditions to the column values. FOREIGN KEY ensures values correspond to values in another table. REFERENCES is used with FOREIGN KEY to specify the linked table and column.


In SQL, which clauses placed after the data type of a field ensure that the content of this field is unique?

Justification: PRIMARY KEY ensures uniqueness and non-null values. UNIQUE ensures all values are unique but allows one null (if supported).


In SQL, the Drop Table command allows:

Justification: DROP TABLE removes the full table, including its data, structure, and constraints, from the database and does not just remove only rows or columns.


Exercise 02

To describe the employees of a company and their distribution among different functions, the following table has been created:

codempnomempprenempdnempniveaucodfctintfct
E001TOUNSISafa1980-01-10Master20Finance
E002KEFIAli1981-09-12High School10Administrative
E003BEJIMohamed1977-04-15High School20Finance
E004TOUATILamia1965-06-21Technician20Security
E005SOUSSILeila1982-11-28Master10Administrative
E006SFAXILotfi1988-06-09Master30Legal

Description of Columns

ColumnDescriptionColumnDescription
codempEmployee codednempEmployee's date of birth
nomempEmployee's last nameniveauRecruitment level
prenempEmployee's first namecodfctFunction code
intfctFunction title

This table has been copied from the professor @kouba01's post.


Anomalies in the Current Structure:

The table provided by the professor to us has various anomalies like data redundancy, update anomalies as well as insertion and deletion anomalies. These anomalies are given below:

Redundancy Anomalies: The fields intfct (function title) and codfct (function code) are repeated for every employee in the same function. For example, employees in the "Finance" function have the same values for both intfct and codfct. This redundancy leads to unnecessary data duplication.

Update Anomaly: If the function title or code changes (e.g., renaming "Finance" to "Accounting"), we would need to update multiple rows where the function is assigned. This could lead to inconsistent data if one row is updated but others are missed.

Insert Anomaly: If we want to add a new function but there are no employees yet, we cannot insert the function details (function title and code) because there would be no employee record to associate it with.

Delete Anomaly: If the last employee in a function leaves the company and their record is deleted, the information about that function (its title and code) would be lost since it's tied to the employee records.


Textual representation of this database with R1 and R2 applied

The provided anomalies in various tables will be normalized in accordance with R1 and R2 criteria. Following the application of R1 and R2 management rules, the texture representation in various tables is as follows.

Table: Employee
codemp: Employee code (Primary Key)
nomemp: Employee's last name
prenom: Employee's first name
demp: Employee's date of birth
niveau: Recruitment level (Foreign Key)

Table: Function
codfct: Function code (Primary Key)
intfct: Function title

Table: Employee_Function
codemp: Employee code (Foreign Key)(Primary Key)
codfct: Function code (Foreign Key)(Primary Key)


Explanation:



Textual representation of this database with R1 and R2 applied

Table: Employee
codemp: Employee code (Primary Key)
nomemp: Employee's last name
prenom: Employee's first name
demp: Employee's date of birth
niveau: Recruitment level (Foreign Key)

Table: Function
codfct: Function code (Primary Key)
intfct: Function title

Table: Employee_Function
codemp: Employee code (Foreign Key)(Primary Key)
codfct: Function code (Foreign key)(Primary Key)
start_date: Start date of the assignment(Primary Key)
end_date: End date of the assignment (It can be null)


Explanation:



Textual representation of this database with R1 and R2 applied

Table: Bonus
codemp: Employee code (Foreign Key)(Primary Key)
anneePrime: Year of the performance bonus(Primary Key)
primeRend: Value of the performance bonus(Primary Key)


Explanation:



Exercise 03

Create the "GestElections" database

CREATE DATABASE GestElections;
USE GestElections;


Explanation:



Create the various tables in the "GestElections" database

Electeur Table:

CREATE TABLE Electeur (
NumElv INT AUTO_INCREMENT PRIMARY KEY,
LastName VARCHAR(15) NOT NULL,
FirstName VARCHAR(15) NOT NULL,
AgeElv TINYINT CHECK (AgeElv BETWEEN 12 AND 20)
);


Explanation:



Liste Table:


CREATE TABLE Liste (
NumList INT PRIMARY KEY,
DesList VARCHAR(20) NOT NULL UNIQUE,
NbrCand TINYINT CHECK (NbrCand BETWEEN 1 AND 6)
);


Explanation:



Candidate Table:

CREATE TABLE Candidat (
NumCand INT PRIMARY KEY,
NumList INT NOT NULL,
Appelation VARCHAR(30) NOT NULL,
FOREIGN KEY (NumList) REFERENCES Liste(NumList)
);


Explanation:



Voter Table:

CREATE TABLE Voter (
NumElv INT,
NumCand INT,
NumList INT,
Datevote DATE,
PRIMARY KEY (NumElv, NumCand, NumList),
FOREIGN KEY (NumElv) REFERENCES Electeur(NumElv),
FOREIGN KEY (NumCand) REFERENCES Candidat(NumCand),
FOREIGN KEY (NumList) REFERENCES Liste(NumList)
);


Explanation:



Add a "Datevote" column representing the current date to the "Voter" table

ALTER TABLE Voter
ADD Datevote DATE DEFAULT CURRENT_DATE;


Explanation:



Expand the "Appelation" column of the "Candidat" table to 30 characters

ALTER TABLE Candidat
MODIFY Appelation VARCHAR(30);


Explanation:



Split the "NomPrenElv" column of the "Electeur" table into "LastName" and "FirstName"

ALTER TABLE Electeur
DROP COLUMN NomPrenElv;

ALTER TABLE Electeur
ADD LastName VARCHAR(15) NOT NULL,
ADD FirstName VARCHAR(15) NOT NULL;


Explanation:



Create the "GestElections" database

ALTER TABLE Electeur
ADD Gender ENUM('Boy', 'Girl') NOT NULL;


Explanation:



Query to compile vote statistics:

SELECT Gender, COUNT(*) AS VoteCount
FROM Electeur E
JOIN Voter V ON E.NumElv = V.NumElv
GROUP BY Gender;


Explanation: