SQL Overview, SQL Data Type, DDL and Constraints: Ref. Chapter7

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

‫ﻫﻲ اﻟﻠﻲ اذا ﺟ‪4‬ﺖ @ﺴﻮي ا=ﺴﻴﺮت‪،‬ا‪8‬ﺪ‪5‬ﺖ‪،‬د‪5‬ﻠ‪4‬ﺖ‪ ..

‬ﻋﻠﻰ اﻟﺪاﺗﺎﺑ(ﺲ >‪DML-‬‬


‫ﻫﻲ اﻟﻠﻲ اﻋﺮف ﻓﻴﻬﺎ ا=ﺸﺊ ﺟﺪاول اﺣﻂ ﻗﻴﻮد… >‪DDL-‬‬

‫‪SQL Overview , SQL Data Type , DDL and‬‬


‫‪Constraints‬‬
‫‪Ref. Chapter7‬‬

‫‪1‬‬
SQL overview
• Official pronunciation is ‘S-Q-L‘ or ( see-qual)
• SQL: Structured Query Language
‫ ﻓﻲ اﻟﺪاﺗﺎ ﺑ(ﺲ‬SQL ‫ﻞ راح =ﺴﺘﺨﺪم‬5‫…ﻒ واﻟﺘﻌﺪ‬Ž‫ﻋﺸﺎن =ﺴﻮي اﻟﺘﻌ‬

• The standard for relational database management systems (RDBMS)


such as Oracle Database.
• All SQL statements are instructions to the database.
(‫ﻐﻰ اﺳﻮي اﺳﺘﺮﺟﺎع‬8‫ واذا ا‬,insert row ،‫ﻋ}ﺎرە ﻋﻦ اواﻣﺮ ا•ﺘﺒﻬﺎ ﻟﻠﺪاﺗﺎ ﺑ(ﺲ)ا=ﺸﺊ ﺟﺪول‬
• Easy to learn:
• Consists of standard English words, case insensitive
.‫ﻔﺮق‬5‫ﻞ او ﺳﻤﻮل ﻋﺎدي ﻣﺎ‬4z‫ﻌﺮف ﻛﺘ}ﺖ ﻛﺒ‬5‫ﻣﺎ‬

• It is a non-procedural language:
• you specify what information you require, rather than how to get it.
In other words, SQL does not require you to specify the access
2
methods to the data
.‫ﺠﻴﺒﻬﺎ‬5 ‫ﻒ‬4‫ﺎﻧﻬﺎ او ﻛ‬ƒ‫( ﻣﺎ اﻗﻮﻟﻪ و…ﻦ ﻣ‬how) ‫ﻐﻰ ﻣﻦ دون‬8‫( ا‬what)‫@ﺲ اﻗﻮﻟﻪ وش‬
The Process of Database Design
Database Design ‫ﺎة‬4‫دورة ﺣ‬

Step1 Step2 Step3 Step4


Conceptual Create
Real World Relational Load Data
model schema
Domain Data Model (DML)
(ERD) (DDL)

ER,EER Logical model ‫اﺣﻂ اﻟﺪاﺗﺎ ﻓﻲ اﻟﺪاﺗﺎ ﺑ(ﺲ ا=ﺸﺊ اﻟﺪاﺗﺎ ﺑ(ﺲ‬
‫)اﻟﺮوز واﻟ™ﻮﻟﻤﺰ واذا @ﺴﻮي‬
(‫ﺖ‬4‫ﻠ‬5‫ﺖ او د‬5‫ﺪ‬8‫ا‬

3
SQL Overview
‫ ﻓﻴﻬﺎ؟‬SQL‫ اﻟﻠﻲ ﺗﺨﺪﻣﻨﻲ ال‬tasks ‫ا©ﺶ ال‬
• SQL provides statements for a variety of tasks, including:
1. Querying data( Select command) ٤ ‫ اﻟﻄﻼب اﻟﻠﻲ ﻣﻌﺪﻟﻬﻢ ا•ﺒﺮ ﻣﻦ‬select :‫ﻣﺜﺎل‬
select ‫ﺎﺳﺘﺨﺪام‬8 (‫…ﺰ‬Ž…ª™‫ﺔ اﺳﺘﺮﺟﺎع ﻣﻦ اﻟﺪاﺗﺎ ﺑ(ﺲ)اﻟ‬4‫اﺳﻮي ﻋﻤﻠ‬

3. Inserting, updating, and deleting rows in a table (DML)


4. Creating, replacing, altering, and dropping objects(DDL)
5. Controlling access to the database and its objects(DCL)
6. Guaranteeing database consistency and integrity(DCL) ]
DCL-> Data control Language-> ‫اﺳﺘﺨﺪﻣﻬﺎ ﻋﺸﺎن اﻟﺒﺮﻣﺸﻦ ﻋﻠﻰ اﻟﺪاﺗﺎ ﺑ(ﺲ‬

4
SQL Identifier
‫ ﻛﻮﻟﻤﺰ ﻻزم اﻋﻄﻴﻬﺎ اﺳﻢ‬،‫ ﻓﻴﻮ‬،‫}ﻞ‬±‫ ﺗ‬،‫ﻴﻮت‬²‫ﺖ اي اﺗﺘﺮ‬³‫اذا ا=ﺸ‬
• SQL identifiers are used to identify objects in the database,
such as table names, view names, and columns.
• The following restrictions are imposed on an identifier:
‫ﺎت‬4‫ﻫﻨﺎ ﻗﻴﻮد ﻋﻠﻰ اﻟﻤﺴﻤ‬

• an identifier can be no longer than 128 characters (most


dialects have a much lower limit than this);
• an identifier must start with a letter;
• an identifier cannot contain spaces

5
‫‪ISO SQL Data Types‬‬
‫اذا ا=ﺸ‪³‬ﺖ اﺗﺘﺮ‪²‬ﻴﻮت ﻻزم اﻗﻮل ا©ﺶ ﻧ‪ª‬ع اﻟﺪاﺗﺎ اﻟﻠﻲ ﺑ‪4‬ﺤﻤﻠﻬﺎ وﻫﻨﺎ ﻧﺤﺪد )‪(Data Type‬‬

‫‪6‬‬
‫ﻧ}ﺪا ﺑﺘﻔﺼ‪4‬ﻠﻬﺎ ‪8‬ﺎﻟﺴﻼ‪5‬ﺪ اﻟﻠﻲ ‪8‬ﻌﺪە‬
• Boolean data …0,1 ‫ﺎ‬5 true, false ‫ﺎ‬5 yes,no ‫ﺎ‬5 ‫ﻔﻲ‬4‫ﻤﺘﻴﻦ ﻓﻘﻂ واﺳﻤﻴﻬﺎ ﻋﻠﻰ ﻛ‬4‫ﺗﺤﻤﻞ ﻗ‬
• Boolean data consists of the distinct truth values TRUE and FALSE
• Character data
• To store strings
• The format for specifying a character data type is:

CHAR ‫ﺖ اﺳﺘﺨﺪم‬8‫اذا ﻃﻮﻟﻪ ﺛﺎ‬


VARCHAR ‫اذا ﻃﻮﻟﻪ ﻣﺘﻐﻴﺮ اﺳﺘﺨﺪم‬

• string may be defined as having a fixed or varying length.


• Char ‫ﺖ‬8‫ﻜﻮن ﻃﻮﻟﻬﺎ ﺛﺎ‬5 ‫ﻤﺮي ﻛﻴﺰ‬5‫ﻏﺎﻟ}ﺎ اﻟﺒﺮا‬ ‫ــﻊ ﺧﺎﻧﺎت‬²‫ﻜﻮن ار‬5 ‫ﺎﻟﻀ}ﻂ‬8 ‫ﻫﻨﺎ ﻻزم‬
stores fixed-length character strings . Ex: branchNo CHAR(4)
• Varchar ‫ ﺧﺎﻧﻪ‬٣٠ ‫ﻫﻨﺎ اي ﻋﺪد ﺧﺎﻧﺎت @ﺲ ﻣﺎﺗﺘﻌﺪى ال‬
Stores variable-length strings. Ex: address VARCHAR(30)
➢String values are quoted with single quotes
• Eg ‘12234’, ‘abcd’, ‘a12’
7
exact numeric
The exact numeric data type is used to define numbers with an exact
representation
• Syntax for specifying numeric
• numeric (precision, scale)
• Precision is the maximum digits of numbers <- ‫ﻋﺪد اﻟﺨﺎﻧﺎت‬
• Scale specifies the position of decimal point. <- ‫ﻢ ﺧﺎﻧﻪ‬Ì ‫ﻌﺪ اﻟﻔﺎﺻﻠﻪ‬8
• The default scale is always 0 ‫ﻌﺘﺒﺮ ﻟﻲ اﻟﻌﺪد ﺻﺤﻴﺢ‬5 ‫ راح‬scale‫اذا ﻣﺎﺣﺪدت ال‬
• E.g.
• numeric(5) ex) 12345
• numeric(6,2) 6 digit (not including decimal point) decimal
number with 2 digits after the decimal point , ex) 1234.56 S
P
8
exact numeric
• There are several ways of specifying an exact numeric data
type:

