You are on page 1of 31

SHRI C.J.

PATEL COLLEGE OF COMPUTER STUDIES

Subject Code: 2CS1010302

Subject Name:-

ADVANCED DATABASE MANAGEMENT SYSTEM

Faculty Name – Hemant Patel

1
ADVANCE DATABASE MANAGEMENT SYSTEM
Unit-1
Introduction of RDBMS / ADBMS
What is Data?
• Data is different types of information usually formatted in a
particular manner.
• Store meaningful information in proper structure and manner
its is called data.
• Data is information that has been translated into a form that
is efficient for movement or processing.
What is Information?
Information is defined as classified and organized data that has
some meaningful value for the user. Information is also the
processed data used to make decisions and take action.
What is Database?
A database is an organized collection of structured information, or
data, typically stored electronically in a computer system. A
database is usually controlled by a database management system
(DBMS). Together, the data and the DBMS, along with the
applications that are associated with them, are referred to as a
database system, often shortened to just database.
What is DBMS?
DBMS is a software or group of programs designed to perform
database operations such as creating a new database,
administering and managing the databases by performing various
user-specified database operations. The DBMS internally might
consist of a group of programs each performing different tasks
related to database management. Some DBMS examples, My
SQL, Oracle, System 2000, MS Access, My SQL server, etc.

2|Page
ADVANCED DATABASE MANAGEMENT SYSTEM

What is RDBMS?
RDBMS is the collection of programs and capabilities that enables
the user to interact with a relational database. A relational
database management system (RDBMS) is a type of DBMS with
row-based table structure. Most commercial RDBMS is use SQL.
The most basic RDBMS functions are related to create, read,
update and delete operations, collectively known as the CRUD
cycle.
Features of RDBMS:
• An RDBMS is easily accessible using SQL commands.
• An RDBMS provides full data independence.
• The basic unit of data storage in a relational database is
called a table.
• A table consists of tuples/rows/records and each record has
one or more columns used to store values.
• In RDBMS, we can use conditional operations such as joins
and restrictions.
• An RDBMS enables data sharing between users.
• Also at the same time, you can ensure consistency of data
across multiple tables by using integrity constraints.
• An RDBMS minimizes the redundancy of data.

Advantages of RDBMS:
• Support for a very large database.
• Automatic optimization of searching (when possible).
• RDBMS has a simple view of the database that conforms to
much of the data used in businesses.
• RDBMS uses Structured Query Language.
• Easy extendibility, as new data may be added without
modifying existing records this is also known as scalability.

3|Page
ADVANCED DATABASE MANAGEMENT SYSTEM

• RDBMS has data security which is critical when data sharing


is based on privacy.
• RDBMS defines how the data is organized and how the
relations among them are associated.
• It defines the entities and relationships among them. It
contains a descriptive detail of the database.
Disadvantages of RDBMS:
• No support for complex objects such as documents, video,
images.
• Often poor support for storage of complex objects.
• Still no efficient and effective integrated support.
Difference between DBMS RDBMS
ADBMS RDBMS
The concept of a relationship is not It is based on the concept of
there. relationship.

The speed of operation is slow. The speed of operation is fast

The hardware and software The hardware and software


requirements are very low. requirements are very high.

It uses the concept of files. It uses the concept of tables.

The facilities and utilities offered are The facilities and utilities offered are
limited. not limited.

DBMS normally uses a 3GL. RDBMS normally uses a 4GL.

Ex: Dbase, Fox base Ex: ORACLE, FOCUS

4|Page
ADVANCED DATABASE MANAGEMENT SYSTEM

E.F. Codd’s Rules:


