P. 1
المرجع الشامل فى جمل SQL

المرجع الشامل فى جمل SQL

|Views: 6,802|Likes:
المرجع الشامل في جمل SQL كتيب يتحدث عن جمل SQL. تم تحميل من قبل موقع برامج سوفت اكس جيم http://www.softxgame.com
المرجع الشامل في جمل SQL كتيب يتحدث عن جمل SQL. تم تحميل من قبل موقع برامج سوفت اكس جيم http://www.softxgame.com

More info:

Published by: أكبر مكتبة كتب عربية on Sep 09, 2009
حقوق الطبع:Public Domain

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/11/2014

pdf

text

original

!#$%&' (*$+', -.

$/', Arab Team 2000 0 1#23 1435 46$7',8 9:;'2* <2=




1%', 9%=8 >8, ? @8, A$B', (D EGH I2#J' A$B', L1M# $NP, Q,
R=S# TU 'S:;# V8L1',
COM . Schools 3 W . WWW
W8 2X746$7* Y:D 2I, Z4= 1:'8 S [ 2X* E$]^M', _2:;', `+* 9.1+W8 b]

QSc]e ' 9#2f R6$# TU EL2%U A$B', ,GH 94! SQL


(D #1:# >8, SQL :

g2I2]%', 1U,S= !'2+#8 hSi1' je2"', g2^' T# ]e2]= ^' (H


(H 2# SQL : Structured Query Language

^' ?2;* #K+7ek, g2 ]&c]X', ) .Sf ]D$3 46$7', (H (;+. (

0 - ^' (H 1X+4* \2l', je2"', g2^' T# ]e2]= ANSI
m - 1U,S:' hSi1', T# n;c4W g2I2]%',
o - E1U2:', T# g2I2]%', p,$l7eq T# n;c4W
r - g2I2]%', E1U2= ('q g2I2]* D2[q T# n;c4W
s - E1U2:', T# g2I2]* tG3 T# T# n;c4W
v - &!C4', g2I2]%', 9.1+W T# n;c4W
w - $]i5 , xX/',8 x&+7', &Xe ^' (H








!#$%&' (*$+', -.$/', Arab Team 2000 m 1#23 1435 46$7',8 9:;'2* <2=
^' SQL ]e2]= ^' (H :
+4* \2l', ]e2]:', g2^&', T# ^' (H 1X
ANSI (American National Standards I nstitute)
g2I2]%', 1U,S= xyI !'2+#8 hSi@ T# n;c4W Database System
946 SQL 9z# g2I2]%', 1U,S= {#,$* R# 94+W :
Ms-Access, Ms-SQL Server, DB2, Oracle, etc.

^' xC:;W SQL T]4C= ('q :

1- SQL Data Manipulation Language (DML)

S|C4', xC:', SH TU h : g2I2]%', !'2+#

0 - Select : g2I2]%', E1U2= T# g2I2]%', p,$l7eq
m - INSERT I NTO : E1.16 g2I2]* D2[q
o - Update : &!C4', g2I2]%', (&U 9.1+7',
r - Delete : E1U2:', T# g2I2]%', tG3

2- Data Definition Language (DDL)
4', xC:', SH TU hS|C : g2I2]%', }.$+W

0 - Create Database : E1.16 g2I2]* E1U2= ?2BIk
m - Create Table : g2I2]* E1U2= 9i,@ h816 ?2BIk
o - ALTER TABLE : h81!', (D 9.1+7&'
r - DROP TABLE : g2I2]%', E1U2= T# h81!', tG"'
s - CREATE I NDEX : Ik B ~"%&' A27/# ?2
v - DROP INDEX : ~"%', A27/# tG"'


!#$%&' (*$+', -.$/', Arab Team 2000 o 1#23 1435 46$7',8 9:;'2* <2=
&46 >85 Select