<- ‫ﻪ ﻓﻮاﺻﻞ اﺳﺘﺨﺪﻣﻬﺎ‬4‫ﺎن ﻓ‬Ì ‫اذا‬

• INTEGER is used for large positive or negative whole


numbers.‫ﺤﻪ ﻣﻮﺟ}ﻪ او ﺳﺎﻟ}ﺔ‬4‫اﻋﺪاد ﺻﺤ‬
• SMALLINT is used for small positive or negative whole
numbers( ‫ﺤﻪ ﻣﻮﺟ}ﺔ او ﺳﺎﻟ}ﺔ ﻟ™ﻦ اﺻﻐﺮ )@ﺲ ﺗﻮﺻﻞ ﻟﻼﻟﻮف‬4‫اﻋﺪاد ﺻﺤ‬.
• Ex: rooms SMALLINT (The column rooms of the PropertyForRent
table)
➢ By specifying this data type, less storage space can be reserved for
the data ‫ﺗﺎﺧﺬ ﻟﻼﻟﻮف زي ﻣﺎﻗﻠﻨﺎ ﻟ™ﻦ ﻣﻴﺰﺗﻬﺎ ﺗﺎﺧﺬ ﻣﺴﺎﺣﻪ اﻗﻞ‬
9
Approximate numeric data

• The approximate numeric data type is used for defining


numbers that do not have an exact representation
• such as real numbers
Approximate numeric data <- ‫( =ﺴﺘﺨﺪم‬5.2E6) ‫اذا ﻋﻨﺪي اﺳﺲ او اﻋﺪاد ﻣﺮە ﺻﻐﻴﺮە‬

Data types -> Approximate numeric


FLOAT [precision] <-‫ﻏﺎﻟ}ﺎ راح =ﺴﺘﺨﺪم اﻟﻔﻠﻮت‬
DOUBLE
REAL