Dr E.F.Codd, also known to the world as the „Father of Database
Management Systems‟ had propounded 12 rules which are in-fact
13 in number. The rules are numbered from zero to twelve.
According to him, a DBMS is fully relational if it abides by all his
twelve rules. Till now, only few databases abide by all the eleven
rules. His twelve rules are fondly called „Edgar Frank Codd‟s
Twelve Commandments‟. His brilliant and seminal research paper
„A Relational Model of Data for Large Shared Data Banks‟ in its
entirety is a visual treat to eyes.
Rule 1: The information Rule.
“This rule requires that all information should be represented as
data values in the rows and columns of the table. This is the basis
of the relational model.”
Rule 2: The Guaranteed access Rule.
“Each data value in a relational database should be accessible
logically by specifying the relation‟s name, primary key value, and
the attribute name.”
Rule 3: Systematic treatment of null values.
“DBMS must support NULL values to represent missing or
inapplicable information. NULL values must be distinct from zero
or spaces. It must be independent of data type.”
Rule 4: Active online catalog based on the relational model.
“System catalog is a collection of relations that the DBMS
maintains for its own use. These relations hold the description of
the structure of the database.”

5|Page
ADVANCED DATABASE MANAGEMENT SYSTEM
Rule 5: Comprehensive data sub-language Rule.

“This rule states that the system must support at least all of the
following functions.”
Data Definition
View Definition
Data Manipulation Operation (Interactive and by program).
Security and Integrity Constraints
Transaction Management Operations
Rule 6: View updating Rule.
“All views which are theoretically updatable should be updatable
by the system.”
Rule 7: High-level insert, update and delete.
“The rows should be treated as sets during insert, update and
delete operation. The operation that modifies the database should
deal with sets and not with single rows.”
Rule 8: Physical data independence Rule.
“The application programs must remain unchanged when any
changes are made in storage representation or access methods.”
Rule 9: Logical data independence.
“The changes that do not modify any data stored in that relation,
do not require changes to be made to the application program.”
Rule 10: Integrity independence Rule.
“Integrity constraints must be specified independently of
application programs. They must be stored in the catalog.”

6|Page
ADVANCED DATABASE MANAGEMENT SYSTEM

Rule 11: Distribution independence.


“Existing applications should operate successfully when the data
is distributed. The physical location of data, the control program,
and the application may be different but the distribution should not
affect the functioning of the application.”
Rule 12: Non-subversion Rule.
“Database security and integrity should not be bypassed. The
DBMS must ensure that no other level can bypass the constraints
specified to maintain the integrity of the database.”
Reference Link
https://dotnettutorials.net/lesson/introduction-to-rdbms/
https://www.w3schools.in/dbms/intro
https://www.tutorialspoint.com/e-f-codd-s-12-rules-for-rdbms

7|Page
ADVANCED DATABASE MANAGEMENT SYSTEM
Unit – 2
ADBMS

SQL: - Structure Query Language


▪ Structured Query Language (SQL)

▪ SQL is a data sub-language that has constructs for defining and


processing a database

– Used stand-alone within a DBMS command

– Embedded in triggers and stored procedures

– Used in scripting or programming languages

SQL was developed by IBM in late 1970s


– SQL statement is terminated with a semicolon

• Developed by IBM in the 1970’s, originally called Sequel

• The standard relational-database language

o Data Types:-

– Char for fixed-length character


Character storing capacity 2000 bytes
– VarChar for variable-length character
• It requires additional processing than Char data types

• Character storing capacity 4000 bytes

– Date 07 bytes

SQL parts:
• DDL:-Data Definition Language
• DML:-Data Manipulations Language
• TCL:-Transaction Control Language
• DCL:-Data Control Language

o DDL-: Data Definitions Language

– Data Definition Language (DDL) is used to define database


structures

o Create
o Alter
o Drop
o Rename
o Truncate
8|Page
ADVANCED DATABASE MANAGEMENT SYSTEM
▪ Create Command:
o CREATE TABLE statement is used for creating relations
o Each column is described with three parts: column name,
data type, and optional constraints

Syntex:
Create table <table_name>
(col1 datatype(size),
Col2 datatype(size));

Example:-
– CREATE TABLE Emp

(emp_no number(3),
emp_name varchar2(10),
Job varchar2(30),j_date date,
Salary number(12,2),sex char(1),
Dept_no number(2));

o ALTER Statement:-

▪ ALTER statement changes table structure, properties, or


constraints after it has been created

▪ Modify the structure of object (table).

