2. DATA : Collection of Information
DATABASE : Collection of interrelated data in
contiguous locations in the form of table.
DBMS : is a set of program that enable users to
create and maintain a database.
RDBMS : It supports largest of databases .It also
provide access to many concurrent users.
3. DataBase Management System
(DBMS)
Database system consists of two parts :
DBMS : is a set of program that organizes
and maintains the information.
Database Application : the program that
allows us to view, retrieve and update
information stored in the DBMS.
4. Data Structure
Name Roll_no Address
Anu CA101 Madurai
Hari CA114 Chennai
Table (ie) contain rows & column
(eg) : students_details -------- Entity
---Attributes
5. Table Properties
Formal Relational
Tuple
Cardinality
Attribute
Degree
Primary Key
Informal Equivalents
Row, Record
No. of rows
Column, Field
No. of Column
Unique identifier
7. Relational DataBase Management
System
(RDBMS)
The DBMS whose designed is based on
relational theory in mathematics is called
RDBMS.
RDBMS packages are Oracle, Sybase and
Informix etc.
It supports largest of databases .It also
provide access to many concurrent users.
8. Difference between DBMS &
RDBMS
DBMS: It allows only one person to access
the database at a given time.
RDBMS: It allows many users
simultaneously to access the database.
9. ORACLE
Oracle is an Object Relational Database
Management system (ORDBMS)
Oracle database offers capabilities of both
relational and object oriented database systems.
Oracle products are based on a concept called
Client/Server Technology
10. Tools of Oracle
SQL * Plus : Structured Query Language, we can
store, retrieve, edit and run SQL commands.
PL/SQL : PL/SQL block can contain any number
of SQL statement integrated with flow of control
statement.
Forms : For generating and executing forms
based applications.
Reports : For displaying and printing reports.
11. Structured Query Language
(SQL)
Oracle’s database language is SQL which is
used for storing and retrieving information.
A table is a primary database object of SQL
that is used to store data.
SQL supports the following commands.
DDL, DML,TCL
13. Character Datatype
Char(n)
1. fixed length character
string,
2. it store alphanumeric
values,
3. size is between 1-255
bytes.
(eg) name char(25)
varchar(n)
1. variable length character
string,
2. it store alphanumeric
values
3. size is between 1-2000
bytes.
(eg) roll_no varchar2(6)
Back
14. Number Datatype
we can store +ve, -ve, zero, fixed point,
floating point.
Syntax :
Column_name number { p=38, s=0}
Column_name number(p)
Column_name number(p,s)
(eg) age number(2)
price number(5,2)
Back
15. Date Datatype
It is use to store date and time.
It has fixed length of 7 bytes
Default date datatype is “dd-mon-yy”
To view the system date sysdate()
(eg) dt_adm date
Back
16. Raw & Long raw datatype
It use to store byte oriented data like binary
data or byte string. (graphic images and
digitized sound)
The maximum size of datatype is 255 bytes.
Only storage and retrieval of data are possible,
manipulation of data cannot be done.
The maximum size of long raw datatype is
2GB.
Back
17. Data Definition Language
(DDL)
1. Create – to create new table
Syntax :
Sql> create table <table_name>
(column definition1,
column definition2……….);
(eg)
Sql> create table stud_det
(name char(10), roll_no varchar2(6),
age number(2));
18. Data Definition Language
(DDL)
2. Alter – to modify the existing (or) add new field.
Syntax :
Sql> alter table <table_name> modify or add
(column definition1, …….);
(eg)
Sql> alter table stud_det modify (name char(20));
Sql> alter table stud_det add (address varchar2(20));
19. Data Definition Language
(DDL)
3. Drop – to drop the table structure.
Syntax :
Sql> drop table <table_name> ;
(eg)
Sql> drop table stud_det ;
20. Data Definition Language
(DDL)
4. Truncate – values in the table are deleted
Syntax :
Sql> truncate table <table_name> ;
(eg)
Sql> truncate table stud_det ;
21. Data Manipulation Language
(DML)
1. insert – to add one or more rows to a table.
Syntax :
Sql> insert into <table_name> values
(list of data);
(eg)
Sql> insert into stud_det values
(‘Arul’,’08CA01’,20,’madurai’);
22. Data Manipulation Language
(DML)
To insert more than one record to a table.
(eg)
Sql> insert into stud_det values
(‘&name’,’&roll_no’,&age,’&address’);
Sql>/
Sql> enter the name:
enter the rollno:
enter the age:
23. Data Manipulation Language
(DML)
2. Select – query is a request for information.
Syntax : To view the whole table.
Sql> select * from <table_name>;
Syntax : To view the particular column.
Sql> select <column_name> from <table_name>;
Syntax : To view only the particular row.
Sql> select <column_name> from <table_name> where
<column_name>=‘values’;
24. Data Manipulation Language
(DML)
Syntax : To view Selecting distinct row .
Sql> select distinct<column_name> from
<table_name>;
To order the column name in ascending order.
Sql>select name from stud_det where course=‘MCA’
order by name;
To order the column name in descending order.
Sql>select * from stud_det order by name,roll_no desc;
25. Data Manipulation Language
(DML)
3. update – to alter the column values in a table.
Syntax :
Sql> update <table_name> set
<column_name>=‘value’ where condition;
(eg)
Sql> update item set price=100.50 where prod_id=101;
Sql> update item set price=100.50 where prod_id
in(select prod_id from product);
26. Data Manipulation Language
(DML)
4. delete – to delete particular rows.
Syntax :
Sql> delete from <table_name> where
condition;
(eg)
Sql> delete from stud_det where
roll_no=’08CA01’;
29. Transaction Control Language
(TCL)
3. savepoint – it is used to identify a point in a transaction to
which we can later rollback
Syntax :
Sql> savepoint save_pt;
(eg)
Sql> update order_info set total=7000 where id=101;
Sql>savepoint sp1;
Sql> update order_info set total=7200 where id=102;
Sql>savepoint sp2;
Sql>rollback to savepoint sp1;
30. Data Control Language (DCL)
1. Grant – the user wants to share an object with others, the
appropriate privilege can be granted on that particular
object
Syntax :
Sql> grant privileges on<object_name> to <username>;
Object_name – table_name
Privileges – insert, select, update, delete
(eg)
Sql> grant select, insert on customer to accounts;
31. Data Control Language (DCL)
2. Revoke –to withdraw the privileges which has
been granted to a user.
Syntax :
Sql> revoke privileges on<object_name> from
<username>;
(eg)
Sql> revoke select, insert on customer from
accounts;
32. Integrity Constraints
A mechanism used by oracle to prevent
invalid data entry into the table.
Domain integrity constraints.
Entity integrity constraints.
Referential integrity constraints.
34. Entity Integrity Constraints.
‘unique’ constraint
To prevent the duplicate values of a specified
column.
This constraint can also allow null values.
(eg)
Sql> create table price (prod_id number(6)
constraint prod_cons unique, stdprice
number(6,2), minprice number(8,2));
35. Entity Integrity Constraints.
‘primary key’ constraint
To avoid the duplicate values of a specified
column.
This constraint does not allow null values.
(eg)
Sql> create table price (prod_id number(6)
constraint prod_cons primary key,
stdprice number(6,2), minprice
number(8,2));
36. Referential Integrity Constraint
It establishes relationship between the two table
have the same column.
Column in the parent table as primary key and
same column in the child table as foreign key.
(eg) create table product (prod_id number(6)
constraint prd_fk references price (prod_id),
prod_name varchar2(20));
37. On delete cascade clause
The rows under the referenced key column in
a parent table are deleted, then all rows in
the child table with dependent foreign key
column will also be deleted automatically.
(eg) create table product (prod_id number(6)
constraint prd_fk references price
(prod_id)on delete cascade, prod_name
varchar2(20));
39. Operators in SQL
1. Arithmetic operator:
To perform calculation based on values.
+, -, *, and /
(eg)
Sql> select prodid, stdprice, minprice,
stdprice + minprice from price where prodate=‘4-
june-07’;
Sql> select ordid, itemid, 100*(actualprice+qty) from
item where prodid=111;
40. Operators in SQL
2. Comparison operator
To compare one expression with another.
= , != , < , > , <= , >= , between, not,
in (to match with any value in the list),
like (to match a character pattern) and
is null (to check whether it is null).
41. Operators in SQL
2. Comparison operator
(eg)
Sql> select * from order_info where total>7000;
Sql> select * from order_info where not (custid=2
or custid=4);
Sql> select * from order_info where shipdate
in(’22-jan-07’,’03-feb-07’);
42. Operators in SQL
2. Comparison operator
LIKE operator which is used to search a character
pattern. The LIKE operator recognizes special
character like % and _
(eg)
Sql> select name, address, city from customer where
name like ‘v%’;
Sql> select * from customer where name like ‘v_e’;
43. Operators in SQL
3. Logical operator
To Combine the result of two conditions to
produce a single result.
AND , NOT and OR
(eg)
Sql> Select * from order_info where shipdate=
‘4-jan-07’ and total<9000;
44. SQL * Plus Functions
Single Row Function
1. Date function
2. Numeric function
3. Character function
4. Conversion function
5. Miscellaneous function
Group Function.
Count Function
Group by Clause
Having Clause
45. Date Function
i) add_months :
Format – add_months(d,n)
d – date, n – no. of months
(eg)
Sql> select doadm,add_months(doadm,2) from student;
o/p -> 10-oct-00
15-nov-00
do_adm
10-aug-00
15-sep-00
46. Date Function
ii) last_day : It return the day of corresponding to the last day of
the month.
Format – last_day(d)
(eg)
Sql> select last_day(doadm) from student;
o/p -> 31-aug-00
31-aug-00
doadm
10-aug-00
15-sep-00
47. Date Function
iii) months_between : It is used to find the number of months
between two dates
Format – months_between(d1,d2)
d1 & d2 – date
(eg)
Sql> select months_between(dojoin,doadm) from student;
o/p -> 1.35
4.83
do_adm dojoin
25-sep-00 05-nov-00
15-may-00 10-oct-00
48. Date Function
iv) next_day :
Format – next_day(d,day)
d – date, day – any weekday
Sql> select next_day(doadm, ‘Friday’) from student;
v) Round : the date is round to the nearest date
format – (d [fmt])
d – date, fmt – year (or) month (or) day
Sql> select round(doadm, ‘year’) from student where
name=‘kumar’;
49. Character Function
Function Input Output
Initcap(char) Select initcap(‘hello’) from dual; Hello
Lower(char) Select lower(‘FUN’) from dual; Fun
Upper(char) Select upper(‘sun’) from dual; SUN
Ltrim(char,set) Select ltrim(‘XYZadams’,’XYZ’)
from dual;
Adams
Rtrim(char,set) Select rtrim(‘XYZadams’,’ams’)
from dual;
XYZad
51. Conversion Function
i) To_char()
It convert date to a value of varchar2 datatype.
format: to_char(d, [fmt] )
(eg) select to_char(sysdate,’dd “of” fmmonth yyyy’) from
dual;
o/p -> 15th of October 2007
i) To_date()
(eg) select to_date(‘january 15 2006,’month-dd-yyyy) from
dual;
o/p -> 15-jan-06
52. Group Function
A Group function return a result based on group
of rows.
1. Avg : (eg) select avg(total) from table_name;
2. Max : (eg) select max(cutid) from table_name;
3. Min : (eg) select min(ordid) from table_name;
4. Sum : (eg) select sum(total) from table_name;
53. Count Function
Count(*): count all the rows, including the duplicate
and null.
Count (column_name): count the no.of values
present in the column without including null.
Count (distinct col_name): similar to
count(col_name) but eliminates duplicate value
while counting.
54. Count Function
(eg) itemfile
Sql> select count(*) from itemfile;
Sql> select count(Q_hand) from itemfile;
Sql> select count(distinct P_cat) from itemfile;
item P_cat Q_hand It_rate
I201 Spares 40 100.50
I202 Spares 30 200
I203 Spares 30 500
1204 accession 1000
55. Group by Clause
Simultaneous usage of column name and
group function .
(eg)
Sql> select P_cat from customer group by
P_cat;
56. Having Clause
It is used to specify certain condition on rows,
retrieved by using group by clause.
(eg)
Sql> select P_cat, max(It_rate) from itemfile
group by P_cat having P_cat not in
(‘accession’);
57. Set Operators and Joins
Set operator combine the result of two queries into a
single one.
Union.
Union all.
Intersect.
Minus.
58. Set Operators and Joins
Student1 student2
Name R_no
X 1
Y 2
Z 3
W 4
Name R_no
Y 2
Z 3
W 4
A 5
59. Set Operators and Joins
Union : It return all distinct rows and selected by both
queries.
(eg)
Sql> select r_no from student1 union select r_no from
student2;
Output:
r_no
1
2
3
4
5
60. Set Operators and Joins
Union all : It return all rows selected by either query include
duplicate.
(eg)
Sql> select r_no from student1 union all select r_no from student2;
Output:
r_no
1
2
3
4
2
3
4
5
61. Set Operators and Joins
Intersect : It return only rows that are common on both the
queries.
(eg)
Sql> select r_no from student1 intersect select r_no from
student2;
Output:
r_no
2
3
4
62. Set Operators and Joins
Minus : It return all distinct rows selected only by
the first query and not by the second.
(eg)
Sql> select r_no from student1 minus select r_no from
student2;
Output:
r_no
1
63. Relating Data through Join
Concept.
The purpose of a join is to combine the data spread
across tables. The join is actually perform by the
‘where’ clause which combine the specified rows
of table.
Syntax:
Sql> select column from table1, table2 where
logical expression;
64. Relating Data through Join
Concept.
Simple Join:
equi-join
non equi-join
Self Join
Outer Join
65. Simple Join
equi-join : it is based on equalities
(eg) sql> select * from item,ord_info where
item.ordid = order_info.ordid;
non equi-join : it specifies the relationship
between columns belonging to different table by
making use of relational operators.
(eg) sql> select * from customer, order_info where
customer.cust_id > order_info.cust_id and
customer.repid =10;
66. Self Join
Joining of a table to itself is known as a self
join (ie) it joins one row in a table to another.
(eg) sql> select a.itemcode, a.itemdesc,
a.qty_hand, b.max_level from itemfile a,
itemfile b where a.qty_hand < b.max_level and
a.p_cat = ‘spares’ and a.itemcode=b.itemcode;
67. Outer Join
It extends the result of a simple join. An
outerjoin return all the rows returned by simple
join as well as those rows from one table that do
not match any row from the other table. The
symbol(+) represent outerjoin.
68. Table aliases
To prevent ambiguity in a query we include
table name in the select statement. It is use to
make multiple table queries shorter and more
readable.
(eg)
Sql> select c.*, d.itemcode, d.qty_ord from
ord_mast c, order_det d where c.order_no =
d.order_no;
69. Sub Queries
Nesting of queries, one within the
other is termed as a subquery.
It is used to retrieve data from tables
which depend on the value in the
table.
70. Sub Queries
(eg) student
name Roll_no course doadm dojoin
A 951 MCA 15-aug-07 20-sep-07
B 933 MCA 01-sep-07 25-sep-07
C 937 MCA 25-sep-07 05-nov-07
D 940 MCA 02-jul-07 07-aug-07
Back
71. Sub Queries
(eg) student1
name Roll_no course doadm dojoin
C 937 MCA 25-sep-07 05-nov-07
E 901 MCA 01-oct-07 21-sep-07
F 950 MCA 15-nov-07 30-nov-07
D 940 MCA 02-jul-07 07-aug-07
Back
72. Sub Queries
Sql> select * from student where Roll_no = (select
Roll_no from student1 where doadm=’02-jul-07’
and course=‘MCA’);
Output:
name Roll_no course doadm dojoin
D 940 MCA 02-jul-07 07-aug-07
73. Sub Queries
Subqueries can also return more than one value.
We should include operators like any, all, in
(or) not in between the comparison operator and
the subquery.
Note:
‘=any’ ----- in
‘!=all’ ----- not in
76. Sub Queries
(eg) for usage of any operator
Sql> select * from student1 where mark1 < any (select mark1
from student where mark1 between 65 and 80);
Output : for inner query Mark1
76
70
Output :
name Roll_no mark1 mark2
G 941 63 35
H 948 61 60
77. Sub Queries
(eg) for usage of all operator
Sql> select * from student1 where mark1 > all (select mark1
from student where mark1 <60);
Output : for inner query Mark1
59
46
Output :
name Roll_no mark1 mark2
E 931 78 76
C 935 76 45
G 941 63 35
H 948 61 60
78. Multiple Subqueries
A subquery itself can contain a subquery.
(eg)
Sql> select * from ven_mast where vencode =
(select vencode from ord_mast where
orderno = (select orderno from ord_details
where qty_ord=100 and itemcode=‘i203’));