Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

4. Chapter 2 Relational Database - Implementing with SQL (1)

This document provides an overview of implementing relational databases using SQL, covering key concepts such as creating tables, data types, and integrity constraints. It details SQL commands for querying, inserting, updating, and deleting data, as well as defining primary and foreign keys. Additionally, it explains how to modify table structures and enforce data integrity through various constraints.

Uploaded by

ouagur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

4. Chapter 2 Relational Database - Implementing with SQL (1)

This document provides an overview of implementing relational databases using SQL, covering key concepts such as creating tables, data types, and integrity constraints. It details SQL commands for querying, inserting, updating, and deleting data, as well as defining primary and foreign keys. Additionally, it explains how to modify table structures and enforce data integrity through various constraints.

Uploaded by

ouagur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

CHAPTER 2 PART 2

RELATIONAL DATABASE -
IMPLEMENTING WITH SQL
Zakia Challal
zakiachallal@gmail.com

Computer Science Engineering. 2 nd year. 2024-2025


Co nte nt
01 SQL

02 C rea te ta ble s

In teg rity co n stra in ts


03
04 In se rt da ta

05 Update da ta

06 Del ete data

07 E xerci s e
Introdu ctio n

In the previous part of this chapter we have learn how to design a


database and lead to normalized set of relations.

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.

Main Uses of SQL:


Querying Data – SELECT
Inserting Data – INSERT INTO
Updating Data – UPDATE
Deleting Data – DELETE
Creating & Modifying Database Structures – CREATE TABLE, ALTER TABLE, DROP TABLE
Course exa mpl e
Student Department
StudID First Last Birthday Address email DepID DepID Name Head Budget
name name
Crea te Table

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);

Create table Department (DepID integer, Name varchar(50), Head varchar(40));

Note: SQL is not case sensitive.


Da ta Typ e
Character data type
• Char(n): Fixed-length character string (max 2000 bytes)
• Varchar(n): Variable-length character string (max 4000 bytes).
Numeric data type
• Number(p,s): fixed and floating-point numbers (p:Total number of digits, s: Number of digits after the decimal)
• Integer/int: equivalent to Number (38,0)

Date time data type


• Date: Stores date and time (YYYY-MM-DD HH:MI:SS).
• Timestamp: Stores date and time with fractional seconds (n).
Large Object data type
• Clob: Stores large text data.
• Blob: Stores large binary data (e.g., images, videos)
Inte grity con st ra int
Reminder

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.

Constraint Type Purpose


Domain Constraint Restricts values based on data type and rules
Entity Integrity Ensures every table has a unique identifier (Primary key)
Referential Integrity Maintains valid relationships between tables (Foreign key)
Unique Constraint Ensures uniqueness of certain attributes
NOT NULL Constraint Prevents missing (NULL) values
CHECK Constraint Imposes additional rules on column values
Inte grity con st ra int
Primary Key

Different ways to define the primary key:


When creating table:
Create table Student (StudID integer Primary key, FirstName …….)
Or
Create table Student (StudID integer, FirstName varchar(20), LastName varchar(20), Birthday
date, email varchar(50), DepID integer, Constraint PK1 Primary key (StudID));

Or After creating table


Alter table Student add constraint PK1 Primary key (StudID);
Inte grity con st ra int
Fo r e i g n K e y

Different ways to define the Foreing key:


When creating table:
Create table Student (StudID integer Primary key, FirstName ……, DeptID integer Foreign key
references Department(DeptID));
Or
Create table Student (StudID integer Primary key, FirstName ….., DepID integer, Constraint
FK1 Foreign key (DeptID) references Department(DeptID));

Or After creating table


Alter table Student add constraint FK1 Foreign key (DeptID) references Department(DeptID);
Inte grity con st ra int
Fo r e i g n K e y - ON DELETE CASCADE/ ON DELETE SET NULL

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

Different ways to define the constraint UNIQUE:


When creating table:
Create table Student (StudID integer Primary key, FirstName ….., email varchar(50) Unique)
Or
Create table Student (StudID integer, FirstName varchar(20), LastName varchar(20), Birthday
date, email varchar(50), DepID integer, Constraint U1 Unique(email));

Or After creating table


Alter table Student add constraint U1 Unique(StudID);
Inte grity con st ra int
Not Null

Different ways to define the constraint NOT NULL:


When creating table:
Create table Student (StudID integer Primary key, FirstName varchar(20) Not Null,…..)
Or
Create table Student (StudID integer, FirstName varchar(20), LastName varchar(20), Birthday
date, email varchar(50), DepID integer, Constraint C1 Check (FirstName is not null));

Or After creating table


Alter table Student add constraint C1 Check (FirstName is not null);
Inte grity con st ra int
Check

Different ways to define the constraint CHECK:


When creating table:
Create table Student (StudID integer Primary key,….., Birthday date Check (Birthday < Sysdate)…);
Or
Create table Student (StudID integer, FirstName varchar(20), LastName varchar(20), Birthday date,
email varchar(50), DepID integer, Constraint C1 Check (Birthday < Sysdate));

Or After creating table


Alter table Student add constraint C1 Check (Birthday < Sysdate);
Inte grity con st ra int
Default
Different ways to define the constraint DEFAULT:
When creating table:
Create table Department(DeptID integer Primary key, Name varchar(20), Budget int Default 0);

Or After creating table


Alter table Department Modify Budget Default 0;
Alter table
Add column
Add one column

Alter table Student Add Phone varchar(15);

Add multiple column

Alter table Department Add (Capacity int, CreationDate Date);


Alter table
Drop column
Drop one column

Alter table Student Drop column Phone;

Drop multiple column

Alter table Department Drop (Capacity , CreationDate);


Alter table
Modify column

Modify data type


Alter table student Modify FirstName varchar(50);

Modify not null/null constraint


Alter table Student modify LastName Not Null;
Alter table Student modify LastName Null; ( correct only if a not null constraint is already defined )

Add Default Value


Alter table Department Modify Budget Default 0;
Alter table
Rename column

ALTER TABLE table_name RENAME COLUMN old_coloumn TO new_column;

Ex: Alter table Department Rename Column Head To HeadDept;


Re n ame table

RENAME old_table TO new_table;

Ex: Rename Department to Dept;


Drop ta bl e

DROP TABLE nom_table;


Ex: Drop Table Dept;
Ins er t d ata into a table

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

DELETE FROM table_name WHERE condition;

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

You might also like