▪ Syntax:

– Alter table <table_name>

Add / modify (col_name


Data type (size));
– Alter table <table_name>

ADD CONSTRAINT <constraint name>


Constraint (col_name) REFERENCES
<Table_name> (col_name));

▪ Example:

– Alter table Emp add commission number(8,2);


– ALTER TABLE Emp

ADD CONSTRAINT EmployeePK


Primary key (Emp_no);
– Alter table Emp modify ename varchar2(25);

9|Page
ADVANCED DATABASE MANAGEMENT SYSTEM
o DROP Statements:-
▪ DROP TABLE statement removes tables and their data from the
database
▪ A table cannot be dropped if it contains foreign key values
needed by other tables

▪ Syntax:
DROP TABLE <table_name>

▪ Example:
DROP TABLE CUSTOMER;

o RENAME Statement:-

▪ RENAME statement change the table name from the database


▪ Syntax:
RENAME <old table_name> to <new table_name>
– Example:

RENAME emp to employee;

o DML-: Data Manipulations Language


▪ Data Manipulation Language (DML) is used to query and
Update data

o Insert
o Update
o Delete
o Select

o INSERT Statement:-
▪ To insert record into table object
▪ Syntax:
Insert into <Table_name>
Values (&col1, &col2, &col…..);
Or.
Insert into <Table_name>
(&col1, &col2, &col…..);
Values (val1, val2,val…..);

10 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
▪ Example:

Insert into Emp


Values (&empno,’&job’, &salary,
&deptno);

Insert into Emp


Values (empno,job,salary,deptno)
Values (2,’cleark’,2050, 20);

o UPDATE Statement:-
▪ Update and modify record and set of record to use
Update statement
▪ Syntex:
Update <Table_name>
set <col_name>= <value>
where <col_name>=<value>
▪ Example:
Update Emp
set ename= ‘mohit’
where eno=2;
Update Emp
set ename= upper(ename),
salary=salary+100;

o DELETE Statements:-
▪ Delete the single record and set of record to use
Delete command.
▪ Syntex:
Delete <Table_name>
[where <condition>]
Example:
– Delete Emp;
– Delete Emp where emp_no=1;
– Delete Emp where job=’Manager’;

o SELECT Statements :-
▪ Select command is use for retrieve data from particular
table
▪ Syntex:
– Select <col_list> from <table_name>
Where <condition>
– Select <col_list> from <table_name>
11 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
[Where <condition>]
[group by <con_name>]
[having <condition>]
[order by <con_name>]
[desc];

▪ Example:
– Select *from Emp;
– Select emp_no,emp_name,salary from Emp;
– Select salary+1000 from Emp
Where job=’manager’;

o TCL-:Transactional Control Language

– Transactional Control Language (TCL) is used to control


transaction in database

o Commit
o Rollback
o Savepoint

o COMMIT Statement:-

▪ Commit is used to save data in parmenantly store in


database.

▪ Commit save data from buffer and unsaved data.

▪ Syntex & Example:


Sql> update Emp set salary =3000
Where emp_no = 2;
Sql> commit;
▪ So ,salary will be change of emp_no is 2 and save
Parmenent in database

o ROLLBACK Statement:-
▪ Rollback is used for cancel the transaction
▪ Rollback is used to remove transaction
▪ Rollback is one type of undo statement
▪ Syntex:
– Rollback;
– Rollback to savepoint <savepoint_name>

12 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
▪ Example:
Sql> delete Emp;
Sql>select *from Emp;
Sql>rollback;

– In example do not delete emp table because use


rollback

o SAVEPOINT Statement:-
▪ Savepoint is a bookmark for reader
▪ Savepoint is create one area for statement
▪ Syntex:
Savepoint <savepoint name>

▪ Example:
Sql> savepoint p1;
Sql> delete Emp where dept_no=10;
Sql> savepoint p2;

Sql> update Emp set salary=3000


Where emp_no=5;

Sql> savepoint p3;


Sql> delete Emp where dept_no=20;
Sql> rollback to savepoint p2;

– In savepoint p2 statement update do not update


