‫اوﻻ وﻗﺒﻞ اﻟﺒﺪء ﻓﻰ اﻟﺸﺮح اود ان اذﻛﺮ ﻣﺼﺪر اﻟﺸﺮح ﻟﻸﻣﺎﻧﻪ ھﺬه‬

‫اﻟﺪروس ﻣﻨﻘﻮﻟﻪ ﻋﻦ ﻣﻮﻗﻊ‬
‫‪WWW.W3Schools.COM‬‬
‫وﻟﻘﺪ ﻗﻤﺖ اﻧﺎ ﻓﻘﻂ ﺑﺘﺮﺟﻤﺘﮫﺎ وﺗﻮﺿﯿﺢ وﺗﻌﺪﻳﻞ ﺑﻌﺾ اﻟﻨﻘﺎط اﻟﺼﻐﯿﺮه ﺑﮫﺎ‬
‫ﺳﯿﻜﻮن ھﺬا اﻟﺸﺮح ﻋﺒﺎره ﻋﻦ ﻣﺮﺟﻊ ﺷﺎﻣﻞ ﻟﺠﻤﻞ ‪SQL‬‬
‫اوﻻ ﻣﻘﺪﻣﻪ ﻓﻰ ‪: SQL‬‬
‫ھﻰ ﻟﻐﻪ ﻗﯿﺎﺳﯿﻪ ﻣﻦ ﻟﻐﺎت اﻟﺤﺎﺳﺐ ﻟﺪﺧﻮل وﻣﻌﺎﻟﺠﻪ ﻗﻮاﻋﺪ اﻟﺒﯿﺎﻧﺎت‬
‫ﻣﺎ ھﻰ ‪Structured Query Language : SQL‬‬
‫ﻟﻐﻪ ﺑﻨﺎء اﻷﺳﺘﻌﻼﻣﺎت اﻟﮫﯿﻜﻠﯿﻪ )ﻳﻌﻨﻰ ھﻰ اﻟﺘﺮﺟﻤﻪ ﺣﺮﻓﯿﻪ ﺷﻮﻳﻪ(‬
‫‪-١‬‬
‫‪-٢‬‬
‫‪-٣‬‬
‫‪-٤‬‬
‫‪-٥‬‬
‫‪-٦‬‬
‫‪-٧‬‬

‫ھﻰ ﻟﻐﻪ ﻗﯿﺎﺳﯿﻪ ﻣﻦ ﻟﻐﺎت اﻟﺤﺎﺳﺐ اﻟﺨﺎﺻﻪ ﺑﻤﻌﮫﺪ ‪ANSI‬‬
‫ﺗﻤﻜﻨﻚ ﻣﻦ اﻟﺪﺧﻮل ﻟﻘﻮاﻋﺪ اﻟﺒﯿﺎﻧﺎت‬
‫ﺗﻤﻜﻨﻚ ﻣﻦ إﺳﺘﺨﺮاج اﻟﺒﯿﺎﻧﺎت ﻣﻦ اﻟﻘﺎﻋﺪه‬
‫ﺗﻤﻜﻨﻚ ﻣﻦ إﺿﺎﻓﻪ ﺑﯿﺎﻧﺎت إﻟﻰ ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت‬
‫ﺗﻤﻜﻨﻚ ﻣﻦ ﻣﻦ ﺣﺬف ﺑﯿﺎﻧﺎت ﻣﻦ اﻟﻘﺎﻋﺪه‬
‫ﺗﻤﻜﻨﻚ ﻣﻦ ﺗﻌﺪﻳﻞ اﻟﺒﯿﺎﻧﺎت اﻟﻤﺴﺠﻠﻪ‬
‫أﺧﯿﺮا ھﻰ ﻟﻐﻪ ﺳﮫﻠﻪ اﻟﺘﻌﻠﻢ واﻟﻔﮫﻢ‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪١‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

‫ﻟﻐﻪ ‪ SQL‬ھﻰ ﻟﻐﻪ ﻗﯿﺎﺳﯿﻪ ‪:‬‬
‫ھﻰ ﻟﻐﻪ ﻣﻦ اﻟﻠﻐﺎت اﻟﻘﯿﺎﺳﯿﻪ اﻟﺨﺎﺻﻪ ﺑﻤﻌﮫﺪ‬
‫)‪ANSI (American National Standards Institute‬‬
‫ﺗﻤﻜﻨﻚ ﻣﻦ دﺧﻮل وﻣﻌﺎﻟﺠﻪ ﻧﻈﻢ ﻗﻮاﻋﺪ اﻟﺒﯿﺎﻧﺎت ‪Database System‬‬
‫ﺟﻤﻞ ‪ SQL‬ﺗﻌﻤﻞ ﻣﻊ ﺑﺮاﻣﺞ ﻗﻮاﻋﺪ اﻟﺒﯿﺎﻧﺎت ﻣﺜﻞ ‪:‬‬
‫‪Ms-Access, Ms-SQL Server, DB2, Oracle, etc.‬‬

‫ﺗﻨﻘﺴﻢ ﻟﻐﻪ ‪ SQL‬إﻟﻰ ﻗﺴﻤﯿﻦ ‪:‬‬

‫)‪1- SQL Data Manipulation Language (DML‬‬
‫ھﻮ اﻟﻘﺴﻢ اﻟﻤﺴﺌﻮل ﻋﻦ ‪ :‬ﻣﻌﺎﻟﺠﻪ اﻟﺒﯿﺎﻧﺎت‬
‫‪ : Select -١‬إﺳﺘﺨﺮاج اﻟﺒﯿﺎﻧﺎت ﻣﻦ ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت‬
‫‪ : INSERT INTO -٢‬إﺿﺎﻓﻪ ﺑﯿﺎﻧﺎت ﺟﺪﻳﺪه‬
‫‪ : Update -٣‬اﻟﺘﻌﺪﻳﻞ ﻋﻠﻰ اﻟﺒﯿﺎﻧﺎت اﻟﻤﺴﺠﻠﻪ‬
‫‪ : Delete -٤‬ﺣﺬف اﻟﺒﯿﺎﻧﺎت ﻣﻦ اﻟﻘﺎﻋﺪه‬
‫)‪2- Data Definition Language (DDL‬‬
‫ھﻮ اﻟﻘﺴﻢ اﻟﻤﺴﺌﻮل ﻋﻦ ‪ :‬ﺗﻌﺮﻳﻒ اﻟﺒﯿﺎﻧﺎت‬
‫‪ : Create Database -١‬ﻷﻧﺸﺎء ﻗﺎﻋﺪه ﺑﯿﺎﻧﺎت ﺟﺪﻳﺪه‬
‫‪ : Create Table -٢‬ﻷﻧﺸﺎء ﺟﺪول داﺧﻞ ﻗﺎﻋﺪه ﺑﯿﺎﻧﺎت‬
‫‪ : ALTER TABLE -٣‬ﻟﻠﺘﻌﺪﻳﻞ ﻓﻰ اﻟﺠﺪول‬
‫‪ : DROP TABLE -٤‬ﻟﺤﺬف اﻟﺠﺪول ﻣﻦ ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت‬
‫‪ : CREATE INDEX-٥‬ﻷﻧﺸﺎء ﻣﻔﺘﺎح ﻟﻠﺒﺤﺚ‬
‫‪ : DROP INDEX -٦‬ﻟﺤﺬف ﻣﻔﺘﺎح اﻟﺒﺤﺚ‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪٢‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

Select ‫أوﻻ ﺟﻤﻠﻪ‬
‫ﺗﺴﺘﺨﺪم ﻓﻰ إﺳﺘﺨﺮاج ﺑﯿﺎﻧﺎت ﻣﻦ داﺧﻞ اﻟﺠﺪول ﺣﺴﺐ اﻟﻤﻄﻠﻮب‬
Syntax :‫ﺗﻜﺘﺐ اﻟﺠﻤﻠﮫ ﻛﺎﻟﺘﺎﻟﻰ‬
SELECT column_name(s)
FROM table_name

‫ ﻏﯿﺮ ﺣﺴﺎﺳﻪ ﻟﺤﺎﻟﻪ اﻟﺤﺮوف‬SQL ‫ ﺟﻤﻞ‬: ‫ﻣﻼﺣﻈﻪ ھﺎﻣﻪ‬
SELECT = select ‫ﻻ ﻳﻮﺟﺪ ﻓﺮق‬
: ‫ﻣﺜﺎل ﻋﻠﻰ اﻟﺠﺪول اﻟﺘﺎﻟﻰ‬
The database table "Persons":
LastName
Hansen
Svendson
Pettersen

FirstName
Ola
Tove
Kari

Address
Timoteivn 10
Borgvn 23
Storgt 20

City
Sandnes
Sandnes
Stavanger

(FirstName , LastName) ‫ﻧﺮﻳﺪ أﺧﺘﯿﺎر اﻷﺳﻢ اﻷول و اﻷﺳﻢ اﻷﺧﯿﺮ‬
: ‫ﺗﻜﺘﺐ اﻟﺠﻤﻠﻪ ﻛﺎﻟﺘﺎﻟﻰ‬
SELECT LastName,FirstName FROM Persons

: ‫وﻳﻜﻮن اﻟﻨﺎﺗﺞ ﻛﺎﻟﺘﺎﻟﻰ‬
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Hansen
Svendson
Pettersen

FirstName
Ola
Tove
Kari

: ‫ﻷﺧﺘﯿﺎر ﺟﻤﯿﻊ اﻟﺒﯿﺎﻧﺎت ﺗﻜﺘﺐ ﻛﺎﻟﺘﺎﻟﻰ‬
SELECT * FROM Persons

: ‫وﺗﻜﻮن اﻟﻨﺘﯿﺠﻪ ﻛﺎﻟﺘﺎﻟﻰ‬
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Hansen
Svendson
Pettersen

FirstName
Ola
Tove
Kari

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

Address
Timoteivn 10
Borgvn 23
Storgt 20

٣

City
Sandnes
Sandnes
Stavanger

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

‫ﻣﻼﺣﻈﻪ ھﺎﻣﻪ ‪ :‬ﺑﻌﺾ ﻧﻈﻢ إداره ﻗﻮاﻋﺪ اﻟﺒﯿﺎﻧﺎت ﺗﺘﻄﻠﺐ وﺟﻮد ) ; (‬
‫ﻓﻰ ﻧﮫﺎﻳﻪ ﺟﻤﻠﻪ ‪ SQL‬وھﺬا ﻻن ﺑﻌﺾ اﻟﻨﻈﻢ ﺗﺪﻋﻢ ﺗﻨﻔﯿﺬ أﻛﺜﺮ ﻣﻦ‬
‫ﺟﻤﻠﻪ ﻓﻰ ﻧﻔﺲ اﻟﻮﻗﺖ‪.‬‬
‫ﻣﻦ اﻟﻨﻈﻢ اﻟﺘﻰ ﻻ ﺗﺤﺘﺎج إﻟﻰ ) ; ( ‪. Ms-Access – Ms-Sql server‬‬
‫ام ﻣﻦ اﻟﻨﻈﻢ اﻟﺘﻰ ﺗﺤﺘﺎج إﻟﻰ ) ; ( ‪. Oracle‬‬

‫اﻷن ﻧﻨﺪرج ﻟﺠﻤﻠﻪ أﺧﺮى او إﺿﺎﻓﻪ ﻟﺠﻤﻠﻪ ‪SELECT‬‬
‫وھﻰ ﺟﻤﻠﻪ ‪SELECT DISTINCT‬‬
‫إﺿﺎﻓﻪ ﻛﻠﻤﻪ ‪ DISTINCT‬ﻟﻠﺠﻤﻠﻪ ﺗﻤﻜﻨﻚ ﻣﻦ اﻟﻌﺮض اﻟﻘﯿﻢ ﺑﺪون‬
‫ﺗﻜﺮار )ﻳﻌﻨﻰ ﻟﻮ اﻟﺠﺪول ﻓﯿﻪ ﺑﯿﺎن ﻣﺘﻜﺮر اﻛﺘﺮ ﻣﻦ ﻣﺮه ﺑﺘﻌﺮﺿﻪ‬
‫ﻣﺮه واﺣﺪه ﺑﺲ( ‪.‬‬
‫ﻣﺜﺎل ‪ :‬ﻋﻠﻰ أﺳﺘﺨﺪام ‪ DISTINCT‬وﻋﺪم أﺳﺘﺨﺪاﻣﮫﺎ‬
‫‪SELECT Company FROM Orders‬‬
‫‪"Orders" table‬‬
‫‪OrderNumber‬‬
‫‪3412‬‬
‫‪2312‬‬
‫‪4678‬‬
‫‪6798‬‬

‫‪Company‬‬
‫‪Sega‬‬
‫‪W3Schools‬‬
‫‪Trio‬‬
‫‪W3Schools‬‬
‫اﻟﻨﺘﯿﺠﻪ ‪Result:‬‬
‫‪Company‬‬
‫‪Sega‬‬
‫‪W3Schools‬‬
‫‪Trio‬‬
‫‪W3Schools‬‬

‫ھﻨﺎ واﺿﺢ اﻟﻔﺮق ﻋﻨﺪ أﺳﺘﺨﺪام ‪DISTINCT‬‬
‫‪SELECT DISTINCT Company FROM Orders‬‬
‫اﻟﻨﺘﯿﺠﻪ ‪Result:‬‬
‫‪Company‬‬
‫‪Sega‬‬
‫‪W3Schools‬‬
‫‪Trio‬‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪٤‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

‫اﻻن ﻧﻨﺘﻘﻞ ﻟﺠﺰء اﺧﺮ اﻻ وھﻮ اﺳﺘﺨﺪام اﻟﺸﺮط‬
WHERE ‫اﺳﺘﺨﺪام ﻋﺒﺎره‬
‫ وﺗﺤﺘﻮى ﻋﻠﻰ اﻟﺸﺮط اﻟﻤﻄﻠﻮب‬Select ‫ﺗﺄﺗﻰ ﺑﻌﺪ ﺟﻤﻠﻪ‬
: ‫وﻳﻜﻮن ﺑﻨﺎﺋﮫﺎ ﺑﮫﺬا اﻟﺸﻜﻞ‬
Syntax
SELECT column FROM table
WHERE column operator value

: ‫ ﻳﻤﻜﻨﻨﺎ أﺳﺘﺨﺪام اﻟﻤﻌﺎﻣﻼت اﻷﺗﯿﻪ‬Where ‫ﻣﻊ ﺟﻤﻠﻪ‬
‫اﻟﻮﺻﻒ‬

‫اﻟﻤﻌﺎﻣﻞ‬

‫ﻳﺴﺎوى‬
‫ﻻ ﻳﺴﺎوى‬
‫أﻛﺒﺮ ﻣﻦ‬
‫أﺻﻐﺮ ﻣﻦ‬
‫أﻛﺒﺮ ﻣﻦ او ﻳﺴﺎوى‬
‫اﺻﻐﺮ ﻣﻦ او ﻳﺴﺎوى‬
‫ﻳﻜﻮن اﻟﺸﺮط ﺑﯿﻦ ﻗﯿﻤﺘﯿﻦ‬
‫ﻟﻠﺒﺤﺚ ﻋﻦ ﻛﻠﻤﺎت ﻣﺘﺸﺎﺑﮫﻪ‬

=
<>
>
<
>=
<=
Between
LIKE

<>‫ اﻟﻤﻌﺎﻣﻞ‬SQL ‫ ﻓﻰ ﺑﻌﺾ اﻷﺻﺪارات ﻣﻦ‬: ‫ﻣﻼﺣﻈﻪ ھﺎﻣﻪ‬
!= ‫ﻳﻜﺘﺐ ھﻜﺬا‬

Where ‫ﻣﺜﺎل ﻋﻠﻰ أﺳﺘﺨﺪام ﻋﺒﺎره‬
SELECT * FROM Persons
WHERE City='Sandnes'
"Persons" table
LastName
Hansen
Svendson
Svendson
Pettersen

FirstName
Ola
Tove
Stale
Kari

Address
Timoteivn 10
Borgvn 23
Kaivn 18
Storgt 20

City
Sandnes
Sandnes
Sandnes
Stavanger

Year
1951
1978
1980
1960

City
Sandnes
Sandnes
Sandnes

Year
1951
1978
1980

Result ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Hansen
Svendson
Svendson

FirstName
Ola
Tove
Stale

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

Address
Timoteivn 10
Borgvn 23
Kaivn 18

٥

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

‫ﻣﻼﺣﻈﻪ ھﺎﻣﻪ ‪ :‬ﻋﻠﻰ أﺳﺘﺨﺪام ﻋﻼﻣﻪ اﻟﺘﻨﺼﯿﺺ ﻣﻊ اﻟﺠﻤﻠﻪ ) ' (‬
‫ﻳﺠﺐ أﺳﺘﺨﺪام ﻋﻼﻣﻪ ﺗﻨﺼﯿﺺ ﻣﻔﺮده ﻣﻊ اﻟﻘﯿﻢ اﻟﻨﺼﯿﻪ ﻣﺜﻞ‬
‫اﻷﺳﻤﺎء اﻣﺎ ﺑﺎﻟﻨﺴﺒﻪ ﻟﻠﺒﯿﺎﻧﺎت اﻟﺮﻗﻤﯿﻪ ﺗﻜﺘﺒﻪ ﻣﻦ دون ﻋﻼﻣﺎت ‪.‬‬
‫ﻣﺜﺎل ﻋﻠﻰ ذﻟﻚ ‪:‬‬
‫ﻟﻠﺒﯿﺎﻧﺎت اﻟﻨﺼﯿﻪ ‪For text values:‬‬
‫ﻃﺮﯾﻘﻪ ﺻﺤﯿﺤﻪ ‪This is correct:‬‬
‫'‪SELECT * FROM Persons WHERE FirstName='Tove‬‬
‫ﻃﺮﯾﻘﻪ ﺧﺎﻃﺌﻪ ‪This is wrong:‬‬
‫‪SELECT * FROM Persons WHERE FirstName=Tove‬‬