INTEGER-> ‫ﺤﻪ ﺳﻮاء ﻣﻮﺟ}ﻪ او ﺳﺎﻟ}ﺔ‬4‫ﻟﻼﻋﺪاد اﻟﺼﺤ‬


DECIMAL-> ‫ﻟﻼﻋﺪاد اﻟﻠﻲ ﻓﻴﻬﺎ ﻓﻮاﺻﻞ‬ 10
FLOAT-> (‫ﺎﻻﻟﻪ‬8 ‫اذا ﻋﻨﺪي اﺳﺲ او اﻋﺪاد ﻣﺮە ﺻﻐﻴﺮە )زي اﻟﻠﻲ ﺗﻄﻠﻊ‬
Date and time
• SQL uses the date data type to store both date and time
• SQL date has rich formats, you need to specify it
SS second 0-59
MI Minute 0-59
HH Hour 1-12

Lecture4
HH24 Military hour 1-24
DD day of month 1-31 (depends on month)
DAY day of the week Sunday-Saturday
D day of the week 1-7
MM month number 1-12
MON month abbreviated Jan—Dec
Month Month spelled out January-December
YY last 2 digits of year eg, 98
11
YYYY full year value eg, 1998

‫ﺲ ﻧﻌﺮﻓﻬﺎ وﻧﺘﻌﺎﻣﻞ ﻣﻌﻬﺎ‬c ‫ﻣﺎﻧﺤﻔﻈﻬﺎ‬


Date and time
• Example of date format:
• ‘dd-mon-yyyy’ 01-dec-2001
• ‘dd/mm/yyyy’ 01/12/2001

Lecture4
• ‘mm-dd-yy hh:mi:ss’ 12-01-01 12:30:59

12
Data Definition language ( DDL )

Lecture4
Table Creation

13
Data Definition (Creating a table)

• Creating a table
create table table-name (
column-name1 datatype ,
……..

Lecture4
column-nameN datatype );

‫ وﻫﺬي ﺛﺎﺑﺘﻪ‬create table ‫* اول ﺷﻲ ﻧﻜﺘﺐ‬


‫}ﻞ وﻗﻮس‬±‫ﻌﺪﻫﺎ اﺳﻢ اﻟﺘ‬8 *
‫ﻨﻬﻢ ﻓﺎﺻﻠﺔ‬±²‫ﻞ اﻟ™ﻮﻟﻤﺰ و‬Ý‫ﺎر وﻫﻜﺬا ﻟ‬Ì ‫ﺎر او ﻓﺎر‬Ì ‫ﻪ اذا‬4‫ﺎﻧﺎت ﻓ‬4‫ع اﻟﺒ‬ª‫ﻌﺪﻫﺎ اﺳﻢ اﻟ™ﻮﻟﻢ وﻧ‬8 *
‫* اﺧﺮ ﺷﻲ ﻧﺤﻂ اﻟﻘﻮس واﻟﻔﺎﺻﻠﺔ اﻟﻤﻨﻘﻮﻃﺔ‬
14
Integrity Constraints
• An integrity constraint defines a business rule for a table
column.
• When enabled, the rule will be enforced by SQL
• constraints can be specified as row constraints and table

Lecture4
constraints. Tables constraints can, for example, identify
several columns such as Primary key.
• If the results of an INSERT or UPDATE statement violate an
integrity constraint, the statement will be rolled back.
‫ ﻣﺎراح‬null ‫ﻤﺔ اﻟﺒﻮزﺷﻦ‬4‫ﻌﺪﻳﻦ اﺳﻮي ا=ﺴﺮت واﺣﻂ ﻗ‬8 ‫ﻜﻮن ﻋﻨﺪە ﺑﻮزﺷﻦ‬5 ‫ﺪ ان ﻻزم اﻟﻤﻮﻇﻒ‬4‫ﻟﻮ ﻣﺜﻼ اﻧﺎ اﺣﻂ ﻗ‬
rolled back ‫ﺴﻤﻰ‬à‫ﺔ اﻻ=ﺴﺮت و‬4‫ﺤﻪ ﻗ}ﻞ ﻋﻤﻠ‬4‫ﻳﺮﺿﻰ ©ﺴﻮي ﻟﻲ ا=ﺴﺮت ﻓﺮاح ﻳﺮﺟﻊ اﻟﺪاﺗﺎ ﺑ(ﺲ ﻻﺧﺮ ﺣﺎﻟﻪ ﺻﺤ‬
• Note : If you don’t give the constraint name, the system will
generate a name automatically, but the name is hard for 15
human understanding.
Integrity Constraints
• Constraint clauses can appear in the following statements:
Integrity ‫ﺪ‬4‫ﻒ اي ﻗ‬4‫ﻤﺪﻳﻨﻲ اﺿ‬4‫ﻴﻮت اﻟﻤﻮﺟﻮدة ﻓ‬²‫ﻞ اﻻﺗﺘﺮ‬Ì ‫}ﻞ راح اﺣﻂ‬±‫ﺖ ا=ﺸﺊ ﺗ‬4‫اذا ﺟ‬
:‫ ﻓﻲ اﻟﺤﺎﻻت ﻫﺬي‬Constraints
1. CREATE TABLE
2. ALTER TABLE

Lecture4
3. CREATE VIEW
4. ALTER VIEW

16
The Five types of integrity constraint
‫ﺔ‬4‫ﺎت ﺛﺎﻧ‬4‫ﻣﺴﻤ‬

1. NOT NULL constraint -> Required data