data of emp_no =5 because use rollback

o DCL-:Data Control Language

– Data Control Language (DCL) is used to controlling data in


database.

– To give access second user and taken away rights from second
user by DCL command.

o Grant
o Revoke

o Grant command:-

▪ Give right to access the table and object to other


user.

13 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
▪ Give all privilages(select,delete,alter,insert) to
second user .

▪ Syntex :

Grant <privileges / All> on <table_name>


to <user> [with grant option];
▪ Example:
Grant all on Emp to scott;
Grant select,update,insert on Emp to bca;
Grant all on Emp to scott
with grant option;
▪ Grant-> second user do not give right to third user
to access database.
▪ Grant option-> second user give right to third user
to access database.

o Revoke command:-

▪ Taken away rights to access the table and object to


other user.

▪ Remove all privilages(select,delete,alter,insert) to


second user .

▪ Syntex :
Revoke <privileges / All> on <table_name>
from <user>;
▪ Example:
Revoke all on Emp from scott;
Revoke select,update,insert on Emp from
bca;

o Operators:-
(1) Arithmetic: +,-,*,/
(2) Relational: >,<,<=,>=,!=,between,like,is,in,<>
(3) Logical: And,Or,Not

Query with Select and operators:

Sql>select *from Emp where salary >= 4000;

Sql>select *from Emp where dept_no=10 and job=’manager’;

Sql>select *from Emp where emp_no between 20 and 50;


14 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
Sql>select *from Emp where salary is null;

Sql>select *from Emp where salary like ‘_7_ _’;

Sql>select *from Emp where ename like ‘a%’;

Sql>select *from Emp where ename like ‘%a%a%a%a’;

% - group of character
_ (under score) – single character

Sql>select *from user_constraints


where table_name like ‘e%’;

Sql>select *from Emp


where (dept_no != 10 or job = ‘manager’)
and salary > 2500;

Sql>select *from Emp where dept_no


Not in (20,40);

Sql>Delete Emp where dept_no=10 and sal < 1500;

Sql>Update Emp set ename =’Mr’ || ename


Where sex=’M’ or sex=’F’;

String concatenate -> double pipe || sign

Sql>select ‘Rs’|| salary “Total salary” from Emp;

❖ Order By Clause:

– Order by clause is used to display record in order


ascending and descending order
– Sql>Select ename,salary,city from Emp order by
city(desc)

– Sql>Select ename,salary from Emp group by salary(asc)

o Sorting the Results:

▪ ORDER BY phrase can be used to sort rows from SELECT


statement

15 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
SELECT Name, Department FROM EMPLOYEE
ORDER BY Department;
▪ Two or more columns may be used for sorting purposes

SELECT Name, Department FROM EMPLOYEE


ORDER BY Department DESC, Name ASC;
o Built-in Functions and Grouping

▪ GROUP BY allows a column and a built-in function to be used


together

▪ GROUP BY sorts the table by the named column and applies the
built-in function to groups of rows having the same value of
the named column

▪ WHERE condition must be applied before


GROUP BY phrase

▪ Example:
SELECT Department, Count(*) FROM EMPLOYEE
WHERE Emp_no < 600
GROUP BY Department
HAVING COUNT(*) > 1;
▪ ON DELETE CASCADE removes any related referential integrity
constraint of a deleted row

Predicate:-
1.BEETWEEN (Range Operator)
2.LIKE (Pattern Operator)
3.IN (OR Operator)
4.NOT IN

BEETWEEN:
It define range of data from database
Exa.
Select *from student
where sname between ‘a%’ and ‘g%’;
LIKE
% - percent sign matches any string
_ - underscore matches any single character
Exa.
Select *from student
Where sname like ‘a%’;

Select *from student


Where sname like ‘_h%’;

16 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
IN
In operator is same as OR operater
Exa.
Select *from student
Where odate=’03-oct-99 or odate=’04-oct-99
Or odate=’05-oct-99’;

Select *from student Where odate


in(‘03-oct-99’,’04-oct-99’,05-oct-99’)

