Oracle (SQL), Sulieman Khudruj
-
Upload
sulieman-khudruj -
Category
Education
-
view
280 -
download
4
description
Transcript of Oracle (SQL), Sulieman Khudruj
Database : a collection of organized data , or set
of tables with relations.
Why oracle ??
High capacity.
Performance.
Security
Web ready
Oracle :
1) Databse
a. SQL : Structured Query Langauge
b. PL/SQL.
2) Developer
a. Forms
b. Reports
Other Tools:
Designer
Portals
Jdevelopers
Discoverer
EM
Grid controls
Entity Relation Ship Diagram
(ERD)
Symbol Meaning
Entity type
Weak Entity type
Relationship type
Identifying
Relationship type
attribute
Key attribute
Multi valued attribute
Composite attribute
Derived attribue
Simple Example
Database contains 2 tables
Table 1: Emplyee
Empno Ename Sal Birthdate Mobile# Age 1 Ali 5000 14/2/1977 05467756
Table 1: Dept
Deptno Deptname
10 Computer 20 Research 30 Sales 40 Accounting
Empn
o
Enam
e
Employee
s
Sal
Mobile
#
Birthdat
e
Age
Dept
Dento
n Deptname
Wor
k
1
M
SQL Commands Types
DDL : Data Definition language
Create, Drop, Alter, Rename,….
DML: Data Manipulation language
Insert, Delete, Update, select
DCL : Data Control Language
Grant, Revoke, …….
Transactions : Commit, Rollback, Save
point,…
Constraints
Not null : we must enter a value but duplication
is allowed.
Unique : duplication is not allowed but null
values is acceptance.
Check : Any values but must have a condition.
Primary key : Not null and Unique
Foreign key : it is a primary key or unique key
from a nother table.
Data Types
Number (5) , can be upto 5
Number (6,2)
Number , it can reserve to the highest value
but it is not recommended
Char (10) , Ahamed- - - -
Varchar2(20)
Date : Store Date in many formats
7. insert
Dept table
Deptno Depname
10 Computer
20 Resaech
30 Accounting
40 Marketing
Employee
Empno Ename Sal Birthdate Mobile#
1 abdulelah 5000 14/2/1977 054545454 2 omar 6000 14/4/1993 054564645 3 khaled 8000 1/4/1990 054564564 4 mubark 5500 1/1/1990 054645646 5 saad 9000 1/1/1994 056456445 6 adel 29000 13/11/1993 045645644 7 Abd allah 30000 14/5/1996 056456564 8 amer 16000 5/7/1992 045564454 9 Abd aziz 55000 7/4/1979 045645645 10 hamad 50000 4/5/1993 045645666
10. Browsing data (select)
- Firstly create the following table (empdata)
- Then enter the data
Empno Ename Sal Birthdate Mobile# nationality
1 Abdullah 5000 14/2/1977 054545454 Saudi 2 omar 6000 14/4/1993 054564645 Saudi 3 khaled 8000 1/4/1990 054564564 Saudi 4 mubark 5500 1/1/1990 054645646 Saudi 5 saad 9000 1/1/1994 056456445 Saudi 6 saad 29000 13/11/1993 045645644 Jordanian 7 Abd allah 30000 14/5/1996 056456564 Jordanian 8 Amer 16000 5/7/1992 045564454 Egyptian 9 Amer 55000 7/4/1979 045645645 Egyptian 10 hamad 60000 4/5/1993 045645666 Suadi
c) select ename, sal*12 as ann_sal from empdata
d) select sysdate from dual;
e) select 545*87 from dual;
g) select * from empdata
where empno=10;
Note
Type Symbol meaninig
Logical operators
= equal
< > , != , ^ = Not equal
> Less than
< Greater than
>= Less than or equal
<= Greater than or equal
Logical Bitwise and , or , not Contacts between
conditions
h) select * from empdata
where sal >5000 and nationality='Saudi';
i) select ename, sal , mobile
from empdata
where sal between 10000 and 60000
j) select ename , sal , nationality
from empdata
where ename like '%m%'
k) select * from empdata
where ename like 'm%';
L) select * from empdata
where ename like '%h';
m) select ename, sal
from empdata
where ename like '_b%';
p) select * from empdata
where sal in (6000,5000,55000,60000)
q) select * from empdata
where ename in ('omar','Amer')
Revision (1) A)
1)Create table “Student_info” as follows :
Filed Name Type
Student_no Number(4) , primary key
Student_name Varchar2(20)
Student_mobile Number (10)
Student_age Number(4)
1) Insert 2 rows to this table
Student_no Student_name Student_mobile Student_age
100 Ali 054465477 29
101 Omar 059089035 32
3)Display the fields with properties
B)
1) list the steps to create new (RCC with password
KSU) user and
a) log in to user System / Manager
b) write create user RCC identified by KSU
2) give RCC user all the privileges
grant create session to RCC
grant connect, resource to RCC
c)
study to the following empdata and answer the quesions
a) modify length of filed nationality to 50
alter table empdata
modify nationality varchar2(50)
b) rename the name of table to empinfo
rename empdata to empinfo;
c) delete the column birthdate
alter table empinfo
drop column birthdate;
d) drop the table to recyclein
drop table empinfo;
e) show the contents of the recyclein
show recyclein ;
d)
study the following table and answer the quesions
a) calculate the annual salary for each employee with
column alias "emp_ann_sal”, and display the name
and the annual salary
select ename , sal*12 as emp_ann_sal from empinfo;
b) display the records of empno = 6 and empno=9
select * from empinfo
where empno in (6,9);
c) display all records of employee who earn salary
between 8000 to 16000
select * from empinfo
where sal between 8000 and 16000
d) display all records , that names contain d letter
select * from empinfo
where ename like ‘%d%’;
e)display all records , that names contain 8 letters
select * from empinfo
where ename like ‘________’;
f) return the system date
select sysdate from dual;
g) return the value of 18*35
select 18*35 from dual;
Functions
1) Single
2) Group
2) Single
a. Character
b. Number
c. Date
d. General
e. Conversion
I. Character
a. Upper
b. Lower
c. Initcap
d. Substr
e. Instr
f. Replace
g. Translate
h. Lpad ,rpad ,ltrim ,rtrim
i. length
a)
select ename, upper(ename), lower(ename), initcap(ename)
from empdata
b)
select * from empdata
where upper(ename) like '%S%';
e) select ename, instr(ename,'a',1,2) from empdata;
f) select ename ,replace(ename,'Abdullah','Abdulelah')
from empdata
g) select ename ,translate(ename,'ah','ox')
from empdata
h) select ename, lpad(sal,10,'*') , rpad(sal,10,'*') from empdata;
II) Number Function
a. Mod
b. Round
c. Trunk
a. select mod (1.5,1), mod (5,2) , mod (5,1) from dual
b. select round (137.74), trunc(137.74) from dual
III) Date Functions and Conversation functions
‘dd-mon-yy’ default , example : 15-jun-02
Rules :
a. date1-date2 = number of days
b. date1(+/-) number of days = date2
c. date1 (+/-) hours/24=date2
d. date+date = not allowed
a. select months_between ('02-12-2013', '04-6-2013') from
dual
b. select add_months ( '2-7-2013',2) from dual
c. select next_day ('2-7-2013', 1) from dual
note : 1(Saturday) , 2( Sunday),………..
d. select last_day ('2-7-2013') from dual
IV) General Functions
a.
insert into empdata (empno,ename,birthdate,mobile,nationality)
values (11,'Ali','4/6/1990',054546454,'Egyption');
select ename, nvl(sal,0) as " annual salary" from empdata
b. select ename, nullif (sal,60000) from empdata
c. select to_char(sysdate, 'HH:MI:SS AM ') from dual;
IIV) Group functions
a. select count (sal), sum (sal), avg(sal) from empdata
b. select min(sal), max(sal) from empdata
b. suppose this table
no name salary deptno job
1 Ali 5000 10 programmer
2 Khaled 6000 10 analyst
3 Omar 40000 10 designer
4 Sami 55000 20 manager
5 Amer 70000 20 manager
6 Nasser 5440 20 accouter
7 Rami 45400 20 programmer
8 Mubark 20000 30 advisor
9 anas 15000 30 programmer
10 mostafa 22000 30 analyst
c. select deptno ,sum(salary) from empcomp
group by deptno;
d. select deptno,job, sum(salary) from empcomp
group by deptno,job;
d. select job ,sum(salary) from empcomp
group by job;
e. select deptno,job,sum(salary) from empcomp
where deptno <> 30
having sum(salary)>10000
group by deptno, job;
delete rows , update data
a. delete from empdata
where empno=11;
d.
update empdata
set nationality ='Saudi'
where empno =10;
Constraints
Not null : we must enter a value but duplication
is allowed.
Unique : duplication is not allowed but null
values is acceptance.
Check : Any values but must have a condition.
Primary key : Not null and Unique
Foreign key : it is a primary key or unique key
from a nother table.
Example:
Table 1 : patient
Pat_no(pk) Pat_name Pat_db Pat_gender
10 Ali 4/4/1992 M
20 Omar 10/5/1998 M
30 Sami 5/5/1978 M
40 Osman 8/9/1990 M
50 Susan 7/6/1981 F
60 Asma 6/7/1997 F
70 Toni 5/7/1985 M
80 Rania 6/9/1978 F
90 Suad 6/7/1995 M
100 Tareq 5/9/1997 M
references
Table 2: visit
vst_no(pk) Pat_no(FK) vst_date
1 10 1/5/2011
2 40 6/5/2012
3 10 6/5/2011
4 60 8/7/2012
5 80 2/2/2013
6 40 6/6/2012
7 70 9/9/2012
8 80 4/2/2013
Join 1) Cartesian join select pat_name ,pat_db,vst_date
from patient, visit;
2) Equi join select pat_name, pat_db,vst_date
from patient,visit
where patient.pat_no=visit.pat_no
order by pat_name