4. Chapter 2 Relational Database - Implementing with SQL (1)
4. Chapter 2 Relational Database - Implementing with SQL (1)
RELATIONAL DATABASE -
IMPLEMENTING WITH SQL
Zakia Challal
zakiachallal@gmail.com
02 C rea te ta ble s
05 Update da ta
07 E xerci s e
Introdu ctio n
In this part of the chapter we will see how to implement these relations
using a DBMS and the Structured Query Language (SQL)
SQL
SQL (Structured Query Language) is a standard language used to interact with relational
databases. It allows users to store, retrieve, modify, and manage data efficiently.
Syntax:
Create table Table_Name (attribute1 type, attribute2 type,….);
Example:
Create table Student (StudID integer, FirstName varchar(20), LastName varchar(20), Birthday
date, email varchar(50), DepID integer);
An integrity constraint is a set of rules that ensure the coherence of data in a relational
database. These constraints prevent invalid data entry and maintain the logical correctness
of relationships between tables.
We can’t delete tuples from the parent table if related tuples exist in the child table.
Example, we can’t delete a department if there are Student affected to that department.
We can force deletion by adding : ON DELETE CASCADE or ON DELETE SET NULL to the
constraint definition.
ON DELETE CASCADE: delete tuples in the child table automatically.
Ex: delete student of a department when the department is deleted
ON DELETE SET NULL: replace the value of the foreign key with Null.
Ex: replace the value of the DeptID in the student table with Null value.
Example: Create table Student (StudID integer Primary key, FirstName ……, DeptID integer
Foreign key references Department(DeptID) ON DELETE CASCADE);
Inte grity con st ra int
Unique
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Ex: insert into dept (DeptID, Name, HeadDept,Budget) values (1, ‘AI&SD’, ‘Djidel Faiza’, 0);
If we insert values for all columns in order, we can omit the column list:
insert into dept values (1, ‘AI&SD’, ‘Djidel Faiza’, 0);
De lete d ata from a table
Example:
Delete From Student Where StudID=‘1234’;
Delete From Student;
U pd ate d ata i n a table
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Ex:
Update Student
Set email=‘zakiachallal@gmail.com’
Where StudID= 1234;
The END