NOT IN
Exa.
Select *from student Where odate
in(‘03-oct-99’,’04-oct-99’,05-oct-99’)

Connect
username :system
password :manager
* Create new user:-
Syntax:
Create user <user_name> identified by
<password>;
Exa.
Create user hemant identified by
lecturer;
Grant connect,resources to <user_name>;
Grant connect,resources to piyush;

* Show all users:-


Select *from all_users;
* Show current users :-
Select user from dual;
* Alter password of user name:-
Connect
Username :system
Password :manager
Syntax:
Alter user <user_name> identified by
<password>
Exa:
Alter user hemant identified by lecturer11;

17 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
Unit – 3
o VALIDATION OF DATA

– Oracle provide data validation by integrity constraint and


referential constraint .

– Data validation provide at created table and alter table.

o Constraints
▪ Domain
1. Null
2. Not Null
▪ Entity
1. unique
2. primary key
▪ Referential
1. Foreign key

o Null and Not Null

– Null const. is allow and insert null value in column.

– Null in != 0 and space

– Not null do not allow and insert null value in column.

– Syntax:

Create table <table_name>


( col datatype(size) constraint_name);

Example:
Create table Emp (emp_no number(3),
emp_name varchar2(10) Null );
Create table Emp (emp_no number(3),
emp_name varchar2(10) Not null );
o Check

– ckeck const. is used to check value inserted by user in column

– its chech the value of column.

– Syntax:

Create table <table_name>


( col datatype(size) constraint_name);

18 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
Example:
Create table Emp (emp_no number(3)
Ckeck (emp_no in(1,3,5),
Sex char(1) check (sex in (‘M’,’F’) );
o Unique:

– Unique const. is used to insert distinct value and null value


by user in column .

– Unique is used for insert unique record.

– Syntax:

Create table <table_name>


( col datatype(size) constraint_name);
Example:
Create table Emp (emp_no number(3) Unique)

o Primary key:

– Primary key is uniquely identification of record in tabl.

– Primary key is not allow null value of record.

– Primary key = unique + not null

– Do not taken duplicate value of record.

– Syntax:

Create table <table_name>


( col datatype(size) constraint_name);
Example:
Create table Emp (emp_no number(3) primary key);

o Foreign key:

– foreign key is used in relational table.

– Foreign key is give references to first table in primary key


.

– First table Primary key and give references to second table


by foreign key

– Syntax:

Create table <table_name>


( col datatype(size) foreign key(col_name)
References <table_name>(col_name));
19 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
Example:
Create table Emp (dept_no number(3) foreign key
(dept_no) references dept(dept_no) );

*All constraint are used by created two tabe*

Sql>create table Dept


( dept_no number(2) primary key,
Dname varchar2(10) not null,
Loc varchar2(15) not null);
Sql>create table Emp
(emp_no number(2) primary key,
ename varchar2(10) not null,
job varchar2(15) not null,
salary number(12,4) check (salary > 0)
dept_no number(3) foreign key
(dept_no) references Dept(dept_no),
Check job in(‘manager’,’cleark’)
On delete cascade);

• Delete :- do not delete record in parent table (use


primary Key) or relational table.

• On delete cascade:- we use this option then delete record


in parent and child table both.

• Delete command is delete only record in table not delete


structure of table

• Drop command is delete whole table with structure.

• Null and not null give only column level and default
constraint is Null
o After creation of table add constraint:
Syntax:
Alter <table_name> add / modify
Constraint <constraint_name>(col_name);

Example:
Alter table Emp add primary key(emp_no);
Alter table Emp modify ename not null;
Alter table Emp add check(salary>1000);
Alter table Emp add unique(phone_no);

20 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
o Function of Sql:

❖ Character function:
1) Upper(string)
– Upper function convert string into upper latter
– Sql> update emp set ename = upper(ename);
– Sql> select upper(my india is great) from dual;
Output: MY INDIA IS GREAT

2) Lower(string)
– Lower function convert string into upper latter
– Sql> select lower(INDIA)from Emp;
Output: india
– Sql>select *from Emp where lower(job)=’clerk’