‫ﻟﻠﺒﯿﺎﻧﺎت اﻟﺮﻗﻤﯿﻪ ‪For numeric values:‬‬
‫ﻃﺮﯾﻘﻪ ﺻﺤﯿﺤﻪ ‪This is correct:‬‬
‫‪SELECT * FROM Persons WHERE Year>1965‬‬
‫ﻃﺮﯾﻘﻪ ﺧﺎﻃﺌﻪ ‪This is wrong:‬‬
‫'‪SELECT * FROM Persons WHERE Year>'1965‬‬

‫اﺳﺘﺨﺪام اﻟﻤﻌﺎﻣﻞ ‪ Like‬ﻓﻰ اﻟﺸﺮط‬
‫ﺗﺴﺘﺨﺪم ﻓﻰ ﺗﺤﺪﻳﺪ اﻟﺒﺤﺚ ﺑﻜﻠﻤﻪ ﻣﻌﯿﻨﻪ ﻣﺜﺎل اﻟﺒﺤﺚ ﻋﻦ ﻛﻞ‬
‫اﻷﺳﻤﺎء اﻟﻤﻮﺟﻮد ﺑﮫﺎ ﺣﺮف او ﻛﻠﻤﻪ ﻣﻌﯿﻨﻪ ‪.‬‬
‫ﺑﻨﺎﺋﮫﺎ ﺑﮫﺬا اﻟﺸﻜﻞ ‪Syntax‬‬
‫‪SELECT column FROM table‬‬
‫‪WHERE column LIKE pattern‬‬

