Professional Documents
Culture Documents
Advantages of DBMS:
DATA ABSTRACTION:
1
Physical Level:
Conceptual Level:
View Level:
DATA MODELS:
They are used in describing data at the conceptual and view levels.
Some of the widely known models are
2
They are used in describing data at the conceptual and view levels. As the
database is structured in fixed-format records of several types, they are
called as record based logical models.
Some of the widely known models are:
DATABASE FACILITIES:
3
1. Retrieval of information stored in the database.
2. Insertion of new information into the database.
3. Deletion of information from the database.
4. Modification of data stored in the database.
There two types of DMLS:
1. Procedural DMLs requires a user to specify what data is
need and how it get it.
2. Non Procedural DMLs requires a user to specify what data
is needed without specifying how to get it.
STRUCTURE OF A DBMS:
Data Manager:
File Manager:
It is responsible for the structure of files and managing the file space
and also for locating the block containing required record, requesting this
block from the disk manager, and transmitting the required record to the data
manager.
Disk manager:
4
The disk manager transfers the block containing the data requested by the
file manager without the concern of physical characteristics of the underlying
storage media.
Query Processor:
The query processor is used to interpret the online user’s query and covert it
into an efficient series of operations in a form capable of being sent to the
data manager for execution. It uses data dictionary to find the structure of the
relevant portion of the database and uses this information in modifying the
query.
Data Files:
Data Dictionary:
A data dictionary is a file that contains metadata. i.e. data about data. This
file is consulted before actual data is read or deleted in the database system.
Ex: empno attribute in the employee table and deptno attribute in dept table.
2. Foreign key: An attribute which is a primary key in its own table and is
used as a reference in another table is called foreign key.
3. Super Key: A primary key with a combination of other attributes for
unique identification is called super key. In other words primary key is a
minimum super key.
Ex: Empno along with ename is super key.
4. Candidate Key: The super key for which no proper subset is a super
key such a minimal super key is a candidate key.
5
Ex: Ename is a candidate key.
STRUCTURED QUERY LANGUAGE(SQL)
SQL is the official standard language used to access data held in the
databases.
SQL organizes data as tables , indexes , views etc. SQL is the tool
for organizing , managing and retrieving data stored in the database.
SQL provides various features like portability , client-server
architecture ,dynamic data definition , multiple views of data etc.
SQL Statements:
6
It specifies integer type of data with max of n digits.
4.Number(p,s):
It specifies floating number with p as total no of digits and specifies the
number of digits to the right of decimal points.
Syntax:
Column constraints :
1. Primary Key: It will not allow null and duplicate values corresponding to
that column.
2. Not Null : It will allow null values corresponding to that column
3. Unique: It will not allow duplicate values corresponding to that column.
4. Check: It will impose the constraints based on the condition being
mentioned on the column.
5. Reference( foreign key): It will impose the constraints based on the
condition being mentioned on the column
6. Reference (foreign key): It will refer to other column values that are
acting as primary key in other table.
7. On delete cascade: It will delete the values in the column in one table,
corresponding values in other table will be deleted automatically, and it
will work only references only.
Example 1:
7
Create a table employee with employee no as primary key, name
field not be left empty, salary greater than 2000,job field not to be left
empty,deptno is foreign key taken from dept table.
Example 2:
8
Proj_id number(5),
Payment number(8),
Primary key(Emp_id,Proj_id);
To create a table from taking the records from existing table. When creating
a table based on existing table then it is going to transfer only not null
constraint, other constraints will not be transferred.
Example 1:
SQL>Create table Emp
As select Empno, Ename, Salary from Employee;
Syntax:
Alter table<table_name>
Add column_name data_type <column_constraint>
[Modify column_name data_type <column_constraint>];
Add: is used when ever you want to add a new column to the table.
9
Modify: is used to change the size of the column of the same data
type.Modify will not decrease the column size.Whenever it comes to increase
in the size of column it is going to allow only when the field is empty.
Example 1:
Syntax:
Drop table<table_name>;
10
Data Manipulation Language (DML):
Insert command: is used to add rows(or records) to the table. The number
and sequence should match that of columns in the table.If the number of
data values is less, then specify the column names into which data is being
entered. To insert null values, NULL can be used.
Syntax:
Insert into<table_name>values(datavalue_1,………..,datavalue_n);
Insert into<table_name>(column_1,column_3)
Values(datavalue_1,datavalue_3);
Delete Command: is used to delete the records from the specified table.
Syntax:
Delete <table_name>
Where<condition>;
11
Example 1: Deleting the employee holding empno1001
Syntax:
Update <table_name>
Set <field_name> = <value> ,
.
.
.
Where <condition> ;
Example4: Give an increment of 10% salary whose salary is less than Rs.
1000
SQL> update employee set sal = sal*1.1 where sal <1000;
The DML transactions will not be saved implicitly based on the following
reasons.
12
1. Improper shut down
2. System crash
3. When working under SQL environment DML transactions will not
saved.
To handle DML transactions explicitly we require a set of commands which
are commit, roll back and save point.
SQL> Rollback;
Example:
13
Data Retrieval language(DRL);
Syntax:
Select < field_name_1>, …….. , <field_name_n> from <table_name>
where <condition> ;
Example 1: To display the deptno,dname,loc.
Logical Operators:
• And
• Or
• Not
Example 1: To display the employee information who are working in deptno
10 and salary greater than 2000.
SQL>select * from emp
Where deptno=10 and sal>2000;
Example 2: To display the employee information who are working as
manager corresponding to deptno 20.
SQL>select * from emp
Where job=’manager’ and depno=20;
Example 3: To display the employee information who are working in deptno
10,20.
SQL>select * from emp
14
Where deptno=10 or dept=20;
Example 4: To display the employee information who are working as
managers corresponding to deptno 10 as well as the employees who are
receiving salary more than 2000 corresponding to deptno 20.
Special Operators:
Is null
In
Between
like
Example 1: To display the employee information who are not receiving any
commission.
In: When you have multiple conditions among which any one has to be
selected we use in operator.
Example 1: To display the employee details who are working as managers
,clerks ,analyst.
15
SQL>select * from emp
Where job in(‘manager’,’clerk’,’analyst’);
Example 2:To display the employee who are working in the deptno 10,20,30.
SQL>select * from emp
Where deptno in (10,20,30);
Example 1: To display the employee details whose names are ending with
‘s’.
SQL>select * from emp
Where ename like’%s’;
Example 3: To display the employee details whose name has ‘a’ as second
character and ‘r’ as last character.
SQL>select * from emp
Where ename like’_a%r’;
Example 2:To display employee information for whose who are working as
managers and receiving salary other than the range 2000 to 3000.
SQL>select * from emp
Where job=’manager’ and sal not between 2000 and 3000;
Example 3:To display the employee details who are working in dept other
than 20,30.
SQL>select * from emp
Where deptno not in (20,30);
16
Example 4:To display the employee details who are working in deptno 10,20
for their names not ending with ‘s’.
SQL>select * from emp
Where deptno in(10,20) and ename not like ‘%s’;
o Number Functions
o Character Functions
o Date functions
o Group functions
Group Functions:
17
SQL>select sum(sal+comm.) from emp
Where deptno=10;
4.count(expr): Is used to count the no.of records will the expr is satisfied.If
a record has a null value it will not count that record.
Example 1: Find the no.of employees who are receiving the commission.
SQL>select count(comm.) from emp;
Example 2: Find the no.of employees who are receiving the commission
corresponding to deptno.10,20.
SQL>select count(comm) from emp
Where deptno=10 or deptno=20;
5.max(expr): Is used to find the maximum value for the mentioned
numerical attribute.
Example 1: Find the employees who is paid the highest salary.
SQL>select max(sal) from emp;
6.min(expr): Is used to find the minimum value for the mentioned numerical
attribute.
Example: find the employees who is paid the least salary.
SQL>select min(sal) from emp;
18
Numerical Functions:
abs(n)
sqrt(n)
power(n)
mod(n)
1.abs(n): Is used to find the absolute value of n.dual is dummy table for
Temporary manipulation.
19
CHARACTER FUNCTIONS:
Length(str)
Concate(str1,str2)
Substr(str,pos,n)
Ascii(char)
Chr(n)
20
SQL>select substr(‘student’,3,4) from dual;
21
8. upper(str): Converts the given string into upper case.
DATE FUNCTIONS:
3. Last day(date): Returns the last day in the month specified by the date
mentioned.
22
Group by clause: Is used to group a set of repeated values corresponding
to a particular column for which we want to apply group function.
23
Having Clause: Is used when we want to frame the condition of salary is
greater than cannot be used with where clause.
Example 2: To display the deptno.s along with their total salary for which
total salary is greater than 10,000 corresponding to deptno 10,20.
SQL>select deptno,sum(sal) from emp
Where deptno in (10,20)
Group by deptno
Having sum(sal)>10000;
Example 3 : To display the job’s along with the no.of employees working
,provided the highest salary corresponding to the job is greater than 3000
as well as the no.of employees working is more than 3.
SQL>select job,count(*) from emp
Where deptno in (10,20,30)
Group by job
Having max(sal)>2000 and count(*)>2;
24
Order by clause: Is used when we want to display the data in a sorted
order.
1. Ascending order |asc| (default ordering)
2. Descending |desc|
25
SET OPERATORS:
• Union
• Intersection
• Minus
1. union: Is used to take distinct rows from more than one select
statement.By using union all the duplication is not removed.
2.intersect: Is used to take common rows from more than one select
statement.
26
Example 1: To dispalyb the job’s in deptno. 10 and 20.
SQL>select job from emp
Where deptno=10
Intersect
Select job from emp
Where deptno=20;
2. minus: Is used to select the rows from more than first select statement
which are not in the second statement.
27
SUB QUERIES: when we are using more than one select statement to
perform particular operation then we will be calling it as a subquery.In
subquery the inner statement will be executed first and only noce as
compared to the outer statement.
28
SQL>select * from employee
Where sal=(select max(sal) from emp);
Example 3: To display all the employee’s information whose job is same like
Smith at the same time salary greater than Allen.
Example 4:To display all the employee’s information who is receiving salary
greater than average salary of all the employee’s.
Example 5:To display the manager no.alng with the no.of employee’s
working under him should be maximum no.of employees compared with the
other members.
29
When the subquery is returning more than one value to frame the condition
we have to use all or any.
Advantages of views:
30
Providing the security for the table.
By giving limited access to your table to other users.
Complicated queries can be generated by using views a normal
simple table creation.
Creation of view:
31
Replace: Replace option is used whenever you want to over write the
existing select statement with new select statement.
With check option: When ever you want to restrict the user to insert thr
records which are satisfying the where condition of the view then we use with
check option.
32
Where deptno=20
With check option;
If you try to enter deptno other than 20,it will not be accepted into the view.
Altering the sequence: whenever you want to change any options related t
33
Drop sequence: Whenever you want to drop a sequence we use drop
statement.
Syntax: drop sequence <seq.name>
34
PL/SQL(Procedural Language using sql)
PL/SQL can be called the extension of SQL where it is going to support
the SQL statement along with the processing statement we will be calling
it as Procedural Language.
Syntax: Declare
: /*Declaring block*/
Begin
: /*Execution block*/
Exception
: /*Exception block*/
End;
SQL>ed area
Declare
R number(2):=10;
Pi constant number(4,3):=3.14;
A number(7,2);
Begin
A:=Pi*R*R;
DBMS_output.put_line(‘area is=’||to_char(a));
End;
/
35
SQL>@area; /* To execute the PL/SQL block*/
SQL>ed mcom
Declare
Eno number(5):=&emp
Comm1 emp1.comm%type;
Sal1 emp1.sal%type;
Begin
Select comm.,sal into comm1,sal1 from emp
Where empno=eno;
If comm1 is null then
Update emp1
Set comm.=sal*.1
Where empno=eno;
Else
DBMS_output.put_line(‘Commission is existing for’||to_char(eno));
End if;
End;
/
SQL>@mcom;
TRIGGER: Triggers are procedures that are sotored in the database not in
the complied form and implicitly fired when an event
occurs.Traditionally,triggers guarantee that when a specific operation is
performed related actions are performd.Triggers support the execution of a
Pl/SQL block when an INSERT<UPDATE,or DELETE occurances on a table
view.
Advantages of Triggers:
36
Before/after insert or delete or update on <table name> for each
row.
Begin
:
:
End;
SQL>ed t1
Create or replace trigger eins
Before insert on emp for each row
Begin
If:new.sal>5000 then
Raise_application_erroe(-20001,’salary above 5000’);
End if;
End;
/
SQL>@t1
37
SQL>ed t2
Create or replace trigger updel
Before update or delete on dept for each row
Begin
Declare
No number(4);
If updating or deleting then
Select count(*) no from emp
Where deptno=:new.deptno;
If (no>1) then
Raise_application_erroe(-20001,’Records existing in
emp table’);
End if;
End if;
End;
/
SQL>@ t2
38
/*PL/SQL program for checking if a number is prime or not*/
SQL>ed prime.sql
Declare
N number:=&n;
I number:=2;
R number(3):=0;
Begin
While(i<=n/2)
Loop
R:=mod(n,i);
If r=0 then
Exit;
End if;
I:=i
1;
End loop;
If r=1 then
Dbms_output.put_line(‘prime’);
else
dbms_output.put_line(‘not prime’);
end if;
end;
/
SQL>@ prime;
OUTPUT:
39
/*PL/SQL Program for calculating telephone bill*/
Declare
Cursor t1 is select tno,prev,present from telbill order by present desc;
Tree t1%row
type;
Diff telbill.prev%type:=0;
Begin
Open t1;
Loop
Fetch t1 into trec;
Exit when t1%notfound;
Diff:=trec.present-trec.prev;
If diff between 0 and 200 then
Update telbill
Set pay=diff*0.80
Where tno=trec.tno;
40
Elsif diff between 200 and 400 then
Update telbill
Set pay=160+(diff-200)*1
Where tno=trec.tno;
Else
Update telbill
Set pay=160+200+(diff-400)*1.25
Where tno=trec.tno;
End if;
End loop;
Close t1;
End;
/
41
/*PL/SQL Program for Displaying the salesperson who has reached the
Target*/ .
SQL>create table sales1
(sno number(2),
Sname char(15),
Sbr cahr(15),
Tgt number(5));
Declare
Cursor s1 is select * from sales1 order by sbr desc;
Srec s1%rowtype;
Begin
Open s1;
Dbms_output.put_line(‘SALES PERSON WHO HAVE REACHED THE
TARGET’);
Dbms_output.put_line(‘-----------------------------------------------‘);
Loop
Fetch s1 into srec;
Exit when s1%notfound;
If srec.tgt>=7500 then
Dbms_output.put_line(‘--------------------------------------------‘);
Dbms_output.put_line(‘sales person name=’||srec.sname);
Dbms_output.put_line(‘branch name=’||srec.sbr);
End if;
End loop;
42
Close s1;
End;
/
43
/*PL/SQL program for calculating electricity bill*/
Declare
Cursor e1 is select cno,prev,pres from elect order by eno desc;
Erec e1%rowtype;
Diff elect.prev%type:=0;
Begin
For erec in e1
Loop
Diff:=erec.pres-erec.prev;
If diff between 0 an 500 then
Update elect
Set pay=diff*0.80
Where cno=erec.cno;
44
Elsif diff between 0 and 1000 then
Update elect
Set pay=diff*1.20
Where cno=erec.cno;
Elsif diff between 0 and 1500 then
Update elect
Set pay=diff*1.60
Where cno=erec.cno;
Elsif diff between 0 and 2000 then
Update elect
Set pay=diff*2
Where cno=erec.cno;
Else
Update elect
Set pay=diff*2.50
Where cno=erec.cno;
End if;
End loop;
End;
/
45
/*Program for displaying Multiplication table of a given number*/
Declare
Num number (2):=&no1;
Res number(2);
Begin
For I in 1…10
Loop
Res:=num*I;
Dbms_output.put_line(num||’*’||’=’||res);
End loop
End;
/
46
/*Program to check if a given no.is Perfect Square or not*/.
Declare
Num number(2):=&no;
N number(2);
Flag number(1):=0;
Begin
N:=num/2;
For I in 1…n
Loop
Ifmod(num,i)=0 and i*i=num then
Flag:=1;
Dbms_output.put_line(num||’is a perefect square’);
Exit;
End if;
End loop;
If flag=0 then
Dbms_output.put_line(num||’is not a perfect square’);
End if;
End;
/
47
/*Program to check if a given string is Palindrome or not*/
Declare
S varchar2(10):=’&str’;
Len number(2);
C char(1);
T varchar2(10);
Begin
Select length(s) into len from dual;
For i in reverse 1..len
Loop
C:=substr(s,I,1);
T:=concat(t,c);
End loop;
If s=t then
Dbms_output.put_line(s||’is a palindrome’);
Else
Dbms_output.put_line(s||’is not a palindrome’);
End if;
End;
/
48
/*Program to generate Fibonacci Series*/
Declare
F1 number(3):=0;
F2 number(3):=1;
F3 number(3):=0;
J number(3):=&limit;
Begin
For I in 1..j
Loop
F3:=f1+f2;
Dbms_output.put_line(to_char(f3));
Insert into fibo
Value(f3);
F1:=f2;
F2:=f3;
End loop;
End;
/
49
/*Program for generating the Pay Slip for each Employee No.given*/
Declare
I number(5):=&eno;
Cursor e is select empno,ename,sal from emp where empno=I;
No emp.empno%type;
Name emp.ename%type;
S emp.sal%type;
Da number(7,2);
Hra number(7,2);
Pf number(7,2);
Net number(7,2);
Begin
Open e;
Loop
Fetch e into no,name,s;
Exit when e%notfound;
Dbms_output.put_line(‘______________________’);
Dbms_output.put_line(‘******STAR COMPANY******’);
Dbms_output.put_line(‘*****PAY SLIP********’);
Dbms_output.put_line(‘____________________’);
Dbms_output.put_line(‘empno=’||no);
Dbms_output.put_line(‘emplyee name’||name);
Dbms_output.put_line(‘basic salary=’||s);
Da:=s&0.025;
Dbms_output.put_line(‘da=’||da);
Hra:=s*0.05;
Dbms_output.put_line(‘hra=’||hra);
Pf:=s*0.015;
Dbms_output.put_line(‘pf=’||pf);
Net:=s+da+hra-pf;
Dbms_output.put_line(‘gross salary=’||net);
End loop;
Close e;
End;
/
50
/*Program to insert in the table & display the Division of the given
Student*/
SQL>create table s
(rno number(3),
M number(3),
S number(3),
C number(3),
Div varchar(20));
Declare
R number(3):=&rollno;
M1 s.m%type;
S1 s.s%type;
C1 s.c%type;
D s.div%type;
P number(7,2):=0;
Begin
Select m,s,c into m1,s1,c1 from s where rno=r;
P:=(m1+s1+c1)/3;
If p>70 then
D:=’distinction’;
Elsif p between 60 and 70 then
D:=’first division’;
Elsif p>=50 and p<=60 then
D:=’second division’;
Elsif p>=40 and p<=50 then
D:=’third division’;
Else
D:=’fail’;
End if;
Update s
Set div=d
Where rno=r;
Dbms_output.put_line(‘result=’||d);
End;
/
51