2. unique constraint
3. primary key constraint -> Entity Integrity
4. foreign key constraint -> Referential Integrity

Lecture4
5. check constraint -> Domain constraint

17
NOT NULL constraint

• The NOT NULL constraint enforces a column to NOT accept


NULL values. ‫ﺪ‬4‫ اﺳﺘﺨﺪم ﻫﺬا اﻟﻘ‬null ‫ﻪ‬4‫ﻜﻮن ﻓ‬5‫ﻐﻰ اﻟ™ﻮﻟﻢ ﻣﺎ‬8‫اذا ا‬

• The NOT NULL constraint enforces a field to always contain a

Lecture4
value. This means that you cannot insert a new record, or
update a record without adding a value to this field.

Syntax:
create table table-name (
column-name1 datatype NOT NULL );
18
NOT NULL constraint
• Example : The following SQL enforces the "P_Id" column and the
"LastName" column to not accept NULL values:

CREATE TABLE Persons(


P_Id char(5) NOT NULL,

Lecture4
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) );

19
SQL NULL
‫ زي ﺗﻮ‬null ‫ﻌﻨﻲ ﻣﻮ‬5
• If a column in a table is optional, we can insert a new record or
update an existing record without adding a value to this column.
This means that the field will be saved with a NULL value.
null ‫ﺤﻂ ﻫﻮ اﻧﻬﺎ‬5 ‫ﻤﻪ اوﻛﻲ اذا ﻣﺎﺣﻄﻴﺘﻲ راح‬4‫ﺸﻨﻞ اذا ﺣﻄﻴﺘﻲ ﻗ‬å‫ﻪ او‬4‫اﺧﻠ‬

• NULL values are treated differently from other values.


space ‫ﻣﺎ ﺗﻌﺘﺒﺮ ﺻﻔﺮ وﻻ ﺗﻌﺘﺒﺮ‬

Lecture4
• NULL is used as a placeholder for unknown or inapplicable values.
‫ﺪ‬8‫ﻞ ﻣﺆﻗﺖ ﻻﻧﻲ ﻣﺎﻋﺮف اﻟﻔﺎﻟﻴﻮ او ﻣﻮ ﻣﺘﻮﻓﺮە ﺑﻬﺎﻟﻮﻗﺖ @ﺲ ﻣﺎ اﺧﻠﻴﻬﺎ ﻟﻼ‬ƒ‫ اﺳﺘﺨﺪﻣﻬﺎ @ﺸ‬null ‫ال‬
• Note: It is not possible to compare NULL and 0; they are not
equivalent.
• It is not possible to test for NULL values with comparison
operators, such as =, <, or <>.
• We will have to use the IS NULL and IS NOT NULL operators 20
instead.
is null or is not null ‫ﺪاﻟﻬﺎ اﺳﺘﺨﺪم‬8 null ‫ﻣﺎ اﺳﺘﺨﺪم ا•ﺒﺮ واﺻﻐﺮ وﻫﺬوﻟﻲ ﻣﺎراح ﺗﻤﺸﻲ ﻣﻊ ال‬
UNIQUE constraint ‫ﻣﺎﻳﺘﻜﺮر‬

• The UNIQUE constraint prohibits‫ ﺗﻤﻨﻊ‬multiple rows from having


the same value in the same column or combination of columns but
allows some values to be null. ‫ ﻓﺎﻟﻴﻮ‬null ‫ﺎل‬8 ‫ﺴﻤﺢ‬à ‫ﺎﻟﺘﻜﺮار @ﺲ‬8 ‫ﺴﻤﺢ‬à‫ﻣﺎ‬
• The UNIQUE constraint provides a guarantee for uniqueness for a
column or set of columns.
• Unique columns are not automatically NOT NULL

Lecture4
Syntax:
create table table-name (
column-name1 datatype UNIQUE );
OR
create table table-name (
…….
……. 21
[constraint constraint-name] UNIQUE (Columns_list) ;
Example
• The following SQL creates a UNIQUE constraint on the
"P_Id" column when the "Persons" table is created:

CREATE TABLE Persons

Lecture4
(
P_Id char(5) NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
Constraint Unique (LastName, FirstName)
22
);
‫ﺴﻤﻴﻨﻪ‬à ‫ﻜﻮن ﺻﻌﺐ زي ﻣﺎﻗﻠﻨﺎ ﻗ}ﻞ ﻓﺎﻻﻓﻀﻞ اﻧﻚ‬4²‫ﺤﻂ اﺳﻢ ﻣﻦ ﻋﻨﺪە و‬5 ‫ﻫﻨﺎ ﻫﻮ ﻣﺎﺳﻤﻰ اﻟ™ﻮ=ﺴﺘﺮ…ﺖ ﻓﺮاح‬
Primary key Constraints <- null ‫ﺎل‬8 ‫ﺎﻟﺘﻜﺮار وﻻ‬8 ‫ﻣﺎ©ﺴﻤﺢ ﻻ‬

‫ ﺳﻮا‬UNIQUE constraint ‫ وال‬NULL constraint ‫ﺠﻤﻊ ﻟﻲ ال‬5

• A primary key constraint combines a NOT NULL constraint


and a UNIQUE constraint in a single declaration. That is, it
prohibits‫ ﺗﻤﻨﻊ‬multiple rows from having the same value in
the same column or combination of columns and prohibits
values from being null.

