Sei sulla pagina 1di 45

٢٠١٠

(SQL)

. Mohamed_SuezMohamed_SuezMohamed_SuezMohamed_Suez &&&& HISHISHISHIS WifeWifeWifeWife
.
Mohamed_SuezMohamed_SuezMohamed_SuezMohamed_Suez
&&&& HISHISHISHIS WifeWifeWifeWife
ARABOUGARABOUG.orgARABOUGARABOUG.org.org.orgىلإ ءادھإ
3/3/2010

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> desc dept Name Null? Type

SQL> desc dept

Name

Null?

Type

-------------------------------------- --- -------- ----------------------------

DEPTNO

NOT NULL NUMBER(2)

DNAME

VARCHAR2(14)

LOC

VARCHAR2(13)

--------------------------------------- ------------------------------------------------ ----

SQL> select *

2 from dept;

DEPTNO DNAME

LOC

---------- -------------- -------------

10

ACCOUNTING

NEW YORK

20

RESEARCH

DALLAS

30

SALES

CHICAGO

40

OPERATIONS

BOSTON

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select empno,ename,job,hir edate 2 from emp; EMPNO

SQL> select empno,ename,job,hir edate

2 from emp;

EMPNO ENAME

JOB

HIREDAT

E

---------- ---------- --------- ------- -

7369

SMITH

CLERK

17/12/8

0

7499

ALLEN

SALESMAN 20/02/8 1

7521

WARD

SALESMAN 22/02/8 1

7566

JONES

MANAGER

02/04/8

1

7654

MARTIN

SALESMAN 28/09/8 1

7698

BLAKE

MANAGER

01/05/8

1

7782

CLARK

MANAGER

09/06/8

1

7788

SCOTT

ANALYST

19/04/8

7

7839

KING

PRESIDENT 17/11/8 1

7844

TURNER

SALESMAN 08/09/8 1

7876

ADAMS

CLERK

23/05/8

7

EMPNO ENAME

JOB

HIREDAT

E

---------- ---------- --------- ------- -

7900

JAMES

CLERK

03/12/8

1

7902

FORD

ANALYST

03/12/8

1

7934

MILLER

CLERK

23/01/8

2

14 rows selected.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ SQL> select distinct job 2* from
ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ
SQL> select distinct job
2* from emp;
JOB
---------
CLERK
SALESMAN
PRESIDENT
MANAGER
ANALYST

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select empno employee_no , ename employee_name, job

SQL> select empno employee_no , ename employee_name, job jobs

2* from emp

EMPLOYEE_NO EMPLOYEE_N JOBS

----------- ---------- ---------

7369

SMITH

CLERK

7499

ALLEN

SALESMAN

7521

WARD

SALESMAN

7566

JONES

MANAGER

7654

MARTIN

SALESMAN

7698

BLAKE

MANAGER

7782

CLARK

MANAGER

7788

SCOTT

ANALYST

7839

KING

PRESIDENT

7844

TURNER

SALESMAN

7876

ADAMS

CLERK

EMPLOYEE_NO EMPLOYEE_N JOBS

----------- ---------- ---------

7900

JAMES

CLERK

7902

FORD

ANALYST

7934

MILLER

CLERK

14 rows selected.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ ث لاثلا لصفلا ةلئسأ

ث لاثلا لصفلا ةلئسأ SQL> select ename,sal 2 from emp 3 where sal>2850 4

SQL> select ename,sal

2 from emp

3 where sal>2850

4 order by sal desc;

ENAME

SAL

---------- ----------

KING

5000

FORD

3000

SCOTT

3000

JONES

2975

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select ename,sal ٢ from emp ٣ where sal not

SQL> select ename,sal

٢ from emp

٣ where sal not between 1500 an d 2850؛

ENAME

SAL

---------- ----------

SMITH

800

WARD

1250

JONES

2975

MARTIN

1250

SCOTT

3000

KING

5000

ADAMS

1100

JAMES

950

FORD

3000

MILLER