‫ﺗﺴﺘﺨﺪم ﻋﻼﻣﻪ ) ‪ ( %‬ﻟﺘﺤﺪﻳﺪ ﻋﺪد اﻟﺤﺮوف ﻗﺒﻞ و ﺑﻌﺪ‬
‫اﻟﻜﻠﻤﻪ او اﻟﺤﺮف اﻟﺬى ﻧﺒﺤﺚ ﺑﻪ ﻣﺜﺎل ‪:‬‬
‫ﻓﻰ ھﺬه اﻟﺤﺎﻟﻪ ﺳﻮف ﺗﻈﮫﺮ اﺳﻤﺎء اﻷﺷﺨﺎص اﻟﺘﻰ ﺗﺒﺪا ﺑﺤﺮف ‪ A‬ﺛﻢ ﻳﺄﺗﻰ ﺑﻌﺪھﺎ اى ﻋﺪد ﻣﻦ اﻟﺤﺮوف‬
‫‪SELECT * FROM Persons‬‬
‫'‪WHERE FirstName LIKE 'a%‬‬

‫ﻓﻰ ھﺬه اﻟﺤﺎﻟﻪ ﺳﻮف ﺗﻈﮫﺮ اﺳﻤﺎء اﻷﺷﺨﺎص اﻟﺘﻰ ﺗﻨﺘﮫﻰ ﺑﺤﺮف ‪ A‬وﻳﻜﻮن ﻗﺒﻠﮫﺎ اى ﻋﺪد ﻣﻦ اﻟﺤﺮوف‬
‫‪SELECT * FROM Persons‬‬
‫'‪WHERE FirstName LIKE '%a‬‬

‫ﻓﻰ ھﺬه اﻟﺤﺎﻟﻪ ﺳﻮف ﺗﻈﮫﺮ اﺳﻤﺎء اﻷﺷﺨﺎص اﻟﺘﻰ ﺗﺤﺘﻮى ﻋﻠﻰ ﺣﺮف ‪ A‬ﻓﻰ أى ﻣﻮﺿﻮع ﻓﻰ اﻷﺳﻢ‬
‫‪SELECT * FROM Persons‬‬
‫'‪WHERE FirstName LIKE '%la%‬‬

‫ﺑﻘﻰ ﻟﻨﺎ اﻟﺠﺰء اﻟﺨﺎص ﺑـ‬

‫‪Between‬‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫وﺳﻮف ﻧﻨﺪرج إﻟﯿﻪ ﻓﯿﻤﺎ ﺑﻌﺪ‪.‬‬
‫‪٦‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

‫اﻻن ﻧﻨﺘﻘﻞ ﻟﺠﺰء ﺟﺪﻳﺪ اﻻ وھﻮ اﻟﺘﻌﺎﻣﻞ ﻣﻊ اﻟﺒﯿﺎﻧﺎت‬
‫ﻳﻮﺟﺪ ﻟﺪﻳﻨﺎ ﺛﻼﺛﻪ ﺗﻌﺎﻣﻼت ﻣﻊ اﻟﺒﯿﺎﻧﺎت‬
UPDATE : ‫ﺗﻌﺪﻳﻞ‬-٢
INSERT INTO : ‫ إﺿﺎﻓﻪ‬-١
DELETE : ‫ ﺣﺬف‬-٣
INSERT INTO

: ‫اوﻻ إﺻﺎﻓﻪ ﺻﻒ ﻟﻠﺠﺪول‬
: ‫ﻳﻜﻮن ﺑﻨﺎﺋﮫﺎ ﻛﺎﻟﺘﺎﻟﻰ‬

Syntax
INSERT INTO table_name
VALUES (value1, value2,....)

‫وﻳﻤﻜﻨﻚ اﻳﻀﺎ ﺗﺤﺪﻳﺪ اﻟﺤﻘﻮل اﻟﻤﻄﻠﻮب إﺿﺎﻓﺘﮫﺎ ﻓﻘﻂ وﺗﻜﻮن‬
: ‫ﻛﺎﻟﺘﺎﻟﻰ‬
INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)

: ‫ﻣﺜﺎل إﺿﺎﻓﻪ ﺻﻒ ﺟﺪﻳﺪ ﻟﻠﺠﺪول اﻟﺘﺎﻟﻰ‬
This "Persons" table:
LastName
Pettersen

FirstName
Kari

Address
Storgt 20

City
Stavanger

INSERT INTO Persons
VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes')

: ‫اﻟﻨﺘﯿﺠﻪ ﺗﻜﻮن ﻛﺎﻟﺘﺎﻟﻰ‬
LastName
Pettersen
Hetland

FirstName
Kari
Camilla

Address
Storgt 20
Hagabakka 24

City
Stavanger
Sandnes