Lecture4
• A PRIMARY KEY constraint automatically has a UNIQUE
constraint defined on it.
• Note that you can have many UNIQUE constraints per table,
but only one PRIMARY KEY constraint per table.
‫ﻤﺮي‬5‫}ﻞ ﻣﺎﻋﻨﺪي اﻻ ﺑﺮا‬±‫< اﻗﺪر اﺳﻮي @ﺲ واﺣﺪ ﻻن اﺻﻼ اﻟﺘ‬- Primary key Constraints
.‫ﻴﻮﺗﺰ‬²‫ﻴﻮت وﺣﺪە او ﻣﺠﻤﻮﻋﻪ ﻣﻦ اﻻﺗﺘﺮ‬²‫ﺎن اﺗﺘﺮ‬Ì ‫ﻛﻲ واﺣﺪ ﺳﻮاء‬
.‫ ﻋﺎدي‬as many as I can ‫< اﻗﺪر اﺣﻂ‬- UNIQUE constraints 23

‫ﻚ ﻋﺸﺎن اﺿﻤﻦ ﻋﺪم اﻟﺘﻜﺮار‬4‫ﻞ اﻻﺗﺮﻧﺖ ﻛﻴﺰ ﻻزم ا•ﺘﺐ ﻳﻮﻧ‬Ì


Primary key Constraints
Syntax:
create table table-name (
column-name1 datatype Primary Key);

Lecture4
OR

create table table-name (


…….
…….
[constraint constraint-name] Primary Key (Columns_list) ;
24
Primary key Constraints

Example :
create table Department (
dept_no char(4),

Lecture4
Dept_name varchar(25),
CONSTRAINT dept_PK PRIMARY KEY(dept_no));

25
Foreign Keys constraint
‫}ﻞ زي ﻣﺎﻗﻠﻨﺎ ﻗ}ﻞ‬±‫ ﺗ‬parent ‫}ﻞ ﻻزم ﺗﻜﻮن ﻣﻮﺟﻮدە او ﺗﺮﺟﻊ ﻟﻞ‬±‫ ﺗ‬child‫ﺎل‬8 ‫اﻟﺮوز اﻟﻠﻲ ﻣﻮﺟﻮدە‬
foreign key constraint requires values in one table to match
values in another table.

Syntax:

Lecture4
create table table-name (
column-name1 datatype,
……
……
[constraint constraint-name] Foreign key (Column_name)
references referenced_table (column_in_referenced_table) );
26
Foreign Keys constraint
Create table Staff(
staff_no char(3),
staff_name varchar(20),
dept_no char(4),

Lecture4
CONSTRAINT staff_PK PRIMARY KEY(staff_no ),
Constraint staff_fk foreign key (dept_no) references
department (dept_no));

27
referential integrity constraints
‫ﺸﺎﺑﺘﺮ‬z‫ﺎﻟ‬8 ‫ﻔﻴﻦ ﻫﺎﻟﻔﻘﺮە‬5‫ﺷﺎ‬
‫ﻪ‬4‫اﻟﻠﻲ ﻗ}ﻞ ﻟ™ﻦ ﺑﻨﻤﺮ ﻋﻠ‬

‫…ﺘﺮي‬Ž‫ﻞ اﻧﺘﻘ‬4‫…ﻔﺮ=ﺸ‬Ž‫ راح اﺧﺘﺮق اﻟ‬parent ‫ﺎل‬8 ‫ﺤﺪث داﺗﺎ ﻣﻮﺟﻮدە‬8 ‫ﻤﺴﺢ او‬8 ‫اذا‬
• When you delete or update a value of the columns
referenced by other tables, the referential integrity
constraints may be violated.
• ON DELETE/UPDATE Clause