q (D <1l7CW p,$l7e •S&4', jC3 h81!', 9i,@ T# g2I2]*
Syntax !#%"' (*,+ ."/*"/0 :
SELECT column_name(s)
FROM table_name

#2H y3K# : 946 SQL t8$"', '2"' e2C3 $]"
$$D 16S. > SELECT = select

('27', h81!', (&U h2z# :
The database table "Persons":
LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger

i5 1.$I xek, L2]7 8 h8k, $]ik, xek, (FirstName , LastName)
('27'2N &4!', j7cW :

SELECT LastName,FirstName FROM Persons

('27'2N {W2;', QSc.8 :
Result: "#$%& '
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari

'27'2N j7cW g2I2]%', R]46 L2]7ik ( :

SELECT * FROM Persons
('27'2N !]7;', QScW8 :
Result: '"#$%&
LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger
!#$%&' (*$+', -.$/', Arab Team 2000 r 1#23 1435 46$7',8 9:;'2* <2=
#2H y3K# : @S68 j&7W g2I2]%', 1U,S= EL,@q xyI `+* ( ; )
&46 .2XI (D SQL T# $zN5 G]/;W xU1W xy;', `+* Q> ,GH8
Z=S', %/I (D &46 .
('q p27"W > (7', xy;', T# ( ; ) Ms-Access & Ms-Sql server .
xy;', T# <, W (7', ('q p27" ( ; ) Oracle .



&4!' D2[q 8, '$i5 &4!' pL1;I Qk, SELECT
&46 (H8 SELECT DISTI NCT

4&N D2[q DISTI NCT Q81* x]:', )$+', T# n;c4W &4!&'
L,$cW ) [$+7* E$# T# $7N, L$c7# Q2]* ]D h81!', S' (;+.
%* E13,8 E$# ( .

h2z# : <,1l7e5 (&U DI STINCT 1U8 < 2X#,1l7e5

SELECT Company FROM Orders
"Orders" table
Company OrderNumber
Sega 3412
W3Schools 2312
Trio 4678
W3Schools 6798
Result: '"#$%&
Company
Sega
W3Schools
Trio
W3Schools
2;H 5 1;U $$/', b[,8 e 7 <,1l DISTINCT
SELECT DISTINCT Company FROM Orders
Result: %& '"#$
Company
Sega
W3Schools
Trio
!#$%&' (*$+', -.$/', Arab Team 2000 s 1#23 1435 46$7',8 9:;'2* <2=
_$B', <,1l7e, SH8 >, $i, ?+!' 9:7;I Q>,
EL2%U <,1l7e, WHERE

&46 1+* (W-W Select •S&4', _$B', (&U 'S7"W8
9cB', ,GX* 2X/2;* QSc.8 :
Syntax
SELECT column FROM table
WHERE column operator value

&46 R# Where ;;c4. ]Wk, gK#2+4', <,1l7e5 2 :








#2H y3K# : T# g,L,1\k, `+* (D SQL 9#2+4', <>
. ,GcH j7c !=

EL2%U <,1l7e5 (&U h2z# Where

SELECT * FROM Persons
WHERE City='Sandnes'
"Persons" table
LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980
Pettersen Kari Storgt 20 Stavanger 1960
Result '"#$%&
LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980

()*%& +,%& -./
'82C. =
'82C. > <>
T# $%N5 >
T# $^\5 <
'82C. 8, T# $%N5 >=
'82C. 8, T# $^\, <=
]* _$B', QSc. T]74]= T Between
X*2B7# g24&N TU ~"%&' LIKE
!#$%&' (*$+', -.$/', Arab Team 2000 v 1#23 1435 46$7',8 9:;'2* <2=
#2H y3K# : &4!', R# a]M;7', #KU <,1l7e5 (&U ( ' )
9z# ]M;', x]:', R# E@$/# a]M;W #KU <,1l7e5 j!.
@ T# %7cW ]4=$', g2I2]%&' %C;'2* 2#, ?24ek, g2#KU Q8 .
n'P (&U h2z# :
For text values: ]M;', g2I2]%&'
This is correct: !"!# $%&'
SELECT * FROM Persons WHERE FirstName='Tove'
This is wrong: ('*+ $%&'
SELECT * FROM Persons WHERE FirstName=Tove
For numeric values: 2I2]%&' g ]4=$',
This is correct: !"!# $%&'
SELECT * FROM Persons WHERE Year>1965
This is wrong: ('*+ $%&'
SELECT * FROM Persons WHERE Year>'1965'

<,1l7e, 9#2+4', Like _$B', (D
9N TU ~"%', h2z# ;]+# 4&c* ~"%', 1.1"W (D <1l7CW
;]+# 4&N 8, t$3 2X* @S6S4', ?24ek, .
Syntax 0/$1 /2 -35%& &721
SELECT column FROM table
WHERE column LIKE pattern

#KU <1l7CW ( % ) 1+* 8 9%= t8$"', @1U 1.1"7'
h2z# * ~"%I 'G', t$"', 8, 4&c', :
t$"* ,1%W (7', *2lfk, ?24e, $XyW tSe '2"', EGH (D A t8$"', T# @1U ', 2H1+* (W-. x0
SELECT * FROM Persons
WHERE FirstName LIKE 'a%'
$XyW tSe '2"', EGH (D (X7;W (7', *2lfk, ?24e, t$"* A t8$"', T# @1U ', 2X&%= QSc.8
SELECT * FROM Persons
WHERE FirstName LIKE '%a'
t$3 (&U 'S7"W (7', *2lfk, ?24e, $XyW tSe '2"', EGH (D A (D '5 xek, (D 1S[S#
SELECT * FROM Persons
WHERE FirstName LIKE '%la%'
2* *2l', ?+!', 2;' (:* Between 1+* 24]D ]'q pL1;I tSe8 .
!#$%&' (*$+', -.$/', Arab Team 2000 w 1#23 1435 46$7',8 9:;'2* <2=
g2I2]%', R# 9#2+7', SH8 >, 1.16 ?+!' 9:7;I Q>,
g2I2]%', R# gK#2+W 0K0 2;.1' 16S.
0 - D2[q : INSERT INTO m - 9.1+W : UPDATE
o - tG3 : DELETE

D2\q >8, h81!&' }\ : I NSERT INTO

('27'2N 2X/2;* QSc. :
Syntax
INSERT INTO table_name
VALUES (value1, value2,....)
QScW8 Y:D 2X7D2[q •S&4', hS:"', 1.1"W 23., n;c4.8
('27'2N :
INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)

D2[q h2z# ('27', h81!&' 1.16 }\ :
This "Persons" table:
LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger

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

('27'2N QScW !]7;', :

LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes

E14U5 (D g2I2]* D2[5 ) hS:3 ( ('27'2N j7cW Y:D E@1"# :

INSERT INTO Persons (LastName, Address)
VALUES ('Rasmussen', 'Storgt 67')
('27'2N !]7;', QScW :
LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes
Rasmussen Storgt 67

!#$%&' (*$+', -.$/', Arab Team 2000 7 1#23 1435 46$7',8 9:;'2* <2=
h81!', (D 9.1+7', 2]I20 : UPDATE

('27'2N &4!', ?2;* QSc. :
Syntax
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value

2z# ('2"', h81!', (&U 9.1+7', (&U h :
Person:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Storgt 67

(I2z', }M', (D h8k, xek, D2[5 :

UPDATE Person SET FirstName = 'Nina'
WHERE LastName = 'Rasmussen'
Result: '"#$%&
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Storgt 67

Q,S;+', $]^W8 ;.14', xe5 D2[5 :

Z=S', %/I (D @S4U T# $zN5 9.1+W

UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'
Result: '"#$%&
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Stien 12 Stavanger



!#$%&' (*$+', -.$/', Arab Team 2000 8 1#23 1435 46$7',8 9:;'2* <2=
h81!', T# tG"', 2z'20 : DELETE

('27'2N 2X/2;*8 h81!', T# tS/M', tG3 (D <1l7CW :
Syntax
DELETE FROM table_name
WHERE column_name = some_value


h81!', T# }\ tG3 (&U h2z# :
Person:
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger
Rasmussen Nina Stien 12 Stavanger

}M', tG"'

DELETE FROM Person WHERE LastName = 'Rasmussen'
Result "]7;', :
LastName FirstName Address City
Nilsen Fred Kirkegt 56 Stavanger

g2I2]%', R]46 tG"' ) tS/M', ( h81!', T#

]'27', &4!', <1l7CW :

DELETE FROM table_name
or
DELETE * FROM table_name



*2l',8 VL1', T# h8k, ?+!', ('2+W 9, 14"* xW
! SQL Data Manipulation Language (DML)


!#$%&' (*$+', -.$/', Arab Team 2000 0: 1#23 1435 46$7',8 9:;'2* <2=
GH (D 9#2+7', (D 51%I tSe A$B', T# &3$4', E
xW 24' g2D2[q -.$; TU g2I2]%', E1U2= R# $zN5 I8$4*
946 -.$; TU VL1', T# h8k, ?+!', (D 3$f SQL

EL2%U Order By :
@1"# 9:3 jC3 <K+7ek, T# {W2;', j]W$7' <1l7CW
j.$7', (&U h2z# :
Orders:
Company OrderNumber
Sega 3412
ABC Shop 5678
W3Schools 2312
W3Schools 6798

]'27', <K+7ek, &46 G]/;7* <S:;e :

SELECT Company, OrderNumber FROM Orders
ORDER BY Company
Result: '"#$%&
Company OrderNumber
ABC Shop 5678
Sega 3412
W3Schools 6798
W3Schools 2312

, Q, =3KI jC3 j]W$W (D g$X> g2I2]%' 3 9: Company

('27'2N QScW 9:3 T# $zN-* j]W$7', 2I@L, ,Pq :

SELECT Company, OrderNumber FROM Orders
ORDER BY Company, OrderNumber
Result: '"#$%&
Company OrderNumber
ABC Shop 5678
Sega 3412
W3Schools 2312
W3Schools 6798

!#$%&' (*$+', -.$/', Arab Team 2000 00 1#23 1435 46$7',8 9:;'2* <2=
P2# n'P x7. }]N j]W$7', %cU 2I@L, S' ,

h2z# :
SELECT Company, OrderNumber FROM Orders
ORDER BY Company DESC
Result: '"#$%&
Company OrderNumber
W3Schools 6798
W3Schools 2312
Sega 3412
ABC Shop 5678

n'P x7. }]N T]7:.$', <,1l7e5 1.$I 2;I, '23 (D

h2z# :
T]7:.$* n'P 94U Tc4.

('8k, :.$', :

SELECT Company, OrderNumber FROM Orders
ORDER BY Company DESC, OrderNumber ASC

2X%]W$W •S&4', hS:"', *27N (H ]I2z', :.$', 2#,
2]'?2;W 2X%]W$W •S&4', hS:"', x0 >8, 2.1U2MW

h2z# :
SELECT Company, OrderNumber FROM Orders
ORDER BY OrderNumber , Company DESC

E13,8 !]7;', QScW T]7:.$', T# ', (D8
Result: '"#$%&
Company OrderNumber
W3Schools 2312
W3Schools 6798
Sega 3412
ABC Shop 5678
E1.16 gK#2+4' 9:7;I Qk,
!#$%&' (*$+', -.$/', Arab Team 2000 0m 1#23 1435 46$7',8 9:;'2* <2=
T]&#2+4', Or & And
]&#2+4', T.GH <1l7C. $zN5 8, T];$f Y*L (D T

- 9#2+4', AND -:"W '23 (D {/27;', )$+* <S:.
_8$B', R]46
- 9#2+4', OR ', -:"W '23 (D {/27;', )$+* <S:.
_8$B', T# _$f

h2z# : ('27', h81!', (&U

LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes

9#2+4', <,1l7e5 AND :
SELECT * FROM Persons
WHERE FirstName='Tove'
AND LastName='Svendson'
Result: '"#$%&
LastName FirstName Address City
Svendson Tove Borgvn 23 Sandnes

9#2+4', <,1l7e5 OR :
SELECT * FROM Persons
WHERE firstname='Tove'
OR lastname='Svendson'
Result: '"#$%&
LastName FirstName Address City
Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes

2+# T]&#2+4', <,1l7e5 :
SELECT * FROM Persons WHERE
(FirstName='Tove' OR FirstName='Stephen')
AND LastName='Svendson'
Result: '"#$%&
LastName FirstName Address City
Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes

!#$%&' (*$+', -.$/', Arab Team 2000 0o 1#23 1435 46$7',8 9:;'2* <2=
9#2+4', I N

@,$4', 4]:', 1.1"W n;c4. I, 2X;# g2#,1l7e5 E1U '
135 (D 2H@S68 T# 1N-7# Z;N ,Pq 2X[$U hS:"',

&4!', ?2;* :
SELECT column_name FROM table_name
WHERE column_name IN (value1,value2,..)


h2z# : ('27', h81!', (&U

LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Nordmann Anna Neset 18 Sandnes
Pettersen Kari Storgt 20 Stavanger
Svendson Tove Borgvn 23 Sandnes

SELECT * FROM Persons
WHERE LastName IN ('Hansen','Pettersen')
Result: '"#$%&
LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Pettersen Kari Storgt 20 Stavanger



$zN5 Y*L (D #,1l7e5 23.5 Tc4.8 2+# <K+7e5 T#

SELECT * FROM table1_name
WHERE FirstName in (SELECT * FROM table2_name)

h8k, h81!', T# x]:', R]46 )$+* 2;4= h2z4', ,GH (D
Table1_name 9:"', QSc. Q, _$B* FirstName @S6S#
(I2z', h81!', (D table2_name



!#$%&' (*$+', -.$/', Arab Team 2000 0r 1#23 1435 46$7',8 9:;'2* <2=
9#2+4', AND ... BETWEEN
+' <1l7C. 85 *SMI T]74]= T]* g2I2]* US4!# )$
A.L2W 85 <2=L5

&4!', ?2;* :
SELECT column_name FROM table_name
WHERE column_name
BETWEEN value1 AND value2

h2z# : ('27', h81!', (&U

LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Nordmann Anna Neset 18 Sandnes
Pettersen Kari Storgt 20 Stavanger
Svendson Tove Borgvn 23 Sandnes

T]74]:', T]* E@S6S4', g2I2]%', )$U

SELECT * FROM Persons WHERE LastName
BETWEEN 'Hansen' AND 'Pettersen'
Result: '"#$%&
LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Nordmann Anna Neset 18 Sandnes

T]74]:', T]* E@S6S# $]^', g2I2]%', )$U

SELECT * FROM Persons WHERE LastName
NOT BETWEEN 'Hansen' AND 'Pettersen'
Result: '"#$%&
LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger
Svendson Tove Borgvn 23 Sandnes

E2U,$# R# A.L,S7',8 <2=Lk, R# :.$', %/I <1l7CW
(7#KU T]* QScW > A.L,S7', Q, ) ' ( <1l7C. 9* ) # ( .

!#$%&' (*$+', -.$/', Arab Team 2000 0s 1#23 1435 46$7',8 9:;'2* <2=
L2+7C4', xek, Alias

[$U 1;U 9:"', 8, h81!', xe, $]]^W (D <1l7C.
4&N -.$; TU #1l7CI AS

h81!', '23 (D ?2;%', :

SELECT column FROM table as Table Alias

9:"', '23 (D ?2;%', :

SELECT column AS column_alias FROM table

h2z# : ('27', h81!', (&U
This table (Persons):
LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger

SELECT LastName AS Family, FirstName AS Name
FROM Persons
Result: !]7;',
hS:"', ?24e5 =3KI ) E14Uk, (
Family Name
Hansen Ola
Svendson Tove
Pettersen Kari

SELECT LastName, FirstName
FROM Persons AS Employees
Result: !]7;',
Table Employees: h81!', xe, Q, =3KI
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
!#$%&' (*$+', -.$/', Arab Team 2000 0v 1#23 1435 46$7',8 9:;'2* <2=
Y*$', J OI N

R][,S# xH, T# 1S[S# SQL 'G', h8,1!', T]* Y*$', SH
'S7"W (7', h8,1!', T# g2I2]%', )$U (D $]zN 9XC.
xC:', 8 }>S4', 9z# =KU (&U .


8, T]'816 T# g2I2]* )$U ('q p27"I Q2]3k, `+* (D
=KU ?2BI, j!. &#2N !]7;', 9+!' $zN5 .

(e2ek, A27/4', -.$; TU x7. 8,1!', T]* Y*$',
) (Primary Key > (e2e, A27/# QSc. 'G', 9:"',
&i,1* g2I2]%', L,$cW Tc4. .

9:3 ('27', h81!', (D (Employee_ID) 9:3 SH A27/#
T]/>S4', h81!* *2l', (e2e, .

9:"', (I2z', h816 (D 2#, (Order_ID) A27/4', 9:3 SH
9:3 SH8 (6L2i A27/# 23., ]D8 h81!&' (e2e>,
(Employee_ID) .
Employees: 81!', h h8k,
Employee_ID Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
Orders: 98%& : ;=/>%&
Prod_ID Product Employee_ID
234 Printer 01
657 Table 03
865 Chair 03



!#$%&' (*$+', -.$/', Arab Team 2000 0w 1#23 1435 46$7',8 9:;'2* <2=
h2z# : {7;4', SH 2#8 {7;# j&; T# t$+I Q, 1.$I

SELECT Employees.Name, Orders.Product
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
Result: '"#$%&
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair

h2z# : {7;4', j&; T# t$+I Q, 1.$I (Printer)

SELECT Employees.Name
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
AND Orders.Product='Printer'
Result: '"#$%&
Name
Hansen, Ola

<,1l7e, (&U h2z# J OI N INNER :
&4!', ?2;* :
SELECT field1, field2, field3
FROM first_table
INNER JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield
h2z4', (&U &4!', G]/;W :
SELECT Employees.Name, Orders.Product
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
Result: '"#$%&
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair

&4!', <S:W INNER J OIN , R]46 )$+* g2I2]%'
T]'81!', T]* N$7B4',

!#$%&' (*$+', -.$/', Arab Team 2000 07 1#23 1435 46$7',8 9:;'2* <2=
<,1l7e5 (&U h2z# LEFT J OI N :

&4!', ?2;* :
SELECT field1, field2, field3
FROM first_table
LEFT JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield
h2z4', (&U &4!', G]/;W :
SELECT Employees.Name, Orders.Product
FROM Employees
LEFT JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
Result: '"#$%&
Name Product
Hansen, Ola Printer
Svendson, Tove
Svendson, Stephen Table
Svendson, Stephen Chair
Pettersen, Kari

&4!', <S:W LEFT J OIN 46 )$+* h81!', T# g2I2]%', R]
h8k, Employee (I2z', h81!', (D 16SW x' S' (73

<,1l7e5 (&U h2z# RIGHT J OIN :

&4!', ?2;* :
SELECT field1, field2, field3
FROM first_table
RIGHT JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfield
4!', G]/;W h2z4', (&U & :
SELECT Employees.Name, Orders.Product
FROM Employees
RIGHT JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
Result: !]7;',
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
&4!', <S:W RIGHT J OIN T# g2I2]%', R]46 )$+*
(I2z', h81!', Orders SW x' S' (73 h8k, h81!', (D 16
&46 <,1l7e5 (&U h2z# INNER J OIN :
!#$%&' (*$+', -.$/', Arab Team 2000 08 1#23 1435 46$7',8 9:;'2* <2=

{7;4', ,S%&; QG', T]/>S4', ?24e, )$U 1.$I (Printer)

SELECT Employees.Name
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
WHERE Orders.Product = 'Printer'

Result: '"#$%&
Name
Hansen, Ola

&46 A$f T# 2;]X7I, QScI ,GX*8 J OIN .




















!#$%&' (*$+', -.$/', Arab Team 2000 m: 1#23 1435 46$7',8 9:;'2* <2=
{#1', UNION ALL and UNION

j!. Tc'8 T]/&7l# T]'816 T# T]&:3 {#1' <1l7CW
SMI QSc. ', 13,8 T]&:"', (D g2I2]%', 1SI QSc. Q, 8, *
<2=L, ………. A'q

('27'2N QSc. &4!&' ?2;%', :
SQL Statement 1
UNION
SQL Statement 2

h2z# :
Employees_Norway:
E_ID E_Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
Employees_USA:
E_ID E_Name
01 Turner, Sally
02 Kent, Clark
03 Svendson, Stephen
04 Scott, Stephen

9:"', {#@ 1.$I E_NAME T]'81!', T# 9N (D :

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
g2I2]%&' L,$cW Q81* g$X> !]7;', Q, =3KI .
<,1l7e5 UNI ON ALL :
!#$%&' (*$+', -.$/', Arab Team 2000 m0 1#23 1435 46$7',8 9:;'2* <2=

<,1l7e5 9z# UNI ON R]46 )$+* <S:. I, $$/',
L,$cW 16S. S' (73 g2I2]%', .

('27'2N QSc. &4!&' ?2;%', :

SQL Statement 1
UNION ALL
SQL Statement 2

h2z# : 9:"', {#@ 1.$I E_NAME T]:*2C', T]'81!', (D

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


?24ek, 13, L,$cW R# g2I2]%', R]46 LSX> =3KI
T]'81!', (D C$7B4', .



) ^' (D ,16 xX# VL@ ('q 9:7;I Qk, SQL SH8 >,
h,81', Functions (





!#$%&' (*$+', -.$/', Arab Team 2000 mm 1#23 1435 46$7',8 9:;'2* <2=
SQL Functions h,81',

^' SQL ]*2C"',8 .@1+', h,81', T# $]zc', 2X*

?2;%', ',@ 'k (e2ek, :

SELECT function(column) FROM table


h,81', :

()*%& '%&8%&
@1"4', 9:"&' (*2C"', YeS', @2!.5 AVG(column)
tS/M', @1U D$+# ) gK!C', ( "L2/', gK!C', Q81* 9:"', (D COUNT(column)
h81!', (D tS/M', @1U D$+# COUNT(*)
9!e h8, 4]= D$+# 9:"', (D First(column)
9:"', (D 9!e $i5 4]= D$+# last(column)
9:"', (D 9!e 4]= $%N5 D$+# Max(column)
9:"', (D 9"e 4]= $^[5 D$+# Min(column)
9:"', (D x]:', ('246q D$+# SUM(column)
SQL SERVER (&U Y:D 94+W L,$cW Q81* 9:"', (D gK!C', @1U COUNT(DISTINCT column)

&z#k, T# US4!# :

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




!#$%&' (*$+', -.$/', Arab Team 2000 mo 1#23 1435 46$7',8 9:;'2* <2=
SQL GROUP BY and HAVING ?$/',8 R]4!7',

',1', 9z# .@1+', h,81', SUM ('q p27"W 2# ,$]zN
R]4!7', GROUP BY

&4!', ?2;* :
SELECT column,SUM(column) FROM table GROUP BY column

b][S7&' h2z# : ('27', h81!', (&U

Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
]'27', &4!', G/;I :
SELECT Company, SUM(Amount) FROM Sales
Returns this result: !]7;',
Company SUM(Amount)
W3Schools 17100
IBM 17100
W3Schools 17100

t$+I x'8 &N 1S4!4', gGi, gK!C', R]46 Q, =3KI
9!e 9N 1S4!# .

D2[q 1+* <K+7ek, &46 •$!I Qk, GROUP BY :

SELECT Company,SUM(Amount) FROM Sales
GROUP BY Company
Returns this result: !]7;',
Company SUM(Amount)
W3Schools 12600
IBM 4500

I, =3KI b[8, !]7;', Z"%\58 E138 9!e 9N R46 xW

!#$%&' (*$+', -.$/', Arab Team 2000 mr 1#23 1435 46$7',8 9:;'2* <2=
',1', HAVI NG T]+# _$f jC3 g2I2]%', ?$/' <1l7CW

&4!', ?2;* :

SELECT column,SUM(column) FROM table
GROUP BY column
HAVING SUM(column) condition value


-*2C', h2z4', %/I (&U -]%W :

Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100

]'27', <K+7ek, &46 G/;I :

SELECT Company,SUM(Amount) FROM Sales
GROUP BY Company
HAVING SUM(Amount)>10000
Returns this result : !]7;',
Company SUM(Amount)
W3Schools 12600

$%N, g2I2]%', )$U8 _$B', -:"W xW I, =3KI T# 0::::
Y:D .




*2l',8 VL1', T# (I2z', ?+!', ('2+W 9, 14"* xW
! SQL FUNCTI ON

!#$%&' (*$+', -.$/', Arab Team 2000 ms 1#23 1435 46$7',8 9:;'2* <2=
A$B* <S:I tSe VL1', T# ?+!', ,GH (D
Data Definition Language (DDL)

TU hS|C4', xC:', SH : g2I2]%', }.$+W

0 - Create Database : E1.16 g2I2]* E1U2= ?2BIk
m - Create Table : g2I2]* E1U2= 9i,@ h816 ?2BIk
o - ALTER TABLE : h81!', (D 9.1+7&'
r - DROP TABLE : g2I2]%', E1U2= T# h81!', tG"'
s - CREATE I NDEX : Ik B ~"%&' A27/# ?2 ) VL2X/', (
v - DROP I NDEX : ~"%', A27/# tG"'



g2I2]* E1U2= ?2BI, >8, Create Database :

N &4!', ?2;* ('27'2

CREATE DATABASE database_name

g2I2]%', E1U2= xe, @1"I 2+%;

', 2H,S6 D]/# (;+. ][2D QSc7H .@ g2I2]%', E1U2=
h8,16 .








!#$%&' (*$+', -.$/', Arab Team 2000 mv 1#23 1435 46$7',8 9:;'2* <2=
g2I2]%', E1U2= 9i,@ h816 ?2BIk Create Table :

('27'2N &4!', ?2;* QSc. :

CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
.......
)




h816 ?2BI5 (&U h2z# :

CREATE TABLE Person
(
LastName text(30),
FirstName text(30),
Address text(150),
Age (Number)
)


x!38 1,SI,8 hS:"', 1.1"W8 ,816 ?2BI, 2;;c4. Q>,
2X]D g2I2]%', .













!#$%&' (*$+', -.$/', Arab Team 2000 mw 1#23 1435 46$7',8 9:;'2* <2=
2BI, Qk, VL2X/', ? CREATE INDEX :

<K+7ek, ]&4U 9+!. (73 h81!', (D x4M. V$X/',
h81!', %/I V$XD T# $zN, ?2BI, Tc4. 24N 1$e5
]&4U 1$C7' (H 24I, VL2X/', EGH '$. > <1l7C4',
Y:D <K+7ek, .

c4. > h8k, 1S;', VL2X/', T# T]USI C2;H * L$c7. Q, T
S;', 2#, g2I2]%', 1 * g2I2]%', L,$cW Tc4. (I2z', .

1.$/', 1S;', T# V$X/&' ?2;%', ) L$c7. > 'G', ( Unique I ndex

CREATE UNIQUE INDEX index_name
ON table_name (column_name)

'@2+', 1S;', T# V$X/&' ?2;%', Simple Index

CREATE INDEX index_name
ON table_name (column_name)

z# h2 :

CREATE INDEX PersonIndex
ON Person (LastName)

(CcU j]W$7* V$XD ?2BI> :

CREATE INDEX PersonIndex
ON Person (LastName DESC)

h81!', %/I (D T]&:3 (D V$XD ?2BI> :

CREATE INDEX PersonIndex
ON Person (LastName, FirstName)



!#$%&' (*$+', -.$/', Arab Team 2000 m7 1#23 1435 46$7',8 9:;'2* <2=
$#k, Drop

Drop X746$W $#-N 2X#1l7CI 2;H 2;;c'8 ?2:'q ]D$"', 2
., tG3 Tc'8 tG3 ) g2I2]* E1U2= & V$XD & h816 (


g2I2]* 1U2= tG3 >8, : ('27'2N &4!', ?2;* QSc.

DROP DATABASE database_name

g2I2]%', E1U2= T# h816 tG3 2]I20 : &4!', ?2;* QSc.
('27'2N :

DROP TABLE table_name

V$XD tG3 2z'20 : E1U2= 1SI jC3 ?2;%', }&7l.8
g2I2]%',
h2z# :
Syntax for Microsoft SQLJet (and Microsoft Access): g2I2]%', E1U2= 1SI
DROP INDEX index_name ON table_name
Syntax for MS SQL Server: g2I2]%', E1U2= 1SI
DROP INDEX table_name.index_name
Syntax for IBM DB2 and Oracle: g2I2]%', E1U2= 1SI
DROP INDEX index_name
Syntax for MySQL: g2I2]%', E1U2= 1SI
ALTER TABLE table_name DROP INDEX index_name