: ‫ﻹﺿﺎﻓﻪ ﺑﯿﺎﻧﺎت ﻓﻰ أﻋﻤﺪه )ﺣﻘﻮل( ﻣﺤﺪده ﻓﻘﻂ ﺗﻜﺘﺐ ﻛﺎﻟﺘﺎﻟﻰ‬
INSERT INTO Persons (LastName, Address)
VALUES ('Rasmussen', 'Storgt 67')

: ‫ﺗﻜﻮن اﻟﻨﺘﯿﺠﻪ ﻛﺎﻟﺘﺎﻟﻰ‬
LastName
Pettersen
Hetland
Rasmussen

FirstName
Kari
Camilla

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

Address
Storgt 20
Hagabakka 24
Storgt 67

٧

City
Stavanger
Sandnes

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

UPDATE : ‫ﺛﺎﻧﯿﺎ اﻟﺘﻌﺪﻳﻞ ﻓﻰ اﻟﺠﺪول‬
: ‫ﻳﻜﻮن ﺑﻨﺎء اﻟﺠﻤﻠﻪ ﻛﺎﻟﺘﺎﻟﻰ‬
Syntax
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value

: ‫ﻣﺜﺎل ﻋﻠﻰ اﻟﺘﻌﺪﻳﻞ ﻋﻠﻰ اﻟﺠﺪول اﻟﺤﺎﻟﻰ‬
Person:
LastName
Nilsen
Rasmussen

FirstName
Fred

Address
Kirkegt 56
Storgt 67

City
Stavanger

: ‫ﻹﺿﺎﻓﻪ اﻷﺳﻢ اﻷول ﻓﻰ اﻟﺼﻒ اﻟﺜﺎﻧﻰ‬
UPDATE Person SET FirstName = 'Nina'
WHERE LastName = 'Rasmussen'
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Nilsen
Rasmussen

FirstName
Fred
Nina

Address
Kirkegt 56
Storgt 67

City
Stavanger

: ‫ﻹﺿﺎﻓﻪ أﺳﻢ اﻟﻤﺪﻳﻨﻪ وﺗﻐﯿﺮ اﻟﻌﻨﻮان‬
‫ﺗﻌﺪﻳﻞ أﻛﺜﺮ ﻣﻦ ﻋﻤﻮد ﻓﻰ ﻧﻔﺲ اﻟﻮﻗﺖ‬
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Nilsen
Rasmussen

FirstName
Fred
Nina

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

Address
Kirkegt 56
Stien 12

٨

City
Stavanger
Stavanger

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

DELETE : ‫ﺛﺎﻟﺜﺎ اﻟﺤﺬف ﻣﻦ اﻟﺠﺪول‬
: ‫ﺗﺴﺘﺨﺪم ﻓﻰ ﺣﺬف اﻟﺼﻔﻮف ﻣﻦ اﻟﺠﺪول وﺑﻨﺎﺋﮫﺎ ﻛﺎﻟﺘﺎﻟﻰ‬
Syntax
DELETE FROM table_name
WHERE column_name = some_value

: ‫ﻣﺜﺎل ﻋﻠﻰ ﺣﺬف ﺻﻒ ﻣﻦ اﻟﺠﺪول‬
Person:
LastName
Nilsen
Rasmussen

FirstName
Fred
Nina

Address
Kirkegt 56
Stien 12

City
Stavanger
Stavanger

‫ﻟﺤﺬف اﻟﺼﻒ‬
DELETE FROM Person WHERE LastName = 'Rasmussen'
Result :‫اﻟﻨﺘﯿﺤﻪ‬
LastName
Nilsen

FirstName
Fred

Address
Kirkegt 56

City
Stavanger

‫ﻟﺤﺬف ﺟﻤﯿﻊ اﻟﺒﯿﺎﻧﺎت ) اﻟﺼﻔﻮف ( ﻣﻦ اﻟﺠﺪول‬
: ‫ﺗﺴﺘﺨﺪم اﻟﺠﻤﻠﻪ اﻟﺘﺎﻟﯿﻪ‬
DELETE FROM table_name
or
DELETE * FROM table_name

‫ﺗﻢ ﺑﺤﻤﺪ اﷲ ﺗﻌﺎﻟﻰ اﻟﺠﺰء اﻷول ﻣﻦ اﻟﺪرس واﻟﺨﺎص‬
SQL Data Manipulation Language (DML) ‫ﺑـ‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

٩

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

‫ﻓﻰ ھﺬه اﻟﻤﺮﺣﻠﻪ ﻣﻦ اﻟﺸﺮح ﺳﻮف ﻧﺒﺪأ ﻓﻰ اﻟﺘﻌﺎﻣﻞ‬
‫ﺑﻤﺮوﻧﻪ أﻛﺜﺮ ﻣﻊ ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ﻋﻦ ﻃﺮﻳﻖ إﺿﺎﻓﺎت ﻟﻤﺎ ﺗﻢ‬
‫ﺷﺮﺣﻪ ﻓﻰ اﻟﺠﺰء اﻷول ﻣﻦ اﻟﺪرس ﻋﻦ ﻃﺮﻳﻖ ﺟﻤﻞ ‪SQL‬‬
‫ﻋﺒﺎره ‪Order By :‬‬
‫ﺗﺴﺘﺨﺪم ﻟﺘﺮﺗﯿﺐ اﻟﻨﺎﺗﺞ ﻣﻦ اﻷﺳﺘﻌﻼم ﺣﺴﺐ ﺣﻘﻞ ﻣﺤﺪد‬
‫ﻣﺜﺎل ﻋﻠﻰ اﻟﺘﺮﻳﺐ ‪:‬‬
‫‪Orders:‬‬
‫‪OrderNumber‬‬
‫‪3412‬‬
‫‪5678‬‬
‫‪2312‬‬
‫‪6798‬‬

‫‪Company‬‬
‫‪Sega‬‬
‫‪ABC Shop‬‬
‫‪W3Schools‬‬
‫‪W3Schools‬‬

‫ﺳﻨﻘﻮم ﺑﺘﻨﻔﯿﺬ ﺟﻤﻠﻪ اﻷﺳﺘﻌﻼم اﻟﺘﺎﻟﯿﻪ ‪:‬‬
‫‪SELECT Company, OrderNumber FROM Orders‬‬
‫‪ORDER BY Company‬‬
‫اﻟﻨﺘﯿﺠﻪ ‪Result:‬‬
‫‪OrderNumber‬‬
‫‪5678‬‬
‫‪3412‬‬
‫‪6798‬‬
‫‪2312‬‬

‫‪Company‬‬
‫‪ABC Shop‬‬
‫‪Sega‬‬
‫‪W3Schools‬‬
‫‪W3Schools‬‬

‫ﻧﻼﺣﻆ ان اﻟﺒﯿﺎﻧﺎت ﻇﮫﺮت ﻓﻰ ﺗﺮﺗﯿﺐ ﺣﺴﺐ ﺣﻘﻞ‪Company‬‬

‫إذا اردﻧﺎ اﻟﺘﺮﺗﯿﺐ ﺑﺄﻛﺜﺮ ﻣﻦ ﺣﻘﻞ ﺗﻜﻮن ﻛﺎﻟﺘﺎﻟﻰ ‪:‬‬
‫‪SELECT Company, OrderNumber FROM Orders‬‬
‫‪ORDER BY Company, OrderNumber‬‬
‫اﻟﻨﺘﯿﺠﻪ ‪Result:‬‬
‫‪OrderNumber‬‬
‫‪5678‬‬
‫‪3412‬‬
‫‪2312‬‬
‫‪6798‬‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪١٠‬‬

‫‪Company‬‬
‫‪ABC Shop‬‬
‫‪Sega‬‬
‫‪W3Schools‬‬
‫‪W3Schools‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

‫ﻣﺎذا ﻟﻮ اردﻧﺎ ﻋﻜﺲ اﻟﺘﺮﺗﯿﺐ ﻛﯿﻒ ﻳﺘﻢ ذﻟﻚ‬
‫ﻣﺜﺎل ‪:‬‬
‫‪SELECT Company, OrderNumber FROM Orders‬‬
‫‪ORDER BY Company DESC‬‬
‫اﻟﻨﺘﯿﺠﻪ ‪Result:‬‬
‫‪OrderNumber‬‬
‫‪6798‬‬
‫‪2312‬‬
‫‪3412‬‬
‫‪5678‬‬

‫‪Company‬‬
‫‪W3Schools‬‬
‫‪W3Schools‬‬
‫‪Sega‬‬
‫‪ABC Shop‬‬

‫ﻓﻰ ﺣﺎﻟﻪ اﻧﻨﺎ ﻧﺮﻳﺪ أﺳﺘﺨﺪام اﻟﻄﺮﻳﻘﺘﯿﻦ ﻛﯿﻒ ﻳﺘﻢ ذﻟﻚ‬
‫ﻣﺜﺎل ‪:‬‬
‫ﻳﻤﻜﻦ ﻋﻤﻞ ذﻟﻚ ﺑﻄﺮﻳﻘﺘﯿﻦ‬
‫اﻟﻄﺮﻳﻘﻪ اﻷوﻟﻰ ‪:‬‬
‫‪SELECT Company, OrderNumber FROM Orders‬‬
‫‪ORDER BY Company DESC, OrderNumber ASC‬‬

‫اﻣﺎ اﻟﻄﺮﻳﻘﻪ اﻟﺜﺎﻧﯿﻪ ھﻰ ﻛﺘﺎﺑﻪ اﻟﺤﻘﻮل اﻟﻤﻄﻠﻮب ﺗﺮﺗﯿﺒﮫﺎ‬
‫ﺗﺼﺎﻋﺪﻳﺎ اوﻻ ﺛﻢ اﻟﺤﻘﻮل اﻟﻤﻄﻠﻮب ﺗﺮﺗﯿﺒﮫﺎ ﺗﻨﺎزﻟﯿﺎ‬
‫ﻣﺜﺎل ‪:‬‬
‫‪SELECT Company, OrderNumber FROM Orders‬‬
‫‪ORDER BY OrderNumber , Company DESC‬‬

‫وﻓﻰ اى ﻣﻦ اﻟﻄﺮﻳﻘﺘﯿﻦ ﺗﻜﻮن اﻟﻨﺘﯿﺠﻪ واﺣﺪه‬
‫اﻟﻨﺘﯿﺠﻪ ‪Result:‬‬
‫‪OrderNumber‬‬
‫‪2312‬‬
‫‪6798‬‬
‫‪3412‬‬
‫‪5678‬‬

‫‪Company‬‬
‫‪W3Schools‬‬
‫‪W3Schools‬‬
‫‪Sega‬‬
‫‪ABC Shop‬‬

‫اﻷن ﻧﻨﺘﻘﻞ ﻟﻤﻌﺎﻣﻼت ﺟﺪﻳﺪه‬
‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪١١‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

And – Or ‫اﻟﻤﻌﺎﻣﻠﯿﻦ‬
‫ﻳﺴﺘﺨﺪم ھﺬﻳﻦ اﻟﻤﻌﺎﻣﻠﯿﻦ ﻓﻰ رﺑﻂ ﺷﺮﻃﯿﻦ او أﻛﺜﺮ‬
‫ ﻳﻘﻮم ﺑﻌﺮض اﻟﻨﺘﺎﺋﺞ ﻓﻰ ﺣﺎﻟﻪ ﺗﺤﻘﻖ‬AND ‫ اﻟﻤﻌﺎﻣﻞ‬‫ﺟﻤﯿﻊ اﻟﺸﺮوط‬
‫ ﻳﻘﻮم ﺑﻌﺮض اﻟﻨﺘﺎﺋﺞ ﻓﻰ ﺣﺎﻟﻪ ﺗﺤﻘﻖ اى‬OR ‫ اﻟﻤﻌﺎﻣﻞ‬‫ﺷﺮط ﻣﻦ اﻟﺸﺮوط‬
‫ ﻋﻠﻰ اﻟﺠﺪول اﻟﺘﺎﻟﻰ‬: ‫ﻣﺜﺎل‬
LastName
Hansen
Svendson
Svendson

FirstName
Ola
Tove
Stephen

Address
Timoteivn 10
Borgvn 23
Kaivn 18

City
Sandnes
Sandnes
Sandnes

: AND ‫أﺳﺘﺨﺪام اﻟﻤﻌﺎﻣﻞ‬
SELECT * FROM Persons
WHERE FirstName='Tove'
AND LastName='Svendson'
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Svendson

FirstName
Tove

Address
Borgvn 23

City
Sandnes

: OR ‫أﺳﺘﺨﺪام اﻟﻤﻌﺎﻣﻞ‬
SELECT * FROM Persons
WHERE firstname='Tove'
OR lastname='Svendson'
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Svendson
Svendson

FirstName
Tove
Stephen

Address
Borgvn 23
Kaivn 18

City
Sandnes
Sandnes

: ‫أﺳﺘﺨﺪام اﻟﻤﻌﺎﻣﻠﯿﻦ ﻣﻌﺎ‬
SELECT * FROM Persons WHERE
(FirstName='Tove' OR FirstName='Stephen')
AND LastName='Svendson'
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Svendson
Svendson

FirstName
Tove
Stephen

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

Address
Borgvn 23
Kaivn 18

١٢

City
Sandnes
Sandnes

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

IN ‫اﻟﻤﻌﺎﻣﻞ‬
‫ﻟﻪ ﻋﺪه أﺳﺘﺨﺪاﻣﺎت ﻣﻨﮫﺎ اﻧﻪ ﻳﻤﻜﻨﻚ ﺗﺤﺪﻳﺪ اﻟﻘﯿﻤﻪ اﻟﻤﺮاد‬
‫ﻋﺮﺿﮫﺎ إذا ﻛﻨﺖ ﻣﺘﺄﻛﺪ ﻣﻦ وﺟﻮدھﺎ ﻓﻰ أﺣﺪ اﻟﺤﻘﻮل‬
: ‫ﺑﻨﺎء اﻟﺠﻤﻠﻪ‬
SELECT column_name FROM table_name
WHERE column_name IN (value1,value2,..)

‫ ﻋﻠﻰ اﻟﺠﺪول اﻟﺘﺎﻟﻰ‬: ‫ﻣﺜﺎل‬
LastName
Hansen
Nordmann
Pettersen
Svendson

FirstName
Ola
Anna
Kari
Tove

Address
Timoteivn 10
Neset 18
Storgt 20
Borgvn 23

City
Sandnes
Sandnes
Stavanger
Sandnes

SELECT * FROM Persons
WHERE LastName IN ('Hansen','Pettersen')
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Hansen
Pettersen

FirstName
Ola
Kari

Address
Timoteivn 10
Storgt 20

City
Sandnes
Stavanger

‫وﻳﻤﻜﻦ أﻳﻀﺎ أﺳﺘﺨﺪاﻣﻪ ﻓﻰ رﺑﻂ أﻛﺜﺮ ﻣﻦ أﺳﺘﻌﻼم ﻣﻌﺎ‬
SELECT * FROM table1_name
WHERE FirstName in (SELECT * FROM table2_name)

‫ﻓﻰ ھﺬا اﻟﻤﺜﺎل ﻗﻤﻨﺎ ﺑﻌﺮض ﺟﻤﯿﻊ اﻟﻘﯿﻢ ﻣﻦ اﻟﺠﺪول اﻷول‬
‫ﻣﻮﺟﻮد‬FirstName ‫ ﺑﺸﺮط ان ﻳﻜﻮن اﻟﺤﻘﻞ‬Table1_name
table2_name ‫ﻓﻰ اﻟﺠﺪول اﻟﺜﺎﻧﻰ‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

١٣

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

BETWEEN ... AND ‫اﻟﻤﻌﺎﻣﻞ‬
‫ﻳﺴﺘﺨﺪم ﻟﻌﺮض ﻣﺠﻤﻮﻋﻪ ﺑﯿﺎﻧﺎت ﺑﯿﻦ ﻗﯿﻤﺘﯿﻦ ﻧﺼﻮص أو‬
‫أرﻗﺎم أو ﺗﺎرﻳﺦ‬
: ‫ﺑﻨﺎء اﻟﺠﻤﻠﻪ‬
SELECT column_name FROM table_name
WHERE column_name
BETWEEN value1 AND value2

‫ ﻋﻠﻰ اﻟﺠﺪول اﻟﺘﺎﻟﻰ‬: ‫ﻣﺜﺎل‬
LastName
Hansen
Nordmann
Pettersen
Svendson

FirstName
Ola
Anna
Kari
Tove

Address
Timoteivn 10
Neset 18
Storgt 20
Borgvn 23

City
Sandnes
Sandnes
Stavanger
Sandnes

‫ﻋﺮض اﻟﺒﯿﺎﻧﺎت اﻟﻤﻮﺟﻮده ﺑﯿﻦ اﻟﻘﯿﻤﺘﯿﻦ‬
SELECT * FROM Persons WHERE LastName
BETWEEN 'Hansen' AND 'Pettersen'
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Hansen
Nordmann

FirstName
Ola
Anna

Address
Timoteivn 10
Neset 18

City
Sandnes
Sandnes

‫ﻋﺮض اﻟﺒﯿﺎﻧﺎت اﻟﻐﯿﺮ ﻣﻮﺟﻮده ﺑﯿﻦ اﻟﻘﯿﻤﺘﯿﻦ‬
SELECT * FROM Persons WHERE LastName
NOT BETWEEN 'Hansen' AND 'Pettersen'
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Pettersen
Svendson

FirstName
Kari
Tove

Address
Storgt 20
Borgvn 23

City
Stavanger
Sandnes

‫ﺗﺴﺘﺨﺪم ﻧﻔﺲ اﻟﻄﺮﻳﻘﻪ ﻣﻊ اﻷرﻗﺎم واﻟﺘﻮارﻳﺦ ﻣﻊ ﻣﺮاﻋﺎه‬
. (#) ‫ان اﻟﺘﻮارﻳﺦ ﻻ ﺗﻜﻮن ﺑﯿﻦ ﻋﻼﻣﺘﻰ ) ' ( ﺑﻞ ﻳﺴﺘﺨﺪم‬
‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

١٤

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

Alias ‫اﻷﺳﻢ اﻟﻤﺴﺘﻌﺎر‬
‫ﻳﺴﺘﺨﺪم ﻓﻰ ﺗﻐﯿﯿﺮ اﺳﻢ اﻟﺠﺪول او اﻟﺤﻘﻞ ﻋﻨﺪ ﻋﺮﺿﻪ‬
AS ‫ﻧﺴﺘﺨﺪﻣﻪ ﻋﻦ ﻃﺮﻳﻖ ﻛﻠﻤﻪ‬
: ‫اﻟﺒﻨﺎء ﻓﻰ ﺣﺎﻟﻪ اﻟﺠﺪول‬
SELECT column FROM table as Table Alias

: ‫اﻟﺒﻨﺎء ﻓﻰ ﺣﺎﻟﻪ اﻟﺤﻘﻞ‬
SELECT column AS column_alias FROM table

‫ ﻋﻠﻰ اﻟﺠﺪول اﻟﺘﺎﻟﻰ‬: ‫ﻣﺜﺎل‬
This table (Persons):
LastName
Hansen
Svendson
Pettersen

FirstName
Ola
Tove
Kari

Address
Timoteivn 10
Borgvn 23
Storgt 20

City
Sandnes
Sandnes
Stavanger

SELECT LastName AS Family, FirstName AS Name
FROM Persons
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
(‫ﻧﻼﺣﻆ أﺳﻤﺎء اﻟﺤﻘﻮل ) اﻷﻋﻤﺪه‬
Family
Hansen
Svendson
Pettersen

Name
Ola
Tove
Kari

SELECT LastName, FirstName
FROM Persons AS Employees
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
Table Employees: ‫ﻧﻼﺣﻆ ان اﺳﻢ اﻟﺠﺪول‬
LastName
Hansen
Svendson
Pettersen

FirstName
Ola
Tove
Kari

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

١٥

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

‫اﻟﺮﺑﻂ ‪JOIN‬‬
‫ﻣﻮﺿﻮع ﻣﻦ اھﻢ ﻣﻮاﺿﯿﻊ ‪ SQL‬ھﻮ اﻟﺮﺑﻂ ﺑﯿﻦ اﻟﺠﺪاول اﻟﺬى‬
‫ﻳﺴﮫﻞ ﻛﺜﯿﺮ ﻓﻰ ﻋﺮض اﻟﺒﯿﺎﻧﺎت ﻣﻦ اﻟﺠﺪاول اﻟﺘﻰ ﺗﺤﺘﻮى‬
‫ﻋﻠﻰ ﻋﻼﻗﻪ ﻣﺜﻞ اﻟﻤﻮﻇﻒ و اﻟﻘﺴﻢ ‪.‬‬
‫ﻓﻰ ﺑﻌﺾ اﻷﺣﯿﺎن ﻧﺤﺘﺎج إﻟﻰ ﻋﺮض ﺑﯿﺎﻧﺎت ﻣﻦ ﺟﺪوﻟﯿﻦ او‬
‫أﻛﺜﺮ ﻟﺠﻌﻞ اﻟﻨﺘﯿﺠﻪ ﻛﺎﻣﻠﻪ ﻳﺠﺐ اﻧﺸﺎء ﻋﻼﻗﻪ ‪.‬‬
‫اﻟﺮﺑﻂ ﺑﯿﻦ اﻟﺠﺪاو ﻳﺘﻢ ﻋﻦ ﻃﺮﻳﻖ اﻟﻤﻔﺘﺎح اﻷﺳﺎﺳﻰ‬
‫)‪ (Primary Key‬اﻟﺤﻘﻞ اﻟﺬى ﻳﻜﻮن ﻣﻔﺘﺎح اﺳﺎﺳﻰ ﻻ‬
‫ﻳﻤﻜﻦ ﺗﻜﺮار اﻟﺒﯿﺎﻧﺎت ﺑﺪاﺧﻠﻪ ‪.‬‬
‫ﻓﻰ اﻟﺠﺪول اﻟﺘﺎﻟﻰ ﺣﻘﻞ )‪ (Employee_ID‬ھﻮ ﺣﻘﻞ ﻣﻔﺘﺎح‬
‫اﺳﺎﺳﻰ اﻟﺨﺎص ﺑﺠﺪول اﻟﻤﻮﻇﻔﯿﻦ ‪.‬‬
‫اﻣﺎ ﻓﻰ ﺟﺪول اﻟﺜﺎﻧﻰ اﻟﺤﻘﻞ )‪ (Order_ID‬ھﻮ ﺣﻘﻞ اﻟﻤﻔﺘﺎح‬
‫اﻻﺳﺎﺳﻰ ﻟﻠﺠﺪول وﻓﯿﻪ اﻳﻀﺎ ﻣﻔﺘﺎح ﺧﺎرﺟﻰ وھﻮ ﺣﻘﻞ‬
‫)‪.(Employee_ID‬‬
‫اﻟﺠﺪول اﻷول ‪Employees:‬‬
‫‪Name‬‬
‫‪Hansen, Ola‬‬
‫‪Svendson, Tove‬‬
‫‪Svendson, Stephen‬‬
‫‪Pettersen, Kari‬‬

‫‪Employee_ID‬‬
‫‪01‬‬
‫‪02‬‬
‫‪03‬‬
‫‪04‬‬
‫اﻟﺠﺪول اﻟﺜﺎﻧﻰ ‪Orders:‬‬

‫‪Employee_ID‬‬
‫‪01‬‬
‫‪03‬‬
‫‪03‬‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪١٦‬‬

‫‪Product‬‬
‫‪Printer‬‬
‫‪Table‬‬
‫‪Chair‬‬

‫‪Prod_ID‬‬
‫‪234‬‬
‫‪657‬‬
‫‪865‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

‫ ﻧﺮﻳﺪ ان ﻧﻌﺮف ﻣﻦ ﻃﻠﺐ ﻣﻨﺘﺞ وﻣﺎ ھﻮ اﻟﻤﻨﺘﺞ‬: ‫ﻣﺜﺎل‬
SELECT Employees.Name, Orders.Product
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
Name
Hansen, Ola
Svendson, Stephen
Svendson, Stephen

Product
Printer
Table
Chair

(Printer) ‫ ﻧﺮﻳﺪ ان ﻧﻌﺮف ﻣﻦ ﻃﻠﺐ اﻟﻤﻨﺘﺞ‬: ‫ﻣﺜﺎل‬
SELECT Employees.Name
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
AND Orders.Product='Printer'
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
Name
Hansen, Ola

: INNER JOIN ‫ﻣﺜﺎل ﻋﻠﻰ اﺳﺘﺨﺪام‬
: ‫ﺑﻨﺎء اﻟﺠﻤﻠﻪ‬
SELECT field1, field2, field3
FROM first_table
INNER JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield

: ‫ﺗﻨﻔﯿﺬ اﻟﺠﻤﻠﻪ ﻋﻠﻰ اﻟﻤﺜﺎل‬
SELECT Employees.Name, Orders.Product
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
Name
Hansen, Ola
Svendson, Stephen
Svendson, Stephen

Product
Printer
Table
Chair

‫ ﺑﻌﺮض ﺟﻤﯿﻊ اﻟﺒﯿﺎﻧﺎت‬INNER JOIN ‫ﺗﻘﻮم اﻟﺠﻤﻠﻪ‬
‫اﻟﻤﺸﺘﺮﻛﻪ ﺑﯿﻦ اﻟﺠﺪوﻟﯿﻦ‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

١٧

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

: LEFT JOIN ‫ﻣﺜﺎل ﻋﻠﻰ أﺳﺘﺨﺪام‬
: ‫ﺑﻨﺎء اﻟﺠﻤﻠﻪ‬
SELECT field1, field2, field3
FROM first_table
LEFT JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield

: ‫ﺗﻨﻔﯿﺬ اﻟﺠﻤﻠﻪ ﻋﻠﻰ اﻟﻤﺜﺎل‬
SELECT Employees.Name, Orders.Product
FROM Employees
LEFT JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Svendson, Stephen
Pettersen, Kari

Product
Printer
Table
Chair

‫ ﺑﻌﺮض ﺟﻤﯿﻊ اﻟﺒﯿﺎﻧﺎت ﻣﻦ اﻟﺠﺪول‬LEFT JOIN ‫ﺗﻘﻮم اﻟﺠﻤﻠﻪ‬
‫ ﺣﺘﻰ ﻟﻮ ﻟﻢ ﺗﻮﺟﺪ ﻓﻰ اﻟﺠﺪول اﻟﺜﺎﻧﻰ‬Employee ‫اﻷول‬
: RIGHT JOIN ‫ﻣﺜﺎل ﻋﻠﻰ أﺳﺘﺨﺪام‬
: ‫ﺑﻨﺎء اﻟﺠﻤﻠﻪ‬
SELECT field1, field2, field3
FROM first_table
RIGHT JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield

: ‫ﺗﻨﻔﯿﺬ اﻟﺠﻤﻠﻪ ﻋﻠﻰ اﻟﻤﺜﺎل‬
SELECT Employees.Name, Orders.Product
FROM Employees
RIGHT JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
Name
Hansen, Ola
Svendson, Stephen
Svendson, Stephen

Product
Printer
Table
Chair

‫ ﺑﻌﺮض ﺟﻤﯿﻊ اﻟﺒﯿﺎﻧﺎت ﻣﻦ‬RIGHT JOIN ‫ﺗﻘﻮم اﻟﺠﻤﻠﻪ‬
‫ ﺣﺘﻰ ﻟﻮ ﻟﻢ ﺗﻮﺟﺪ ﻓﻰ اﻟﺠﺪول اﻷول‬Orders ‫اﻟﺠﺪول اﻟﺜﺎﻧﻰ‬
: INNER JOIN ‫ﻣﺜﺎل ﻋﻠﻰ أﺳﺘﺨﺪام ﺟﻤﻠﻪ‬
‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

١٨

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

(Printer) ‫ﻧﺮﻳﺪ ﻋﺮض اﺳﻤﺎء اﻟﻤﻮﻇﻔﯿﻦ اﻟﺬن ﻃﻠﺒﻮا اﻟﻤﻨﺘﺞ‬
SELECT Employees.Name
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
WHERE Orders.Product = 'Printer'

Result: ‫اﻟﻨﺘﯿﺠﻪ‬
Name
Hansen, Ola

. JOIN ‫وﺑﮫﺬا ﻧﻜﻮن اﻧﺘﮫﯿﻨﺎ ﻣﻦ ﺷﺮح ﺟﻤﻠﻪ‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

١٩

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

UNION and UNION ALL ‫اﻟﺪﻣﺞ‬
‫ﺗﺴﺘﺨﺪم ﻟﺪﻣﺞ ﺣﻘﻠﯿﻦ ﻣﻦ ﺟﺪوﻟﯿﻦ ﻣﺨﺘﻠﻔﯿﻦ وﻟﻜﻦ ﻳﺠﺐ‬
‫ان ﻳﻜﻮن ﻧﻮع اﻟﺒﯿﺎﻧﺎت ﻓﻰ اﻟﺤﻘﻠﯿﻦ واﺣﺪ اى ﻳﻜﻮن ﻧﺼﻮص او‬
‫………إﻟﺦ‬. ‫ارﻗﺎم‬
: ‫اﻟﺒﻨﺎء ﻟﻠﺠﻤﻠﻪ ﻳﻜﻮن ﻛﺎﻟﺘﺎﻟﻰ‬
SQL Statement 1
UNION
SQL Statement 2

: ‫ﻣﺜﺎل‬
Employees_Norway:
E_ID
01
02
03
04

E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari

Employees_USA:
E_ID
01
02
03
04

E_Name
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen

: ‫ ﻓﻰ ﻛﻞ ﻣﻦ اﻟﺠﺪوﻟﯿﻦ‬E_NAME ‫ﻧﺮﻳﺪ دﻣﺞ اﻟﺤﻘﻞ‬
SELECT E_Name FROM Employees_Norway
UNION
SELECT E_Name FROM Employees_USA
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen

. ‫ﻧﻼﺣﻆ ان اﻟﻨﺘﯿﺠﻪ ﻇﮫﺮت ﺑﺪون ﺗﻜﺮار ﻟﻠﺒﯿﺎﻧﺎت‬
: UNION ALL ‫أﺳﺘﺨﺪام‬
‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

٢٠

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

‫ﻣﺜﻞ أﺳﺘﺨﺪام ‪ UNION‬اﻟﻔﺮق اﻧﻪ ﻳﻘﻮم ﺑﻌﺮض ﺟﻤﯿﻊ‬
‫اﻟﺒﯿﺎﻧﺎت ﺣﺘﻰ ﻟﻮ ﻳﻮﺟﺪ ﺗﻜﺮار ‪.‬‬
‫اﻟﺒﻨﺎء ﻟﻠﺠﻤﻠﻪ ﻳﻜﻮن ﻛﺎﻟﺘﺎﻟﻰ ‪:‬‬
‫‪SQL Statement 1‬‬
‫‪UNION ALL‬‬
‫‪SQL Statement 2‬‬

‫ﻣﺜﺎل ‪ :‬ﻧﺮﻳﺪ دﻣﺞ اﻟﺤﻘﻞ ‪ E_NAME‬ﻓﻰ اﻟﺠﺪوﻟﯿﻦ اﻟﺴﺎﺑﻘﯿﻦ‬
‫‪SELECT E_Name FROM Employees_Norway‬‬
‫‪UNION ALL‬‬
‫‪SELECT E_Name FROM Employees_USA‬‬
‫اﻟﻨﺘﯿﺠﻪ ‪Result:‬‬
‫‪E_Name‬‬
‫‪Hansen, Ola‬‬
‫‪Svendson, Tove‬‬
‫‪Svendson, Stephen‬‬
‫‪Pettersen, Kari‬‬
‫‪Turner, Sally‬‬
‫‪Kent, Clark‬‬
‫‪Svendson, Stephen‬‬
‫‪Scott, Stephen‬‬

‫ﻧﻼﺣﻆ ﻇﮫﻮر ﺟﻤﯿﻊ اﻟﺒﯿﺎﻧﺎت ﻣﻊ ﺗﻜﺮار اﺣﺪ اﻷﺳﻤﺎء‬
‫اﻟﻤﺸﺘﺮك ﻓﻰ اﻟﺠﺪوﻟﯿﻦ ‪.‬‬

‫)اﻷن ﻧﻨﺘﻘﻞ إﻟﻰ درس ﻣﮫﻢ ﺟﺪا ﻓﻰ ﻟﻐﻪ ‪ SQL‬اﻻ وھﻮ‬
‫اﻟﺪوال ‪(Functions‬‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪٢١‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

SQL Functions ‫اﻟﺪوال‬
‫ ﺑﮫﺎ اﻟﻜﺜﯿﺮ ﻣﻦ اﻟﺪوال اﻟﻌﺪدﻳﻪ واﻟﺤﺴﺎﺑﯿﻪ‬SQL ‫ﻟﻐﻪ‬
: ‫اﻟﺒﻨﺎء اﻷﺳﺎﺳﻰ ﻷى داﻟﻪ‬
SELECT function(column) FROM table

: ‫اﻟﺪوال‬
‫اﻟﻮﺻﻒ‬

‫اﻟﺪاﻟﻪ‬

‫ﻹﻳﺠﺎد اﻟﻮﺳﻂ اﻟﺤﺴﺎﺑﻰ ﻟﻠﺤﻘﻞ اﻟﻤﺤﺪد‬

AVG(column)

‫ﻣﻌﺮﻓﻪ ﻋﺪد اﻟﺼﻔﻮف )اﻟﺴﺠﻼت( ﻓﻰ اﻟﺤﻘﻞ ﺑﺪون اﻟﺴﺠﻼت اﻟﻔﺎرﻏﻪ‬

COUNT(column)

‫ﻣﻌﺮﻓﻪ ﻋﺪد اﻟﺼﻔﻮف ﻓﻰ اﻟﺠﺪول‬

COUNT(*)

‫ﻣﻌﺮﻓﻪ ﻗﯿﻤﻪ اول ﺳﺠﻞ ﻓﻰ اﻟﺤﻘﻞ‬

First(column)

‫ﻣﻌﺮﻓﻪ ﻗﯿﻤﻪ أﺧﺮ ﺳﺠﻞ ﻓﻰ اﻟﺤﻘﻞ‬

last(column)

‫ﻣﻌﺮﻓﻪ أﻛﺒﺮ ﻗﯿﻤﻪ ﺳﺠﻞ ﻓﻰ اﻟﺤﻘﻞ‬

Max(column)

‫ﻣﻌﺮﻓﻪ أﺿﻐﺮ ﻗﯿﻤﻪ ﺳﺤﻞ ﻓﻰ اﻟﺤﻘﻞ‬

Min(column)

‫ﻣﻌﺮﻓﻪ إﺟﻤﺎﻟﻰ اﻟﻘﯿﻢ ﻓﻰ اﻟﺤﻘﻞ‬

SUM(column)

SQL SERVER ‫ﻋﺪد اﻟﺴﺠﻼت ﻓﻰ اﻟﺤﻘﻞ ﺑﺪون ﺗﻜﺮار ﺗﻌﻤﻞ ﻓﻘﻂ ﻋﻠﻰ‬

COUNT(DISTINCT column)

: ‫ﻣﺠﻤﻮﻋﻪ ﻣﻦ اﻷﻣﺜﻠﻪ‬
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT
SELECT

AVG(Column) From Table
COUNT(column) From Table
COUNT(*) From Table
First(column) From Table
last(column) From Table
Max(column) From Table
Min(column) From Table
SUM(column) From Table
COUNT(DISTINCT column) From Table

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

٢٢

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

SQL GROUP BY and HAVING ‫اﻟﺘﺠﻤﯿﻊ واﻟﻔﺮز‬

‫ ﻛﺜﯿﺮا ﻣﺎ ﺗﺤﺘﺎج إﻟﻰ‬SUM ‫اﻟﺪوال اﻟﻌﺪدﻳﻪ ﻣﺜﻞ اﻟﺪاﻟﻪ‬
GROUP BY ‫اﻟﺘﺠﻤﯿﻊ‬
: ‫ﺑﻨﺎء اﻟﺠﻤﻠﻪ‬
SELECT column,SUM(column) FROM table GROUP BY column

‫ ﻋﻠﻰ اﻟﺠﺪول اﻟﺘﺎﻟﻰ‬: ‫ﻣﺜﺎل ﻟﻠﺘﻮﺿﯿﺢ‬
Company
W3Schools
IBM
W3Schools

Amount
5500
4500
7100

:‫ﻧﻨﻔﺬ اﻟﺠﻤﻠﻪ اﻟﺘﺎﻟﯿﻪ‬
SELECT Company, SUM(Amount) FROM Sales
Returns this result: ‫اﻟﻨﺘﯿﺠﻪ‬
Company
W3Schools
IBM
W3Schools

SUM(Amount)
17100
17100
17100

‫ﻧﻼﺣﻆ ان ﺟﻤﯿﻊ اﻟﺴﺠﻼت اﺧﺬت اﻟﻤﺠﻤﻮع ﻛﻠﻪ وﻟﻢ ﻧﻌﺮف‬
. ‫ﻣﺠﻤﻮع ﻛﻞ ﺳﺠﻞ‬
: GROUP BY ‫اﻷن ﻧﺠﺮب ﺟﻤﻠﻪ اﻷﺳﺘﻌﻼم ﺑﻌﺪ إﺿﺎﻓﻪ‬
SELECT Company,SUM(Amount) FROM Sales
GROUP BY Company
Returns this result: ‫اﻟﻨﺘﯿﺠﻪ‬
Company
W3Schools
IBM

SUM(Amount)
12600
4500

‫ﻧﻼﺣﻆ اﻧﻪ ﺗﻢ ﺟﻤﻊ ﻛﻞ ﺳﺠﻞ وﺣﺪه وأﺻﺒﺤﺖ اﻟﻨﺘﯿﺠﻪ اوﺿﺢ‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

٢٣

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

‫ ﺗﺴﺘﺨﺪم ﻟﻔﺮز اﻟﺒﯿﺎﻧﺎت ﺣﺴﺐ ﺷﺮط ﻣﻌﯿﻦ‬HAVING ‫اﻟﺪاﻟﻪ‬
: ‫ﺑﻨﺎء اﻟﺠﻤﻠﻪ‬
SELECT column,SUM(column) FROM table
GROUP BY column
HAVING SUM(column) condition value

: ‫ﺗﻄﺒﯿﻖ ﻋﻠﻰ ﻧﻔﺲ اﻟﻤﺜﺎل اﻟﺴﺎﺑﻖ‬
Company
W3Schools
IBM
W3Schools

Amount
5500
4500
7100

: ‫ﻧﻨﻔﺬ ﺟﻤﻠﻪ اﻷﺳﺘﻌﻼم اﻟﺘﺎﻟﯿﻪ‬
SELECT Company,SUM(Amount) FROM Sales
GROUP BY Company
HAVING SUM(Amount)>10000
Returns this result : ‫اﻟﻨﺘﯿﺠﻪ‬
Company
W3Schools

SUM(Amount)
12600

١٠٠٠٠ ‫ﻧﻼﺣﻆ اﻧﻪ ﺗﻢ ﺗﺤﻘﻖ اﻟﺸﺮط وﻋﺮض اﻟﺒﯿﺎﻧﺎت اﻛﺒﺮ ﻣﻦ‬
.‫ﻓﻘﻂ‬

‫ﺗﻢ ﺑﺤﻤﺪ اﷲ ﺗﻌﺎﻟﻰ اﻟﺠﺰء اﻟﺜﺎﻧﻰ ﻣﻦ اﻟﺪرس واﻟﺨﺎص‬
SQL FUNCTION ‫ﺑـ‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

٢٤

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

‫ﻓﻰ ھﺬا اﻟﺠﺰء ﻣﻦ اﻟﺪرس ﺳﻮف ﻧﻘﻮم ﺑﺸﺮح‬
‫)‪Data Definition Language (DDL‬‬
‫ھﻮ اﻟﻘﺴﻢ اﻟﻤﺴﺌﻮل ﻋﻦ ‪ :‬ﺗﻌﺮﻳﻒ اﻟﺒﯿﺎﻧﺎت‬
‫‪ : Create Database -١‬ﻷﻧﺸﺎء ﻗﺎﻋﺪه ﺑﯿﺎﻧﺎت ﺟﺪﻳﺪه‬
‫‪ : Create Table -٢‬ﻷﻧﺸﺎء ﺟﺪول داﺧﻞ ﻗﺎﻋﺪه ﺑﯿﺎﻧﺎت‬
‫‪ : ALTER TABLE -٣‬ﻟﻠﺘﻌﺪﻳﻞ ﻓﻰ اﻟﺠﺪول‬
‫‪ : DROP TABLE -٤‬ﻟﺤﺬف اﻟﺠﺪول ﻣﻦ ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت‬
‫‪ : CREATE INDEX-٥‬ﻷﻧﺸﺎء ﻣﻔﺘﺎح ﻟﻠﺒﺤﺚ )اﻟﻔﮫﺎرس(‬
‫‪ : DROP INDEX -٦‬ﻟﺤﺬف ﻣﻔﺘﺎح اﻟﺒﺤﺚ‬

‫اوﻻ اﻧﺸﺎء ﻗﺎﻋﺪه ﺑﯿﺎﻧﺎت ‪: Create Database‬‬
‫ﺑﻨﺎء اﻟﺠﻤﻠﻪ ﻛﺎﻟﺘﺎﻟﻰ‬
‫‪CREATE DATABASE database_name‬‬

‫ﻃﺒﻌﺎ ﻧﺤﺪد اﺳﻢ ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت‬
‫ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت دﻳﻪ ھﺘﻜﻮن ﻓﺎﺿﯿﻪ ﻳﻌﻨﻰ ﻣﻔﯿﺶ ﺟﻮاھﺎ اى‬
‫ﺟﺪاول ‪.‬‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪٢٥‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

‫ﻷﻧﺸﺎء ﺟﺪول داﺧﻞ ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ‪:Create Table‬‬
‫ﻳﻜﻮن ﺑﻨﺎء اﻟﺠﻤﻠﻪ ﻛﺎﻟﺘﺎﻟﻰ ‪:‬‬
‫‪CREATE TABLE table_name‬‬
‫(‬
‫‪column_name1 data_type,‬‬
‫‪column_name2 data_type,‬‬
‫‪.......‬‬
‫)‬

‫ﻣﺜﺎل ﻋﻠﻰ أﻧﺸﺎء ﺟﺪول ‪:‬‬
‫‪CREATE TABLE Person‬‬
‫(‬
‫‪LastName text(30),‬‬
‫‪FirstName text(30),‬‬
‫‪Address text(150),‬‬
‫)‪Age (Number‬‬
‫)‬

‫اﻻن ﻳﻤﻜﻨﻨﺎ اﻧﺸﺎء ﺟﺪوا وﺗﺤﺪﻳﺪ اﻟﺤﻘﻮل واﻧﻮاع وﺣﺠﻢ‬
‫اﻟﺒﯿﺎﻧﺎت ﻓﯿﮫﺎ ‪.‬‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪٢٦‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

‫اﻷن اﻧﺸﺎء اﻟﻔﮫﺎرس ‪: CREATE INDEX‬‬
‫اﻟﻔﮫﺮس ﻳﺼﻤﻢ ﻓﻰ اﻟﺠﺪول ﺣﺘﻰ ﻳﺠﻌﻞ ﻋﻤﻠﯿﻪ اﻷﺳﺘﻌﻼم‬
‫أﺳﺮع ﻛﻤﺎ ﻳﻤﻜﻦ اﻧﺸﺎء اﻛﺜﺮ ﻣﻦ ﻓﮫﺮس ﻧﻔﺲ اﻟﺠﺪول‬
‫اﻟﻤﺴﺘﺨﺪم ﻻ ﻳﺮى ھﺬه اﻟﻔﮫﺎرس اﻧﻤﺎ ھﻰ ﻟﺘﺴﺮع ﻋﻤﻠﯿﻪ‬
‫اﻷﺳﺘﻌﻼم ﻓﻘﻂ ‪.‬‬
‫ھﻨﺎك ﻧﻮﻋﯿﻦ ﻣﻦ اﻟﻔﮫﺎرس اﻟﻨﻮع اﻷول ﻻ ﻳﻤﻜﻦ ان ﻳﺘﻜﺮر ﺑﻪ‬
‫اﻟﺒﯿﺎﻧﺎت اﻣﺎ اﻟﻨﻮع اﻟﺜﺎﻧﻰ ﻳﻤﻜﻦ ﺗﻜﺮار اﻟﺒﯿﺎﻧﺎت ﺑﻪ ‪.‬‬
‫اﻟﺒﻨﺎء ﻟﻠﻔﮫﺮس ﻣﻦ اﻟﻨﻮع اﻟﻔﺮﻳﺪ )اﻟﺬى ﻻ ﻳﺘﻜﺮر(‬

‫‪Unique Index‬‬

‫‪CREATE UNIQUE INDEX index_name‬‬
‫)‪ON table_name (column_name‬‬

‫اﻟﺒﻨﺎء ﻟﻠﻔﮫﺮس ﻣﻦ اﻟﻨﻮع اﻟﻌﺎدى‬

‫‪Simple Index‬‬
‫‪CREATE INDEX index_name‬‬
‫)‪ON table_name (column_name‬‬

‫ﻣﺜﺎل ‪:‬‬
‫‪CREATE INDEX PersonIndex‬‬
‫)‪ON Person (LastName‬‬

‫ﻻﻧﺸﺎء ﻓﮫﺮس ﺑﺘﺮﺗﯿﺐ ﻋﻜﺴﻰ ‪:‬‬
‫‪CREATE INDEX PersonIndex‬‬
‫)‪ON Person (LastName DESC‬‬

‫ﻻﻧﺸﺎء ﻓﮫﺮس ﻓﻰ ﺣﻘﻠﯿﻦ ﻓﻰ ﻧﻔﺲ اﻟﺠﺪول ‪:‬‬
‫‪CREATE INDEX PersonIndex‬‬
‫)‪ON Person (LastName, FirstName‬‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪٢٧‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

‫اﻷﻣﺮ ‪Drop‬‬
‫‪Drop‬ﺗﺮﺟﻤﺘﮫﺎ اﻟﺤﺮﻓﯿﻪ إﻟﻘﺎء وﻟﻜﻨﻨﺎ ھﻨﺎ ﻧﺴﺘﺨﺪﻣﮫﺎ ﻛﺄﻣﺮ‬
‫ﺣﺬف وﻟﻜﻦ ﺣﺬف اﻳﻪ ) ﻗﺎﻋﺪه ﺑﯿﺎﻧﺎت – ﻓﮫﺮس – ﺟﺪول(‬
‫اوﻻ ﺣﺬف ﻗﺎﻋﺪ ﺑﯿﺎﻧﺎت ‪ :‬ﻳﻜﻮن ﺑﻨﺎء اﻟﺠﻤﻠﻪ ﻛﺎﻟﺘﺎﻟﻰ‬
‫‪DROP DATABASE database_name‬‬

‫ﺛﺎﻧﯿﺎ ﺣﺬف ﺟﺪول ﻣﻦ ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ‪ :‬ﻳﻜﻮن ﺑﻨﺎء اﻟﺠﻤﻠﻪ‬
‫ﻛﺎﻟﺘﺎﻟﻰ ‪:‬‬
‫‪DROP TABLE table_name‬‬

‫ﺛﺎﻟﺜﺎ ﺣﺬف ﻓﮫﺮس ‪ :‬وﻳﺨﺘﻠﻒ اﻟﺒﻨﺎء ﺣﺴﺐ ﻧﻮع ﻗﺎﻋﺪه‬
‫اﻟﺒﯿﺎﻧﺎت‬
‫ﻣﺜﺎل ‪:‬‬
‫ﻧﻮع ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ‪Syntax for Microsoft SQLJet (and Microsoft Access):‬‬

‫‪DROP INDEX index_name ON table_name‬‬
‫ﻧﻮع ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ‪Syntax for MS SQL Server:‬‬

‫‪DROP INDEX table_name.index_name‬‬
‫ﻧﻮع ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ‪Syntax for IBM DB2 and Oracle:‬‬

‫‪DROP INDEX index_name‬‬
‫ﻧﻮع ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ‪Syntax for MySQL:‬‬

‫‪ALTER TABLE table_name DROP INDEX index_name‬‬

‫أﺧﯿﺮ اﻣﺮ ﺣﺬف اﻟﺒﯿﺎﻧﺎت ﻣﻦ داﺧﻞ اﻟﺠﺪول دون ﺣﺬف‬
‫اﻟﺠﺪول ‪:‬‬
‫‪TRUNCATE TABLE table_name‬‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪٢٨‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

ALTER TABLE ‫اﻷﻣﺮ‬
‫ﻳﺴﺘﺨﺪم ﻓﻰ اﻟﺘﻌﺪﻳﻞ ﻋﻠﻰ اﻟﺠﺪول ﻣﻦ إﺿﺎﻓﻪ وﺣﺬف‬
. (‫أﻋﻤﺪه )ﺣﻘﻮل‬
‫ ﻳﻜﻮن ﺑﻨﺎء اﻟﺠﻤﻠﻪ ﻛﺎﻟﺘﺎﻟﻰ‬: ‫ﻹﺿﺎﻓﻪ ﺣﻘﻞ‬
ALTER TABLE table_name
ADD column_name datatype

: ‫ ﻳﻜﻮن ﺑﻨﺎء اﻟﺠﻤﻠﻪ ﻛﺎﻟﺘﺎﻟﻰ‬: ‫ﻟﺤﺬف ﺣﻘﻞ‬
ALTER TABLE table_name
DROP COLUMN column_name

‫ ﻋﻠﻰ اﻟﺠﺪول اﻟﺘﺎﻟﻰ‬: ‫ﻣﺜﺎل‬
Person:
LastName
Pettersen

FirstName
Kari

Address
Storgt 20

: ‫ﻹﺿﺎﻓﻪ ﺣﻘﻞ ﺟﺪﻳﺪ‬
To add a column named "City" in the "Person" table:

ALTER TABLE Person ADD City varchar(30)
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Pettersen

FirstName
Kari

Address
Storgt 20

City

: ‫ﻟﺤﺬف ﺣﻘﻞ ﻣﻦ اﻟﺠﺪول‬
To drop the "Address" column in the "Person" table:

ALTER TABLE Person DROP COLUMN Address
Result: ‫اﻟﻨﺘﯿﺠﻪ‬
LastName
Pettersen

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

FirstName
Kari

٢٩

City

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

SELECT INTO ‫ﺟﻤﻠﻪ‬
‫ﺗﺴﺘﺨﺪم ﻋﺎده ﻓﻰ ﺣﻔﻆ ﻧﺴﺨﻪ أﺣﺘﯿﺎﻃﯿﻪ ﻣﻦ اﻟﺠﺪول‬
: ‫ﺑﻨﺎء اﻟﺠﻤﻞ ﻛﺎﻟﺘﺎﻟﻰ‬
SELECT column_name(s) INTO newtable [IN externaldatabase]
FROM source

: ‫ﻟﻌﻤﻞ ﻧﺴﺨﻪ إﺣﺘﯿﺎﻃﯿﻪ ﻣﻦ اﻟﺠﺪول ﻓﻰ ﻧﻔﺲ اﻟﻘﺎﻋﺪه‬
SELECT * INTO Persons_backup
FROM Persons

IN ‫ ﻧﺴﺘﺨﺪم‬: ‫ﻟﻨﺴﺦ اﻟﺠﺪول ﻟﻘﺎﻋﺪه ﺑﯿﺎﻧﺎت أﺧﺮى‬
SELECT Persons.* INTO Persons IN 'Backup.mdb'
FROM Persons

: ‫ﻳﻤﻜﻦ أﻳﻀﺎ ﻧﺴﺦ ﺣﻘﻮل ﻣﺤﺪده ﻓﻘﻂ‬
SELECT LastName,FirstName INTO Persons_backup
FROM Persons

Where : ‫ﻧﻔﺲ اﻟﻤﺜﺎل اﻟﺴﺎﺑﻖ وﻟﻜﻦ ﻣﻊ إدﺧﺎل ﺷﺮط‬
SELECT LastName,Firstname INTO Persons_backup
FROM Persons
WHERE City='Sandnes'

: ‫إذا اردﻧﺎ ﻧﺴﺦ اﻟﺠﺪول ﻟﻜﻦ ﻓﻰ وﺟﻮد ﻋﻼق ﻣﻊ ﺟﺪول أﺧﺮ‬
SELECT Employees.Name,Orders.Product
INTO Empl_Ord_backup
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

٣٠

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

‫ﺟﻤﻠﻪ ‪CREATE VIEW‬‬
‫ﺗﺴﺘﺨﺪم ﻻﻧﺸﺎء ﺟﻤﻠﻪ ﺗﻌﺮض ﺑﯿﺎﻧﺎت ﺗﺤﺖ ﺷﺮط ﻣﻌﯿﻦ‬
‫ﻳﻤﻜﻦ أﺳﺘﺨﺪام اى ﻣﻦ ﺟﻤﻞ ‪ SQL‬ﺑﮫﺎ ﻣﺜﻞ ﻋﻤﻞ ﻋﻼﻗﻪ‬
‫او إﺿﺎﻓﻪ داﻟﻪ ﻣﻦ اﻟﺪوال ‪.‬‬
‫ﻳﻜﻮن ﺑﻨﺎﺋﮫﺎ اﻟﻌﺎم ﻛﺎﻟﺘﺎﻟﻰ ‪:‬‬
‫‪CREATE VIEW view_name AS‬‬
‫)‪SELECT column_name(s‬‬
‫‪FROM table_name‬‬
‫‪WHERE condition‬‬

‫ﻣﻼﺣﻈﺎت ھﺎﻣﻪ ‪:‬‬
‫‪ -١‬ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ﻻ ﺗﺘﺄﺛﺮ ﺑﻤﺎ ﻳﺘﻢ ﺗﻨﻔﯿﺬه ﻣﻦ ﺷﺮوط او دوال‬
‫‪ -٢‬ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ﻻ ﺗﻘﻮم ﺑﺤﻔﻆ اﻟﻨﺎﺗﺞ ﻓﻰ ﺟﺪول واﻧﻤﺎ ﻳﻘﻮم‬
‫ﻣﺤﺮك ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ﺑﺘﻨﻔﯿﺬ اﻟﺠﻤﻠﻪ ﻛﻞ ﻣﺮه ﺗﻘﻮم‬
‫ﺑﺘﺸﻐﯿﻠﻪ ﻓﯿﮫﺎ‬

‫ﺑﻌﺾ اﻷﻣﺜﻠﻪ اﻟﻤﺒﻨﯿﻪ ﻋﻠﻰ ﻗﺎﻋﺪه اﻟﺒﯿﺎﻧﺎت ‪: Northwind‬‬
‫ﻣﺜﺎل ‪: ١‬‬
‫‪CREATE VIEW [Current Product List] AS‬‬
‫‪SELECT ProductID,ProductName‬‬
‫‪FROM Products‬‬
‫‪WHERE Discontinued=No‬‬

‫ﻛﯿﻒ ﻳﺘﻢ ﺗﺸﻐﯿﻠﻪ ‪ :‬ﻋﻦ ﻃﺮﻳﻖ اﻟﺠﻤﻠﻪ اﻷﺗﯿﻪ‬
‫]‪SELECT * FROM [Current Product List‬‬

‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ ‪Arab Team 2000‬‬

‫‪٣١‬‬

‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

: ٢ ‫ﻣﺜﺎل‬
CREATE VIEW [Products Above Average Price] AS
SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)

:‫ﻳﺘﻢ ﻋﺮﺿﻪ ﺑﺎﻟﺠﻤﻠﻪ‬
SELECT * FROM [Products Above Average Price]

: ٣ ‫ﻣﺜﺎل‬
CREATE VIEW [Category Sales For 1997] AS
SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales
FROM [Product Sales for 1997]
GROUP BY CategoryName

: ‫ﻳﺘﻢ ﻋﺮﺿﻪ ﺑﺎﻟﺠﻤﻠﻪ‬
SELECT * FROM [Category Sales For 1997]

: ‫وﻳﻤﻜﻦ أﺳﺘﺨﺪام اﻟﺸﺮط اﺛﻨﺎء ﻋﺮض اﻟﺒﯿﺎﻧﺎت‬
SELECT * FROM [Category Sales For 1997]
WHERE CategoryName='Beverages'

‫ﺗﻢ ﺑﺤﻤﺪ اﷲ اﻷﻧﺘﮫﺎء ﻣﻦ اﻟﺪروس‬
‫وﻧﺪﻋﻮا اﷲ ان ﻳﻮﻓﻚ اﻟﺠﻤﯿﻊ ﻟﻤﺎ ﻓﯿﻪ اﻟﺨﯿﺮ‬
‫اﺳﺌﻠﻜﻢ اﻟﺪﻋﺎء ﺑﻈﮫﺮ اﻟﻐﺎﺋﺐ‬
‫ﺗﺤﯿﺎﺗﻰ ﻟﻠﺠﻤﯿﻊ أﺣﻤﺪ ﺣﺎﻣﺪ‬
GENIUS-IT@HOTMAIL.COM
‫ﻋﻀﻮ ﻓﻰ‬
‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬
WWW.ARABTEAM2000.COM
GENIUS-IT ‫ﺑﺄﺳﻢ‬
‫ﻗﺎم ﺑﺎﻟﻨﻘﻞ واﻟﺘﺮﺟﻤﻪ أﺣﻤﺪ ﺣﺎﻣﺪ‬

٣٢

Arab Team 2000 ‫اﻟﻔﺮﻳﻖ اﻟﻌﺮﺑﻰ ﻟﻠﺒﺮﻣﺠﻪ‬

Sign up to vote on this title
UsefulNot useful