Lecture4
• The ON DELETE / ON UPDATE clause lets you determine
how Database automatically maintains referential
integrity if you remove or update a referenced primary
key value. If you omit this clause, then database does
not allow you to delete referenced key values in the
parent table that have dependent rows in the child
table.
28
٦٦‫ او ﻏﻴﺮﺗﻪ ل‬٦ ‫ ﻟﻮ ﺣﺬﻓﺖ دﻳ}ﺎرﺗﻤﻨﺖ‬،‫ دﻳ}ﺎرﺗﻤﻨﺘﺰ @ﺲ‬٤ ‫ﺎول‬8 ‫ دﻳ}ﺎرﺗﻤﻨﺘﺰ واﻟﻤﻮﻇﻔﻴﻦ ©ﺸﺘﻐﻠﻮن‬٦ ‫ﺎن ﻋﻨﺪي‬Ì ‫ﻟﻮ اﻧﺎ‬
‫ اﻻوﻟﻰ ﻣﺎﻧﻘﺪر ﻻن‬٤‫ﻪ ﻓﻌﺎدي اﻋﺪل او اﺣﺬف ﻟ™ﻦ اﻟﺪﻳ}ﺎرﺗﻤﻨﺘﺰ ال‬4‫ﺘﻐﻴﺮ؟ ﻻ ﻻن ﻣﺎﻋﻨﺪي اﺣﺪ ﻓ‬±‫ﻪ ﺷﻲ ﺑ‬4‫ﻫﻞ ﻓ‬
.‫ﻪ ﻣﺎراح ©ﺴﻤﺢ ﻟﻲ اﺣﺬف‬4‫ﻪ روز ﻣﺮﺗ}ﻄﻪ ﻓ‬4‫ وﻓ‬on delete\update ‫ﺼﻴﺮ ف‬5 ‫ ﻓﺎﻧﺎ اذا ﻣﺎﺣﺪدت ا©ﺶ‬،‫ﻓﻴﻬﺎ داﺗﺎ‬
On delete -> CASCADE ‫اﺳﻮء ﺷﻲ ال‬
Referential integrity constraints On update-> CASCADE ‫اﻗﻀﻞ ﺷﻲ ال‬
Four options are supported when the user attempt to delete the CK and there
matching FKs:
• ON DELETE/UPDATE CASCADE: delete or update the CK row from the parent table
and all matching FK rows in the child table. ‫}ﻞ‬±‫ﻠﺪ ﺗ‬5‫ﻜﻮن ﻋﻠﻰ اﻟﺸﺎ‬5 ‫اﻟﺘﻐﻴﻴﺮ راح‬
‫ﻞ اﻟﻤﻮﻇﻔﻴﻦ اﻟﻠﻲ ﻓﻴﻬﺎ‬Ì ‫ وﻓﻴﻬﺎ ﻣﻮﻇﻔﻴﻦ‬٣ ‫ ﻟﻮ اﺣﺬف دﻳ}ﺎردﻣﻨﺖ‬CASCADE ‫ﺪ اﻟﻠﻲ راح ﻓﻲ ال‬5‫ﺎﻟﺴﻼ‬8 ‫ﻟﻮ ﻧﻤﺸﻲ ﻋﻠﻰ ﻧﻔﺲ اﻟﻤﺜﺎل اﻟﻠﻲ‬
ON DELETE <- ‫ﻠﻲ‬ƒ‫ اوﺗﻮﻣﺘ‬child ‫ﺴﻮى ب‬z‫ راح ﻳ‬parent ‫ﺎل‬8 ‫…ﻪ‬ª‫ﺑﻴﺮوﺣﻮن ﻻن اﻟﻠﻲ اﺳ‬
‫ﺼﻴﺮون ©ﺸﺘﻐﻠﻮن ﻓﻲ‬4‫ ﺑ‬٣ ‫ﺼﻴﺮ اﻟﻤﻮﻇﻔﻴﻦ اﻟﻠﻲ ©ﺸﺘﻐﻠﻮن ﻓﻲ‬4‫ﺘﻐﻴﺮ ﻋﺎدي ﺑ‬±‫ ﺑ‬٣٣ ‫ وﺧﻠﻴﺘﻪ‬٣ ‫ﺖ ﻋﻠﻰ اﻟﺪﻳ}ﺎردﻧﺖ رﻗﻢ‬5‫ﺪ‬8‫…ﺖ ا‬ª‫ﻟﻮ ﺳ‬
ON UPDATE <- ٣٣
• ON DELETE/UPDATE SET NULL: delete or update the CK row from the parent table

Lecture4
and set the FK values to NULL in the child table.
‫ﺎﻣﻞ‬Ì ‫ﻜﻮﻧﻮن ﻣﻮﺟﻮدﻳﻦ ﻣﻊ‬4‫ﻞ اﻟﻤﻮﻇﻔﻴﻦ اﻟﻠﻲ ﻓﻴﻬﺎ ﺑ‬Ì ‫ وﻓﻴﻬﺎ ﻣﻮﻇﻔﻴﻦ‬٣ ‫ ﻟﻮ اﺣﺬف دﻳ}ﺎردﻣﻨﺖ‬SET NULL ‫ﻟﻮ ﻧﻤﺸﻲ ﻋﻠﻰ ﻧﻔﺲ اﻟﻤﺜﺎل ﻓﻲ ال‬
ON DELETE/UPDATE <- null ‫ﻜﻮن‬4‫ ﺑ‬٣٣ ‫ اﻟﻰ‬٣ ‫ﻌﺪ ﻟﻮ اﺣﺪث رﻗﻢ اﻟﺪﻳ}ﺎردﻣﻨﺖ ﻣﻦ‬²‫ و‬،null ‫ﻜﻮن‬4‫ﺎﻧﺎﺗﻬﻢ @ﺲ رﻗﻢ اﻟﺪﻳ}ﺎردﻣﻨﺖ ﺑ‬4‫ﺑ‬
• This is valid only if the foreign key columns do not have the NOT NULL qualifier
specified.
• ON DELETE/UPDATE SET Default: delete or update the CK row from the parent
table and set the FK values to the specified default value in the child table.
‫ﻔﻮﻟﺖ ﻓﺎﻟﻴﻮ‬5‫ﻞ ﺷﻲ اﺧﺘﺎر د‬Ì ‫ ﻟﻮ راح اﺧﺘﺎر ﻫﺬي ﻻزم ﻗ}ﻞ‬،‫ﻔﻮﻟﺖ ﻓﺎﻟﻴﻮ‬5‫ﺎﺧﺬون اﻟﺪ‬5 ‫ﺖ راح‬5‫ﺪ‬8‫ ﻟﻮ اﺣﺬف او اﺳﻮي ا‬SET Default ‫ﻫﻨﺎ ﻓﻲ‬
• Valid only if DEFAULT constraint is specified
29
• No action: Reject‫ ﺗﻤﻨﻊ‬the delete‫ﺖ‬5‫ﺪ‬8‫ واﻻ‬operation from the parent table.
‫ﻔﻮﻟﺖ‬5‫ﺖ اذا ﻣﺎﺣﺪدت وش ©ﺴﻮي اﻟﺪ‬4‫ﻠ‬5‫اﻻون د‬
• This is the default setting if the ON DELETE rule is omitted. ‫ﻔﻮﻟﺖ ﻻزم‬5‫ﺖ ﻣﺎﻋﻨﺪي ﺷﻲ د‬5‫ﺪ‬8‫ اﻻون ا‬،‫ﻧﻮ ا•ﺸﻦ‬
‫اﺣﺪد‬
‫ﻪ اﺣﺪ ﻣﺮﺗ}ﻂ ﻋﺎدي‬4‫ﺖ اذا ﻣﺎﻓ‬4‫ﻠ‬5‫ﺖ او د‬5‫ﺪ‬8‫ﺴﻤﺢ ﻟﻲ اﺳﻮي ﺷﻲ ﺗﻤﻨﻌﻨﻲ اﺳﻮي ا‬à ‫ﻪ ﻣﺎراح‬4‫ﻪ اﺣﺪ ﻣﺮﺗ}ﻂ ﻓ‬4‫اذا ﻓ‬
PK EmpId EmpName
‫ﺎر‬u‫ﺎﻻﺧﺘ‬x ‫ وﻣﻤﻜﻦ ﺗﺠ|ﺐ ﻛﺬا‬،‫ﻣﺜﺎل‬
1 Ali
• CREATE TABLE EmpMaster 2 Slaut
(EmpId CHAR(1) PRIMARY KEY, 3 John

EmpName VARCHAR(25)); FK
EmpId DeptNo DeptName
1 101 AAA
• CREATE TABLE EmpDetails 2 101 AAA
(DeptId CHAR(3) PRIMARY KEY, 3 103 CCC

DeptName VARCHAR(20)

Lecture4
EmpId CHAR(1) FOREIGN KEY REFERENCES EmpMaster(EmpId)
ON DELETE CASCADE );