3) Initcap(string)
– initcap function convert string into proper case or
title case
– Sql> select initcap(my india is great)
From dual;
Output: My India Is Great

4) Ascii(character)
– Ascii function return the ascii value of character
– Sql> select ascii(‘A’) From dual;
Output: 65
5) Char(number)
– Char function return character value of number

– Sql> select char(65) From dual;


Output: A
6) Length(string)
– length function return length of string
– Sql> select length(india) From dual;
Output: 5

7) Ltrim(string)
– ltrim function to space character in string from
left side.
– Sql> select ltrim( ‘cjpatel’,’cjp’) From dual;
Output: patel

21 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
8) Rtrim(string)
– rtrim function to space character in string from right
side.
– Sql> select ltrim( ‘cjpatel’,’patel’) From dual;
Output: cjp
9) Substr(string,starting position,no of character extract)
– substr function to find(extract)characterr in string .
– Sql> select substr( ‘cjpatel’,3) From dual;
Output: patel
– Sql> select substr( ‘cjpatel’,3,3) From dual;
Output: pat
10) Instr(string)
– Instr function return character position.
– Sql> select instr( ‘cjpatel’,’t’) From dual;
Output: 5
11) Replace(string,finding text,replace text)
– replace function to replace character in string .
– Sql> select replace( ‘wife’,’w’,’kn’) From dual;
Output: knife
12) Rpad(string,length,character to pad)
– rpad function to put character in string from right
side.
– Sql> select rpad( ‘cjpatel’,10,’*’) From dual;
Output: cjpatel***

13) Lpad(string,length,character to pad)


– lpad function to put character in string from left
side.
– Sql> select lpad( ‘cjpatel’,10,’*’) From dual;
Output: ***cjpatel

❖ Numeric function:

1) Round(number,rounding place)
– round function return rounded value of number
– Sql> select round(459.3125) from dual;
Output: 459
– Sql> select round(459.3125,2) from dual;
Output: 459.32
2) Mod(number,no. to devide with)
– mod function return module value of number
– Sql> select mod(5,2) from dual;

22 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
Output: 1
3) Sqrt(number)
– sqrt function return the sqrt root number
– Sql> select sqrt(81) from dual;
Output: 9
4) Abs(number)
– abs function return positive value number
– Sql> select abs(-52) from dual;
Output: 52
5) Power(number,paised number)
– abs function return squre value of number
– Sql> select power(5,2) from dual;
Output: 25
6) Trunc(number)
– trunc function return intiger value of number
– Sql> select trunc(52.5) from dual;
Output: 52
7) Ceil(number)
– ceil function return decimal value number to upper
bound.
– Sql> select ceil(140.70) from dual;

Output: 141
8) Floor(number)
– floor function return decimal value number to lower
bound.
– Sql> select floor(140.70) from dual;
Output: 140

❖ Date function:

1) Sysdate
– sysdate function return system date
– default format is “dd-mon-yy”.
– Sql> selec sysdate from dual;
Output: 13-oct-08

2) Add_month(Date,No)
– Add_month function add month in date value.

– Sql> selec Add_month(sysdate,2) from dual;


Output: 13-dec-08
– Sql> selec Add_month(‘14-09-08’,2) from dual;
23 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
Output: 14-11-08
3) Last_day(Date)
– Last_day function return last date of month.
– Sql> selec last_day(sysdate) from dual;
Output: 30-oct-08
– Sql> selec last_day(‘14-09-08’) from dual;
Output: 31-09-08

4) Month_between(Date1,Date2)
– Month_between function return the difference between
two date.
– Sql> selec Month_between (31-09-08,31-11-08) from
dual;
Output: 02

5) Next_day(Date,Day)
– Next_day function return the date value after given
date.
– Sql> selec next_day (sysdate,’mon’) from dual;
Output: 21-october-08
– Sql> selec next_day (15-09-09,’fri’) from dual;
Output: 19-09-08

6) Greatest(Date1,,Date2,Date……)
– Greatest function return the gretest date from given
date.
– Sql> selec greatest(‘15-09-09’,sysdate,’19-oct-08’)
from dual;
Output: 19-oct-08

