2. 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
4. 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
5. 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
6. 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.
7. 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
8. 1) Create user , grant create session , grant connect,
resource
19. c) select ename, sal*12 as ann_sal from empdata
d) select sysdate from dual;
e) select 545*87 from dual;
20.
21. f) select ename || ' earns ' || sal || ' every month '
info from empdata
22. 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
23. h) select * from empdata
where sal >5000 and nationality='Saudi';
i) select ename, sal , mobile
from empdata
where sal between 10000 and 60000
24. j) select ename , sal , nationality
from empdata
where ename like '%m%'
k) select * from empdata
where ename like 'm%';
25. L) select * from empdata
where ename like '%h';
m) select ename, sal
from empdata
where ename like '_b%';
26. n) select * from empdata
where ename like '____';
o) select * from empdata
where ename like 'o__r';
27. p) select * from empdata
where sal in (6000,5000,55000,60000)
q) select * from empdata
where ename in ('omar','Amer')
29. 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)
30. 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
31. 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 ;
32. 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
33. 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;
34. 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
35. a)
select ename, upper(ename), lower(ename), initcap(ename)
from empdata
b)
select * from empdata
where upper(ename) like '%S%';
40. 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
41. 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
42. c. select next_day ('2-7-2013', 1) from dual
note : 1(Saturday) , 2( Sunday),………..
d. select last_day ('2-7-2013') from dual
44. 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
45. c. select to_char(sysdate, 'HH:MI:SS AM ') from dual;
IIV) Group functions
a. select count (sal), sum (sal), avg(sal) from empdata
46. 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
47. c. select deptno ,sum(salary) from empcomp
group by deptno;
d. select deptno,job, sum(salary) from empcomp
group by deptno,job;
48. 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;
50. delete rows , update data
a. delete from empdata
where empno=11;
d.
update empdata
set nationality ='Saudi'
where empno =10;
51. 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.
52. 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
53.
54.
55. 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
56. 3) Outer join
select pat_name, pat_db,vst_date
from patient,visit
where patient.pat_no (+) =visit.pat_no
4) Non - equi join