• delete from EmpMaster where EmpId=1


EmpMaster EmpDetails
EmpId EmpName EmpId DeptNo DeptName
2 Slaut 2 101 AAA 30
3 John 3 103 CCC

parent child
PK EmpId EmpName
‫ﺎر‬u‫ﺎﻻﺧﺘ‬x ‫ وﻣﻤﻜﻦ ﺗﺠ|ﺐ ﻛﺬا‬،‫ﻣﺜﺎل‬
1 Ali
• CREATE TABLE EmpMaster 2 Slaut
(EmpId CHAR(1) PRIMARY KEY, 3 John
EmpName VARCHAR(25)); FK
EmpId DeptNo DeptName
1 101 AAA
• CREATE TABLE EmpDetails 2 101 AAA
(DeptId CHAR(3) PRIMARY KEY, 3 103 CCC
DeptName VARCHAR(20)

Lecture4
EmpId CHAR(1) FOREIGN KEY REFERENCES EmpMaster(EmpId)
ON DELETE SET NULL );

• delete from EmpMaster where EmpId=1


EmpDetails
EmpMaster
EmpId DeptNo DeptName
EmpId EmpName
NULL 101 AAA
2 Slaut
AAA 31
2 101
3 John
3 103 CCC
parent child
Check Constraints
‫ﺤﻤﻠﻬﺎ ﻫﺬا اﻟ™ﻮﻟﻢ اﺳﺘﺨﺪم ﻫﺬا اﻟ™ﻮ=ﺴﺘﺮ…ﺖ‬5 ‫ﻢ اﻟﻠﻲ‬4‫ﺤﻂ ﺷﺮط ﻋﻠﻰ اﻟﻘ‬8 ‫اذا‬

• The CHECK constraint is used to limit the value range that


can be placed in a column.

• If you define a CHECK constraint on a single column it

Lecture4
allows only certain values for this column.

32
Check Constraints
Syntax: ‫ﺑﻴﻦ ﻗﻮﺳﻴﻦ ﺗﺤﻄﻴﻦ اﻟﺸﺮط ﺣﻘﻚ‬
create table table-name (
column-name1 datatype Check (Check_condition)
);

Lecture4
OR

create table table-name (


…….
…….
[constraint constraint-name] Check (Check_condition) 33
);
Check Constraints Example
/* This way to write comment*/
Create table staff(
Staff_no char(3),
Staff_name varchar(20) not null,
Staff_gender char(1) check (staff_gender in (‘M’, ‘F’)),

Lecture4
Staff_salary numeric (8,2) not null,
Dept_no char(4) not null ,
Constraint staff_pk Primary key (staff_no),
Constraint staff_fk Foreign key (dept_no) references department
(dept_no),
Constraint staff_sal check (staff_salary >10000.00));
34
To allow naming of a
CHECK constraint
DEFAULT constraint

• The DEFAULT constraint is used to insert a default value


into a column.
• The default value will be added to all new records, if no
other value is specified. ‫ﻤﺔ‬4‫ﻔﻮﻟﺖ اذا ﻣﺎﺣﺪدت ﻗ‬5‫ﻤﻪ اﻟﺪ‬4‫ﺗﻨﺤﻂ اﻟﻘ‬

Lecture4
Syntax:
create table table-name (
column-name1 datatype Default (Default_value)
);
35
DEFAULT constraint

• Example : The following SQL creates a DEFAULT constraint


on the "City" column when the "Persons" table is created:

CREATE TABLE Persons