tG3 Q8@ h81!', 9i,@ T# g2I2]%', tG3 $#, $]i5
h81!', :

TRUNCATE TABLE table_name
!#$%&' (*$+', -.$/', Arab Team 2000 m8 1#23 1435 46$7',8 9:;'2* <2=
$#k, ALTER TABLE

. tG38 D2[q T# h81!', (&U 9.1+7', (D <1l7C
E14U5 ) hS:3 . (

9:3 D2[5 : ('27'2N &4!', ?2;* QSc.

ALTER TABLE table_name
ADD column_name datatype

9:3 tG"' : ('27'2N &4!', ?2;* QSc. :

ALTER TABLE table_name
DROP COLUMN column_name

h2z# : h81!', (&U ('27',
Person:
LastName FirstName Address
Pettersen Kari Storgt 20

1.16 9:3 D2[5 :
To add a column named "City" in the "Person" table:
ALTER TABLE Person ADD City varchar(30)
Result: '"#$%&
LastName FirstName Address City
Pettersen Kari Storgt 20
h81!', T# 9:3 tG"' :
To drop the "Address" column in the "Person" table:
ALTER TABLE Person DROP COLUMN Address
Result: '"#$%&
LastName FirstName City
Pettersen Kari

!#$%&' (*$+', -.$/', Arab Team 2000 o: 1#23 1435 46$7',8 9:;'2* <2=
&46 SELECT INTO

h81!', T# ];2]735 lCI =/3 (D E@2U <1l7CW

7'2N 94!', ?2;* ('2 :

SELECT column_name(s) INTO newtable [IN externaldatabase]
FROM source



E1U2:', %/I (D h81!', T# ];2]73q lCI 94+' :

SELECT * INTO Persons_backup
FROM Persons

'$i5 g2I2]* E1U2:' h81!', AC;' : <1l7CI IN

SELECT Persons.* INTO Persons IN 'Backup.mdb'
FROM Persons

Y:D E@1"# hS:3 ACI 23.5 Tc4. :

SELECT LastName,FirstName INTO Persons_backup
FROM Persons

_$f h2i@q R# Tc'8 -*2C', h2z4', %/I : Where

SELECT LastName,Firstname INTO Persons_backup
FROM Persons
WHERE City='Sandnes'

CI 2I@L, ,Pq $i5 h816 R# $KU @S68 (D Tc' h81!', A :

SELECT Employees.Name,Orders.Product
INTO Empl_Ord_backup
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID

!#$%&' (*$+', -.$/', Arab Team 2000 o0 1#23 1435 46$7',8 9:;'2* <2=
&46 CREATE VIEW

T]+# _$f Z"W g2I2]* )$+W &46 ?2BI> <1l7CW
', <,1l7e5 Tc4. 946 T# SQL =KU 94U 9z# 2X*
h,81', T# ',@ D2[q 8, .

('27'2N <2+', 2X/2;* QSc. :

CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

#2H g2y3K# :
0 - h,8@ 8, _8$f T# EG]/;W x7. 24* $0-7W > g2I2]%', E1U2=
m - <S:. 24I,8 h816 (D {W2;', =/"* <S:W > g2I2]%', E1U2=
<S:W E$# 9N &4!', G]/;7* g2I2]%', E1U2= C$"#
2X]D &]^B7*


g2I2]%', E1U2= (&U ];%4', &z#k, `+* Northwind :

h2z# 0 :
CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No

&]^BW x7. }]N : ]Wk, &4!', -.$; TU

SELECT * FROM [Current Product List]





!#$%&' (*$+', -.$/', Arab Team 2000 om 1#23 1435 46$7',8 9:;'2* <2=
h2z# m :
CREATE VIEW [Products Above Average Price] AS
SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)

&4!'2* [$U x7. :

SELECT * FROM [Products Above Average Price]


h2z# o :
CREATE VIEW [Category Sales For 1997] AS
SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales
FROM [Product Sales for 1997]
GROUP BY CategoryName
7. &4!'2* [$U x :
SELECT * FROM [Category Sales For 1997]
g2I2]%', )$U ?2;0, _$B', <,1l7e5 Tc4.8 :

SELECT * FROM [Category Sales For 1997]
WHERE CategoryName='Beverages'




V8L1', T# ?2X7Ik, 9, 14"* xW
$]l', ]D 24' R]4!', nDS. Q, 9, ,SU1I8
j/2^', $Xy* ?2U1', xc&|e,
1#23 1435 R]4!&' (W2]"W
COM . HOTMAIL @ I T - GENIUS
(D S3U
!#$%&' (*$+', -.$/',
M CO . 2000 ARABTEAM . WWW
xe-* GENIUS-I T

You're Reading a Free Preview

تحميل
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->