1300

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select ename,sal 2 from emp 3 where(deptno=10 or

SQL> select ename,sal

2 from emp

3 where(deptno=10 or deptno=3 0) and sal>1500

4 order by sal desc;

ENAME

SAL

---------- ----------

KING

5000

BLAKE

2850

CLARK

2450

ALLEN

1600

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select ename,hiredate 2 from emp 3 where hiredate

SQL> select ename,hiredate

2 from emp

3 where hiredate like'%82';

ENAME

HIREDATE

---------- --------

MILLER

23/01/82

=================================

SQL> select hiredate

from emp

where ename = 'SCOTT';

HIREDATE

---------

19-APR-87

where ename = 'SCOTT'; HIREDATE --------- 19-APR-87 ن كلذو لاؤسلاب امك ةجيتنلا رھظت

ن كلذو لاؤسلاب امك ةجيتنلا رھظت ةظوحلم

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select ename,sal,comm 2 from emp 3 where comm is

SQL> select ename,sal,comm

2 from emp

3 where comm is not null;

ENAME

SAL

COMM

---------- ---------- ----------

ALLEN

1600

300

WARD

1250

500

MARTIN

1250

1400

TURNER

1500

0

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ SQL> select ename 2 from emp 3
ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ
SQL> select ename
2 from emp
3 where ename like'
A%';
ENAME
----------
BLAKE
CLARK
ADAMS

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select ename 2 from emp 3 where ename

SQL> select ename

2 from emp

3 where ename like'%LL%';

ENAME

----------

ALLEN

MILLER

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

عبارلا لصفلا ةلئسأ

عبارلا لصفلا ةلئسأ 1 SQL> select initcap(ename) E MP_NAME,length(ename)

1 SQL> select initcap(ename) E MP_NAME,length(ename) NUMBER1,substr(ename,0,4) SE CTION

2* from emp

EMP_NAME

NUMBER1 SECT

---------- ---------- ----

Smith

5 SMIT

Allen

5 ALLE

Ward

4 WARD

Jones

5 JONE

Martin

6 MART

Blake

5 BLAK

Clark

5 CLAR

Scott

5 SCOT

King

4 KING

Turner

6 TURN

Adams

5 ADAM

EMP_NAME

NUMBER1 SECT

---------- ---------- ----

James

5 JAME

Ford

4 FORD

Miller

6 MILL

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

1 SQL> select ename || job TIT LE 2 from emp 3* where

1 SQL> select ename || job TIT LE

2 from emp

3* where deptno=30

TITLE

-------------------

ALLENSALESMAN

WARDSALESMAN

MARTINSALESMAN

BLAKEMANAGER

TURNERSALESMAN

JAMESCLERK

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

1 SQL> select ename ENAME,rou

1 SQL> select ename ENAME,rou nd((months_between(sysdate,hiredate)/ 12),0)

2* from emp

ENAME

ROUND((MONTHS_BETWEEN(SYSDAT

E,HIREDATE)/12),0)

---------- ---------------------------- ------------------

SMITH

29

ALLEN

29

WARD

29

JONES

29

MARTIN

28

BLAKE

29

CLARK

29

SCOTT

23

KING

28

TURNER

28

ADAMS

23

ENAME

ROUND((MONTHS_BETWEEN(SYSDAT

E,HIREDATE)/12),0)

---------- ---------------------------- ------------------

JAMES

28

FORD

28

MILLER

28

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL > select TO_CHAR(SYSDA TE,'DAY "OF" MM

SQL >

select TO_CHAR(SYSDA TE,'DAY "OF" MM YYYY')

2* from dual

TO_CHAR(SYSDATE,'DAY

--------------------

FRIDAY

OF 03 2010

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> Select to_date( ' FEBRUAR Y 22,

SQL> Select to_date( ' FEBRUAR Y 22, 2004','mm / dd/yyyy')

2 From dual;

TO_DATE

---------

22-FEB-04

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

سماخلا لصفلا ةلئسأ

سماخلا لصفلا ةلئسأ SQL> select max(sal) MAXIMUM, min(sal) NINIMUM,sum(sal)

SQL> select max(sal) MAXIMUM, min(sal) NINIMUM,sum(sal) SUM,round (avg(sal)) AVERAGE

from emp ;

MAXIMUM

NINIMUM

SUM

AVERAG

E

---------- ---------- ---------- ----------

 

5000

800

29025

2073

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select job,max(sal) as MAXI MUM,min(sal) as MINIMUM

SQL> select job,max(sal) as MAXI MUM,min(sal) as MINIMUM

from emp

group by job ;

JOB

MAXIMUM

MINIMUM

---------- ---------- ---------

CLERK

1300

800

SALESMAN

1600

1250

PRESIDENT

5000

5000

MANAGER

2975

2450

ANALYST

3000

3000

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select job,count(job) from emp group by job; JOB

SQL> select job,count(job)

from emp

group by job;

JOB

COUNT(JOB)

---------- ---------

CLERK

4

SALESMAN

4

PRESIDENT

1

MANAGER

3

ANALYST

2

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select count(distinct mgr ) ‘ NUMBER1 OF

SQL> select count(distinct mgr) ‘ NUMBER1 OF MANAGER’

from emp ;

NUMBER1 OF MANAGER

------------------

6

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select e.ename,e.deptno,d. dname 2 from emp e,dept

SQL>

select e.ename,e.deptno,d. dname

2 from emp e,dept d

3 where e.deptno=d.deptno

4* and e.deptno=30;

ENAME

DEPTNO DNAME

---------- ---------- --------------

ALLEN

30 SALES

WARD

30 SALES

MARTIN

30 SALES

BLAKE

30 SALES

TURNER

30 SALES

JAMES

30 SALES

6 rows selected.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select distinct( e.job),d.loc 2 from emp e,dept d 3

SQL>

select distinct( e.job),d.loc

2 from emp e,dept d

3 where e.deptno=d.deptno

4* and e.deptno=30;

JOB

LOC

--------- -------------

SALESMAN CHICAGO

MANAGER

CHICAGO

CLERK

CHICAGO

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select e.ename,e.job,d.dn ame 2 from emp e,dept d 3

SQL>

select e.ename,e.job,d.dn ame

2 from emp e,dept d

3 where e.deptno=d.deptno

4* and d.loc like 'DALLAS'

ENAME

JOB

DNAME

---------- --------- --------------

SMITH

CLERK

RESEARCH

JONES

MANAGER

RESEARCH

SCOTT

ANALYST

RESEARCH

ADAMS

CLERK

RESEARCH

FORD

ANALYST

RESEARCH

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select e.ename,e.job,d.dnam e,e.sal,s.grade 2 from

SQL> select e.ename,e.job,d.dnam e,e.sal,s.grade

2 from emp e,dept d,salgrade s

3 where (e.deptno=d.deptno

4 and e.sal between s.losal and s.hisal)

5 and e.deptno=10

6* order by sal;

ENAME

JOB

DNAME

SAL

GRADE

---------- --------- -------------- --- ------- ----------

MILLER

CLERK

ACCOUNTING

1300

2

CLARK

MANAGER

ACCOUNTING

2450

4

KING

PRESIDENT

ACCOUNTING

5000

5

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select e.empno,e.ename,m.e mpno,m.ename 2 from emp

SQL> select e.empno,e.ename,m.e mpno,m.ename

2 from emp e,emp m

3 where e.mgr=m.empno

4 and e.deptno=10;

EMPNO ENAME

EMPNO ENAME

---------- ---------- ---------- ------ ----

7782

CLARK

7839 KING

7934

MILLER

7782 CLARK

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select ename,hiredate 2 from emp 3 where

SQL> select ename,hiredate

2 from emp

3 where deptno=(select deptno

4 from emp

5 where ename like 'BLAKE');

ENAME

HIREDATE

---------- --------

ALLEN

20/02/81

WARD

22/02/81

MARTIN

28/09/81

BLAKE

01/05/81

TURNER

08/09/81

JAMES

03/12/81

6 rows selected.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ SQL> select empno,ename 2 from emp
ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ
SQL>
select empno,ename
2 from emp
3 where sal> all(select avg(sal)
4 from emp)
5* order by sal desc;
EMPNO ENAME
---------- ----------
7839
KING
7902
FORD
7788
SCOTT
7566
JONES
7698
BLAKE
7782
CLARK
6 rows selected.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select ename,deptno,job 2 from emp 3

SQL>

select ename,deptno,job

2

from emp

3

where deptno=(select deptno

4

from dept

5*

where loc like 'DALL AS');

ENAME

DEPTNO JOB

---------- ---------- ---------

SMITH

20 CLERK

JONES

20 MANAGER

SCOTT

20 ANALYST

ADAMS

20 CLERK

FORD

20 ANALYST

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select empno,ename,job,sal 2 from emp 3 where

SQL> select empno,ename,job,sal

2 from emp

3 where sal< all(select sal

4 from emp

5 where job like 'SALE SMAN')

6 and job<>'SALESMAN';

EMPNO ENAME

JOB

SAL

---------- ---------- --------- ------- ---

7369

SMITH

CLERK

800

7876

ADAMS

CLERK

1

100

7900

JAMES

CLERK

950

SQL> select sal

2 from emp

3 where job like 'SALESMAN';

SAL

----------

1600

1250

1250

1500

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select ename,deptno,sal 2 from emp 3

SQL> select ename,deptno,sal

2

from emp

3

where (sal,deptno) in (select sal,deptno

4

from emp

5

where comm is n ot null)

6

order by sal;

ENAME

DEPTNO

SAL

---------- ---------- ----------

MARTIN

30

1250

WARD

30

1250

TURNER

30

1500

ALLEN

30

1600

SQL> spool off

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select distinct e.ename,dal. dname ,e.sal 2 from

SQL> select distinct e.ename,dal. dname ,e.sal

2 from emp e,(select m.sal,m.co mm,d.dname

3 from emp m,dept d

4 where m.deptno=d.dep tno

5 and d.loc = 'DALLAS')d al

6 where e.sal=dal.sal

7 and nvl(e.comm,0) = nvl(dal.c omm,0)

8 order by sal;

ENAME

DNAME

SAL

---------- -------------- ----------

SMITH

RESEARCH

800

ADAMS

RESEARCH

1100

JONES

RESEARCH

2975

FORD

RESEARCH

3000

SCOTT

RESEARCH

3000

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> select ename,hiredate,sal 2 from emp 3 where

SQL> select ename,hiredate,sal

2 from emp

3 where (sal,nvl(comm,0)) in (se lect sal,nvl(comm,0)

4 from emp

5 where ename = 'SCOTT')

6 and ename<>'SCOTT';

ENAME

HIREDATE

SAL

---------- -------- ----------

FORD

03/12/81

SQL> spool off

3000

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL>insert into emp

SQL>insert into emp

values(1111,'AHMED','ENGENEE R',7839,'01-01-2004',5000,200,10);

1 row created.

======================================= =====

commit

--

SQL >update emp

set sal=7000

where ename='AHMED';

1 row updated.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> update emp 2 set sal = 1000 3 where sal<900; 1

SQL> update emp

2 set sal = 1000

3 where sal<900;

1 row updated.

SQL>commit

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> delete from emp 2 where empno=1111; 1 row deleted.

SQL> delete from emp

2 where empno=1111;

1 row deleted.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> insert into emp 2 values

SQL> insert into emp

2 values (&empno,&ename,&job ,&mgr,&hiredate,&sal,&comm,&deptno) ;

Enter value for empno: 111

Enter value for ename: 'Moh_Sue z'

Enter value for job: 'DBA'

Enter value for mgr: 0

Enter value for hiredate: '01-01-2 010'

Enter value for sal: 12000

Enter value for comm: 1200

Enter value for deptno: 10

old

2:

values (&empno,&enam e,&job,&mgr,&hiredate,&sal,&comm,&d eptno)

new

2:

values (111,'Moh_Suez' ,'DBA',0,'01-01-2010',12000,1200,10)

1 row created.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> create table department (Id number(7),Name

SQL> create table department

(Id number(7),Name varchar2(25 ));

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> create table EMPLOYEE 2 (ID number(7),LAST_NAM E

SQL> create table EMPLOYEE

2 (ID number(7),LAST_NAM E varchar2(25),FIRST_NAME varchar2(25),DEPT_ID

number(7));

Table created.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> alter table employee 2 modify ( 3 Table altered.

SQL> alter table employee

2 modify ( 3

Table altered.

LAST_NAME var char2(50));

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> create table employee SQL> create table

SQL> create table employee

SQL> create table employee as

2 (ID,LAST_NAME,DEPT_ID)

3 as

4

select empno ID,ename

4 select empno,ename,deptno

LAST_NAME,deptno DEPT_ID

from emp; Table created.******

5

5

from emp;

 

Table created.******

SQL> select*

 

SQL> select *

 

2

from employee;

2

from employee;

ID LAST_NAME

DEPT_ID

ID LAST_NAME

DEPT_ID

---------- ---------- ----------

---------- ---------- ----------

 

7369

SMITH

20

7369

SMITH

20

7499

ALLEN

30

7499

ALLEN

30

7521

WARD

30

7521

WARD

30

7566

JONES

20

7566

JONES

20

7654

MARTIN

30

7654

MARTIN

30

7698

BLAKE

30

7698

BLAKE

30

7782

CLARK

10

7782

CLARK

10

7788

SCOTT

20

7788

SCOTT

20

7839

KING

10

7839

KING

10

7844

TURNER

30

7844

TURNER

30

7876

ADAMS

20

7876

ADAMS

20

ID LAST_NAME

DEPT_ID

ID LAST_NAME

DEPT_ID

---------- ---------- ----------

---------- ---------- ----------

 

7900

JAMES

30

7900

JAMES

30

7902

FORD

20

7902

FORD

20

7934

MILLER

10

7934

MILLER

10

14 rows selected.

 

14 rows selected.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> alter table employee2 2 add (FIRST_NAME varchar2(

SQL> alter table employee2

2 add (FIRST_NAME varchar2( 25));

Table altered.

employee2 2 add (FIRST_NAME varchar2( 25)); Table altered. SQL> drop table employee; Table dropped. SQL> Rename

SQL> drop table employee;

Table dropped.

Table altered. SQL> drop table employee; Table dropped. SQL> Rename employee2 to emp loyee; Table renamed.

SQL> Rename employee2 to emp loyee;

Table renamed.

SQL> Rename employee2 to emp loyee; Table renamed. SQL> alter table employee drop (last_name); Table altered

SQL> alter table employee

drop (last_name);

Table altered

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

ريخ او رشع ىداحلا لصفلا ةلئسأ

ريخ او رشع ىداحلا لصفلا ةلئسأ SQL> create table DEPARTMEN T ( deptno number(3) primary

SQL> create table DEPARTMEN T ( deptno number(3) primary key,dnam e varchar2(10),loc varchar2(15));

Table created.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> varchar2(10), create table EMPLOYEE( m p n o

SQL>

varchar2(10),

create table EMPLOYEE(

mpno number(3) ,ename varchar2(10),d name

2 constraint emp_pk primary k ey(empno),

3* constraint emp_uk unique(dn ame) ) ;

Table created.

ﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣﻞـــــــﺣ

SQL> alter table employee add constraint deptno_fk

SQL> alter table employee

add constraint deptno_fk forign key(deptno) references dept(deptno);

Table altered.

forign key(deptno) references dept(deptno); Table altered. SQL> alter table employee add constraint sal_ck

SQL> alter table employee

add constraint sal_ck check(sal b etween 1000 and 5000);

Table altered.

sal_ck check(sal b etween 1000 and 5000); Table altered. SQL> select constraint_name,col umn_name from

SQL> select constraint_name,col umn_name

from user_cons_column

where table_name='EMPLOYEE';

XXXXXXXXXXXXXXXXXX

where table_name='EMPLOYEE'; XXXXXXXXXXXXXXXXXX SQL> select constraint-name,con straint_type from

SQL> select constraint-name,con straint_type

from user_constraint

where table_name='EMPLOYEE';

CONSTRAINT_NAME

C

------------------------------ -

PK_EMP

P

FK DEPTNO

R