Lecture4
(
P_Id char(5) NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'
)
36
Example : All Constraints
Create table staff(
Staff_no char(3),
Staff_name varchar(20) not null,
DateofBirth date,
Staff_nationality char(10) default ‘Saudi’,

Lecture4
Staff_salary numeric(8,2) not null,
Dept_no char(4),

[
Constraint staff_pk Primary key (staff_no),
Constraint staff_fk Foreign key (dept_no) references
department (dept_no) on delete set null,
Constraint staff_sal check (staff_salary >10000.00),
37
UNIQUE(staff_name, DateofBirth));
Modifying Table Definitions (alter )

ALTER consists of six options to:

• Add a new column to a table.


• Drop a column from a table.

Lecture4
• Add a new table constraint.
• Drop a table constraint.
• Set a default for a column.
• Drop a default for a column.

38
Modifying Table Definitions ‫ˆﻘﺔ ﻛﺘﺎﺑﺘﻬﺎ‬‰‫ﻃ‬

Lecture4
39
Examples
alter table orders
add (quantity numeric (3) not null);

alter table staff

Lecture4
drop COLUMN DOB ;

• Be careful if the table contains data.

To add a CHECK Constraint


ALTER TABLE staff ADD constraint staff_sal check
40
(staff_salary >10000.00) ;
Dropping Tables
• Drop table table_name [Cascade Constraints];

• Pay attention to referential integrity constraints when dropping tables.


:‫ﺎر…ﻦ‬4‫}ﻞ او ﻛﻮﻟﻢ ﻋﻨﺪي ﺧ‬±‫ﺤﺬف ﺗ‬8 ‫اذا‬
• (1) RESTRICT: The DROP operation is rejected if the column is
referenced by another database object. This is the default setting.
‫ﻔﻮﻟﺖ ﻟﻮ ﻣﺎ اﺧﺘﺮﺗﻲ ﺷﻲ‬5‫ وﻫﺬا اﻟﺪ‬،‫ﺔ اﻟﺤﺬف‬4‫ﻤﻨﻊ ﻋﻤﻠ‬5 ‫ﻪ راح‬4‫}ﻞ ﻣﺮﺗ}ﻂ ﻓ‬±‫ﻪ ﺗ‬4‫}ﻞ ﻓ‬±‫ﺎن ﻫﺬا اﻟﺘ‬Ì ‫اذا‬

Lecture4
• (2) CASCADE: The DROP operation proceeds and automatically drops
the column from any database objects it is referenced by.
• If Cascade Constraints is used, the constraints will be dropped first.
‫ﻌﺪﻳﻦ اﺣﺬﻓﻪ ﻫﻮ‬8 ‫ﻪ اول‬4‫}ﻠﺰ اﻟﻠﻲ ﻣﺮﺗ}ﻄﻪ ﻓ‬±‫ﻫﻨﺎ راح اﺣﺬف اﻟﺘ‬

• example
• Drop table Staff;
‫ﻪ‬4‫}ﻞ ﻣﺮﺗ}ﻂ ﻓ‬±‫ﻪ ﺗ‬4‫ راح ‘ﻤﻨﻊ اﻟﺤﺬف ﻻن ﻓ‬،Restrict ‫ﻔﻮﻟﺖ اﻟﻠﻲ ﻫﻲ‬5‫ﺨﺘﺎر ﻟﻲ اﻟﺪ‬5 ‫ﻫﻨﺎ ﻣﺎ اﺧﺘﺮﻧﺎ ﺷﻲ ﻓﺎراح‬
• Drop table Department;
‫ﻪ‬4‫}ﻞ ﻣﺮﺗ}ﻂ ﻓ‬±‫ﻪ ﺗ‬4‫ ﻟ™ﻦ راح ‘ﺤﺬف ﻻن ﻣﺎﻓ‬،Restrict ‫ﻔﻮﻟﺖ اﻟﻠﻲ ﻫﻲ‬5‫ﺨﺘﺎر ﻟﻲ اﻟﺪ‬5 ‫ﻫﻨﺎ ﻣﺎ اﺧﺘﺮﻧﺎ ﺷﻲ ﻓﺎراح‬ 41
• Drop table Department cascade constraints;
cascade ‫ﻪ ﻻﻧﻨﺎ اﺧﺘﺮﻧﺎ‬4‫ﻞ ﺷﻲ ﻣﺮﺗ}ﻂ ﻓ‬Ì ‫ﺤﺬف و…ﺤﺬف‬5 ‫ﻫﻨﺎ راح‬
Dropping Constraints

• To drop a constraint, use:

ALTER TABLE Table_name


DROP PRIMARY KEY| UNIQUE (column_name) | CONSTRAINT

Lecture4
constraint_name;

42
Example
• To DROP a CHECK Constraint

ALTER TABLE Persons


DROP CONSTRAINT chk_Person;

Lecture4
• To DROP a FOREIGN KEY Constraint
ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders;

43
ALTER/MODIFY COLUMN

• To change the data type of a column in a table, use the


following syntax:

ALTER TABLE table_name

Lecture4
ALTER COLUMN column_name datatype;

Example:
ALTER TABLE Persons
ALTER COLUMN DateOfBirth year;

44
‫ﺎﻟﻤ|ﺪ ﺗﻴﺮم ﻻﻧﻨﺎ ﺑ—ﺴﺘﺨﺪﻣﻬﺎ‬x ‫ˆﻨﺎ ﻋﻠﻴﻬﺎ ﻣﺎراح ﺗﻄﻠﺐ ﻧﻜﺘﺒﻬﺎ‬‰‫ اﻟﻠﻲ ﻣ‬syntax‫ﺎﻟﻨﻬﺎ‘ﺔ ال‬x
.‫¡ﻪ وˆﻜﻮن ﻋﻠﻴﻬﺎ ﺳﺆال‬¢‫ﺲ اﻟﻤﻬﻢ ﻧﻔﻬﻤﻬﺎ ﻻن ﻣﻤﻜﻦ ﺗﺠﻴﺒﻬﺎ ﻣﻜﺘ‬c ‫ﺎﻟﺒﺮوﺟﻜﺖ‬x

You might also like