❖ Conversion Function:

1)To_char(Date,Format)
– To_char function return and convert into character
string
– Sql> selec to_char(sysdate,’dd-mm-yy’) from dual;
Output: 13-10-08
– Sql> selec to_char(sysdate,’ddth-momth-yyyy’) from
dual;
Output: 13th-october-2008
2)To_date(string,’Format’)
– To_date function return and convert string into date
– Sql> selec to_date(’10-12-08’,’dd-mm-yy’) from dual;
24 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
Output: 13-10-08
– Sql> selec to_char(sysdate,’dd-momth-yyyy’) from dual;
Output: 13-october-2008

❖ Group-Aggregate Function:

1)Sum(column name)
– Sum function return the sum value of given column
– Sql> select sum(salary) from Emp;
Output: 45000
– Sql> select sum(amount) from Dept;
Output: 15000.

2)Avg(column name)
– Avg function return the average value of given
column
– Sql> select avg(salary) from Emp;
Output: 5345.50

3)Min(column name)
– min function return the minimum value of given column
– Sql> selec min(salary) from Emp;
Output: 4500
– Sql> selec min(date) from Emp;
Output: 12-09-08

4)Max(column name)
– max function return the maximum value of given column
– Sql> selec max(salary) from Emp;
Output: 8000
5)Count(*)
– Count(*) function count total number of record in
table
– Sql> selec count(*) from Emp;
Output: 8 (total row)

6)Count(colunm)
– Count() function count total number of record in table
without null value
– Sql> selec count(ename) from Emp;
Output: 6
Total row 8 but two null value in ename column

Unit – 4
25 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
ADBMS

❖ Sub-Query:

– Inner Query and Outer Query two parts of Sub_query


– First execute inner query and than after outer query
execute
– Sql>Select *from Emp where salary > (select
avg(salary) Emp ;

– Sql> Select *from Emp where deptno IN (select


Deptno from Dept where city=’visnagar’

Or city=’surat’);

❖ Set Operators:

1) Union all
2) Union
3) Intersect
4) Minus

1) Union all
– Display all row of two query
– Sql> Select * from Emp where salary > 2000
Union all

Select * from Emp where salary > 5000;


- Union all disply all row which come also double times

2) Union
– Display distinct row of two query
– Sql> Select * from Emp where salary > 2000
Union

Select * from Emp where salary > 5000;


- Union do not disply all row which come also double
times.
- Union disply only single time record

26 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
3) Intersect:
– Intersect display same row of two query
– Common record are disply by intersect operator

– Sql> Select * from Emp where salary > 2000


intersect

Select * from Emp where salary > 5000;

4) Minus:
– Minus operator display row after subtraction of two
query
– Rest of record after sub of two query

– Sql> Select * from Emp where salary > 2000


minus

Select * from Emp where salary > 5000;

❖ Join:

1) Self Join
2) Simple Join
3) Outer Join

1) Self Join:
– Self join is used to joint table by self
– Sql> select X.empno ,Y.empno from Emp X,Emp Y;

Here X and Y alias name of Emp table

2) Simple Join:
– Extract record from two table which are relational
teble

– Sql> select X.empno,X.ename ,Y.deptno,Y.salary from


Emp X,Dept Y where X.eno=Y.deptno;

Here X and Y alias name of Emp table

3) Outer Join:
27 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
– Outer join is used to joint two table
– Extract record from two table which are relational
table with + operator
– select empno,ename,dept,Dept.deptno,Dept.salary

Where Dept.deptno(+) = Emp.deptno;

– Display extra record in Emp table do not in Dept


table
– select empno,ename,dept,Dept.deptno,Dept.salary

Where Dept.deptno = Emp.deptno(+);

– Display extra record in Dept table do not in Emp


table

Indexes:
Indexes are special lookup tables that the database search engine can
use to speed up data retrieval. Simply put, an index is a pointer to
data in a table. An index in a database is very similar to an index in
the back of a book.

For example, if you want to reference all pages in a book that


discusses a certain topic, you first refer to the index, which lists
all the topics alphabetically and are then referred to one or more
specific page numbers.
An index helps to speed up SELECT queries and WHERE clauses, but it
slows down data input, with the UPDATE and the INSERT statements.
Indexes can be created or dropped with no effect on the data.
Creating an index involves the CREATE INDEX statement, which allows you
to name the index, to specify the table and which column or columns to
index, and to indicate whether the index is in an ascending or
descending order.
Indexes can also be unique, like the UNIQUE constraint, in that the
index prevents duplicate entries in the column or combination of
columns on which there is an index.

The CREATE INDEX Command

The basic syntax of a CREATE INDEX is as follows.


CREATE INDEX index_name ON table_name;

28 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
Single-Column Indexes
A single-column index is created based on only one table column. The basic
syntax is as follows.

CREATE INDEX index_name


ON table_name (column_name);

Unique Indexes

Unique indexes are used not only for performance, but also for data
integrity. A unique index does not allow any duplicate values to be inserted
into the table. The basic syntax is as follows.

CREATE UNIQUE INDEX index_name


on table_name (column_name);

Composite Indexes

A composite index is an index on two or more columns of a table. Its basic


syntax is as follows.

CREATE INDEX index_name


on table_name (column1, column2);

Whether to create a single-column index or a composite index, take into


consideration the column(s) that you may use very frequently in a query's
WHERE clause as filter conditions.
Should there be only one column used, a single-column index should be the
choice. Should there be two or more columns that are frequently used in
the WHERE clause as filters, the composite index would be the best choice.

VIEW

A view is nothing more than a SQL statement that is stored in the database
with an associated name. A view is actually a composition of a table in
the form of a predefined SQL query.

A view can contain all rows of a table or select rows from a table. A view
can be created from one or many tables which depend on the written SQL
query to create a view.

29 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
Views, which are a type of virtual tables, allow users to do the following.

• Structure data in a way that users or classes of users find natural


or intuitive.
• Restrict access to the data in such a way that a user can see and
(sometimes) modify exactly what they need and no more.
• Summarize data from various tables which can be used to generate
reports.

Creating Views

Database views are created using the CREATE VIEW statement. Views
can be created from a single table, multiple tables or another view.
To create a view, a user must have the appropriate system privilege
according to the specific implementation.

The basic CREATE VIEW syntax is as follows –

CREATE VIEW view_name AS


SELECT column1, column2.....
FROM table_name
WHERE [condition];

You can include multiple tables in your SELECT statement in a


similar way as you use them in a normal SQL SELECT query.

Example:-
Consider the CUSTOMERS table having the following records –
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Pina | 32 | Ahmedabad | 2000.00 |
| 2 | Jinal | 25 | Delhi | 1500.00 |
| 3 | Mitali | 23 | Kota | 2000.00 |
| 4 | Dolly | 25 | Mumbai | 6500.00 |
| 5 | Hashvi | 27 | Bhopal | 8500.00 |
| 6 | Pinkal | 22 | MP | 4500.00 |
| 7 | Rippal | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

30 | P a g e
ADVANCED DATABASE MANAGEMENT SYSTEM
Following is an example to create a view from the CUSTOMERS table.
This view would be used to have customer name and age from the
CUSTOMERS table.

SQL > CREATE VIEW CUSTOMERS_VIEW AS


SELECT name, age
FROM CUSTOMERS;

Now, you can query CUSTOMERS_VIEW in a similar way as you query an


actual table. Following is an example for the same.

SQL > SELECT * FROM CUSTOMERS_VIEW;

This would produce the following result.


+----------+-----+
| name | age |
+----------+-----+
| Pina | 32 |
| Jinal | 25 |
| Mitali | 23 |
| Dolly | 25 |
| Hrshvi | 27 |
| Pinkal | 22 |
| Rippal | 24 |
+----------+-----+

Two types of View


o Read-only view

Create view v1 as select *from student with read only;

o Updatable view

Create view v1 as select *from student;

31 | P a g e

You might also like