Professional Documents
Culture Documents
Jay
SQL
What is data actually?
Data:
It is stored representation of OBJECTS
and EVENTS that have meaning and importance in users environment.
Data can be structure or unstructured.
o StrucuredEmpName,address.
o UnstructuredEmpPhoto,AddrMap.
Representation of data
KING 10 5000 15-02-08
JONES 10 4500 02-03-06
BLAKE 20 3500 15-06-05
SCOTT 20 2000 12-05-02
FORD 30 1100 13-04-00
MILLER 10 1200 14-07-99
Representation of Information:
Employees Information
Organization: Date: 21-Aug 2012
Durga Soft Place:Hyderabad
Name Department Salary DOJ
KING 10 5000 15-02-08
JONES 10 4500 02-03-06
BLAKE 20 3500 15-06-05
SCOTT 20 2000 12-05-02
FORD 30 1100 13-04-00
MILLER 10 1200 14-07-99
1 Oracle 11g/12c
Length of size
Values allowed
Source of data
Ownership
Metadata and data are always separate.
Metadata enables the database designers and programmers to understand exactly in
which form the data should exist within
the system.
Database is a platform where we can place the data fro the future
references. Database is a collection of interrelated data, i.e. database always
stores data along with its relationships.
2 Oracle 11g/12c
Database Management system evolution
First time introduced during 1960s
Relational model first defined by E.F codd(IBM) in 1970.
Objectives behind Evolution
Reduce the maintenance cost of software.
Manage complex data types.
Provider easier and faster access to data even for novice user.
Different database Models
Flat file DBMS
Hierarchical DBMS
Network DBMS
Relational DBMS
Object-Relational DBMS
Logical Structures
In design state the system is represented in the form of Entity relationship model.
In the database state it is representation of the actual metadata of the database
management system software which will be in the form of tablespaces , data
dictionary objects etc.
Physical structures
In this the system is represented in the form of tables, indexes etc.
Database should have the ability to provide access to external tables for files outside
the database, as if the rows in the files were rows in the table.
Creating structures (Tables & Indexes)
With in the oracle database, the basic structure is a table used to store data.
3 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Table space:
It is logical storage unit within oracle database and is not visible in the file system of the
machine on which the database resides.
The table space builds the bridge between the oracle database and the file system in
which the tables or index data is stored.
A data file can be part of one and only one table space.
Data models:
Data models helps in exploring ideas and improve the understanding the database
design for both developers
and system designers.
Communicate
Describe
Investigate
Analyze
Categories
4 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Database Models:
The main object of the database is store the inter related data and
maintain the data. The very basic elementary piece of data is called as data
item.we assume that item cannot be subdivided into smaller data types and
5 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
at the same time retain any meaning to the users of the data. The
relationship among the data items, which shows how they are related, is
called as Data model.
The database models are
The File Management System was the first method used to store data
in a computerized database. Each data item is stored on disk sequentially in
one large file. In order to locate one particular item the search starts at the
beginning and each item is checked subsequently till the match is found.A
particular relationship cannot be drawn between the items other than the
sequence in which it is stored.
Drawbacks:
A particular record can not be located quickly. If the data has be stored, the
whole file has to be read and rewritten in the new order.
It will not support data types.
It will not support to store or allocate memory dynamically.
Data con not be shared with concurrent users.
Data duplication.
Very poor security: Operating system provide only a password mechanism
for security. This is not sufficiently flexible to enforce security polices in
which different users have permission to access different subsets of the data.
Retrieving data from a file is not faster: We have to write special
programs to answer each question a user may want to ask about the data.
These programs are likely to be complex because of the large volume of
data to be searched.
Hierarchical Database System(HDS):
DBMS
Fig 1.0
Jim Clerk 3000 Jack Mgr 6000
6 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Disadvantages:
It is not possible to enter a new level into the system. As and when
such a need arises the entire structure has to be revamped. Another
disadvantage is that this model does not support Many-to-Many
relationship. In case this sort of relation is required then multiple copies of
the same data have to made which result in redundancy. To overcome this
drawback, the Network Database Model was introduced.
Note: The terminal points shows, for example below ID.1.n.indicate that
similar data are present at that point.
The main idea behind the NDS model is to bring about Many-to_Many
relationship. The relationship between the different data items is called
as sets. This system also uses a pointer to locate a particular record(i.c
called as Physical Links)
Disadvantages:
The use of pointers leads to complexity in the structure. As a result of
the increased complexity mapping of related data become very
difficult.
Relational Mode(RDBMS):-
The Model was first outlined by E.F Codd 1970.
The Components of Relation Model are:
o Collection of objects or relations that stores the data.
o A set of operations that can act on the relations to produce
other relations.
Data integrity for accuracy and consistency.
7 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
ER-MODEL Benefits:
8 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
One-to-One:
9 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Fig 1.2
Many-to-One:
One student can register for only one particular course at a time, where a
number of students could register for the same course.
This is illustrated with Fig1.3
Student 1
Student 3
Fig 1.3
Many-to Many:
A vendor can sell a number of items and many vendors can sell particular
item. This can be understand from Fig 1.4 given below.
Vendor 1 Item 1
Vendor 2 Item 2
Fig 1.4
Row or Tuple :
Column Or Attribute:
10 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
A Field:
It can be found at the Intersection of row and a column.
A field can have only one value, or may not have a value at all, the
absence of value in Oracle is represented as NULL.
Each table contains data that describes exactly only one entity.
Data about different entities is stored in different tables.
RDBMS enables the data in one table to be related to another table by
using the Foreign keys.
A Foreign Key is a column or a set of Column that refer to a Primary
Key in the same table or another table.
Should not specify the access route to the tables, and should not
reveal the physical arrange.
The Database is accessed using Structured Query Language(SQL)
The language is a collection of set operators.
Components of SQL:
11 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
SELECT
Grants(give) Revoke(cancel)
12 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Oracle Architecture:
et
User
Processes Listener Dnnn
Multithreaded
Server
Snpn
PMON
Private
Redo Log buffer
Buffer Cache
RECO
To distributed datLaCbKa
N se noLd
GeWs
R CKPT DBWR
SMON
Archive
Oracle Process
Destination
(Background process)
Redo
ARCH Logs
Data1.dbf
Data2.dbf
Data3.dbf
init.ora
Control File
13 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
o SQL commands
o PL/SQL block
o It has own commands to set environment as
requirement.
o To generate report.
o To save SQL command as file(.sql)
o To save PL/SQL command as file(.sql)
o To save output as file(.lst)
Database Objects:
A Oracle database can contain multiple data structures.
The different Database objects in Oracle are:
Table in Oracle:
Table can be crated at any time, even when the users are using the
database.
14 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Table Restriction:
COLUMN NAME:
The column name should begin with a letter and can be 1-30
characters long.
Column name is Unique in a table.
Column name can be renamed.
Maximum columns in table is 1000.
15 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
They are less restrictive than other data types an have very few
properties.
They data is stored in strings with byte values
The different character data types are:
VARCHAR2 NVARCHAR2
The Maximum size allowed is 2000 Bytes and size has to be specified.
If the data short than the actual size then the blank pads are applied.
16 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Number(Precision, Scale)
Number data type stores the numeric data, the precision is the total no
of digits required and scale stands for the rounding of decimal place.
Range of precision is from 1 to 38.
Integer data type will converted as Number Data type with the maxim
size 38 digit as precision .
LONG:
17 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
*Minute *Second
Syntax: TIMESTAMP(Fractional-Seconds-Precision)
RAW:
LONG RAW:
It stores the binary data similar to RAW but can store more
bytes than RAW.
Maximum length is 2GB.
No size is required.
18 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
It enables access to binary file LOBs which are stored in the systems
outside ORACLE.
A BFILE column or the attributes stores the BFILE locator.
The BFILE locator maintains the directory alias and the filename.
The Binary File LOBs do not participate in transaction and are not
recoverable.
The maximum size is 4 GB.
Codds Rules:-
19 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
20 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
The database user need not known about the physical Implementation
of data storage and retrieval.
The data base users view of a data should not be effected by any
change in the database tables.(Materialized view).
To Create Table:
Syntax:.
Example:
(
Empno Number(4), Ename Varchar2(15),
21 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Conventional INSERT.
Direct-Path INSERT.
22 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Syntax:
In this case the values should be provided to all the columns that
exists inside the table.
The order of values declared in the Values clause should follow the
original order of the columns in the table.
The Character and Date type data should be declared in single Quotes.
Number information can be applied normally.
In this case the Order of columns declared in insert need not be the
same as that of the original table order.
The data values in the values caluse should match with that of INSERT
list.
The columns not supplied with data are filled with NULL values, Until
the NOT NULL constraint is declared.
23 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
SYSDATE FUNCTION
SYSDATE is Pseudo-column.
This function return the current date& time.
USER FUNCTION:
This function returns the user name of the user who has logged
in.
24 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Example:
Example:
Substitution Variables:
25 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
The List file is used to save all information which performed in SQL
*Plus .
Example:
Sql>SAVE E:\nitdir\ins
Sql> SAVE E:\nitdir\ins REPLACE
To Get Script:
26 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Sql>Get E:\nitdir\ins
To Run Script:
Run is used to display the script and run the file.
Sql>@ E:\nitdir\ins
Sql>ed E:\nitdir\ins
Select used to retrieve data from one or more than table ,view,
object tables.
It is for read only purpose.
The select is the most frequently used command as access to
information is needed all the time.
PREREQUISTIES:
PROJECTION:
o It chooses the columns in a table that are expected to
return by a query.
JOIN :
27 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Syntax:
1)DEPT
Column Name Data Type
------------------------------- ------ ------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
2)EMP
28 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
3)SALGRADE
Dept:
14 rows selected.
29 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
--------- -------- ------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
30 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
ARITHMETIC OPERATORS:
Operator Precedence:
31 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
General Function:
This functions work with any data type and pertain to using null
value.
NVL Function:
Syntax:
NVL(Expr1,Expr2)
Expr1: is the source value or expression that may contain NULL.
Expr2: is the target value for converting NULL.
o NVL(Number_column, 0)
o NVL(date_column, 01-JAN-09)
o NVL(Character_column, Unavailable)
Note : If Expr1 is Character data then Expr2 may any Data type.
Sql>Select NVL(100,200) from dual;
Sql> Select NVL(null,200) from dual;
Sql> Select ename,sal,comm,sal+NVL(comm,0) from emp;
Sql> Select ename,sal,comm,(sal*12)+NVL(comm,0) from emp;
Sql> Select ename,sal,comm,(sal+500)+NVL(comm,0) from emp;
32 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
NVL2 Function:
Syntax:
NVL2(expr1,expr2,expr3)
o If expr1 is not null,NVL2 returns expr2.if expr1 is null, NVL2
returns expr3.
o Expr1 may any data type.
o The data type of the return value is always the same as the data
type of expr2 , unless Expr2 is character data .
Example:-
1) select nvl2(comm,0,1000) from emp;
2) select sal,comm, sal+nvl2(comm,100,1000) from emp;
>select ename,sal,comm, nvl2(comm,'sal+comm','sal') income
from emp where deptno in(10,30);
NULLIF Function:
Syntax:
NULLIF(expr1,expr2)
o Compares two expressions and returns null if they are equal,or
the first if there are not equal.
Example:
>SELECT NULLIF(100,200) from dual;
COALESCE:
33 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
OCP Questions :
You want to display the name and annual salary multiplied by the
commission_pct for all employees. For records that have a NULL
commission_pct, a zero must be displayed against the calculated column.
34 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
LITERALS IN ORACLE:
A Literal and Constant value are synonyms to one another and refer
to a fixed data value.
The types of Literals recognized by Oracle are
o Text Literals
o Number Literals
o Interval Literals
Text Literals:
It specifies a text or character literal.
It is used to specify values whenever text or CHAR appear in
35 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Example:
Employee Information
'Manager's Specification'
36 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Duplicate Rows:
Filter Of Record:
37 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
marks.
Character values are case sensate and Date values are format
sensitive
(DD-MON-YY)
The Comparison operator are used in conditions that compare one
expression to another.
**< >,^=,!=**>**>=**<**<=**=
Example:
Logical Operators:
A logical condition combines the result of two component conditions to
produce a single result.
Three logical operators are available in Oracle.
AND OR NOT
AND Operator:
Truth Table:
AND
38 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Example:
OR Operator:
Truth Table:
OR
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
NOT Operator:
39 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
40 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Rules of Precedence:
Example:
41 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
42 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
The Wild Card symbols can be used in any combination with literal
character.
For finding exact match for % and - the ESCAPE option has to be
used, which is / symbol with ESCAPE option.
Sql> Select *From Dept Where Dname LIKE ' \_%' ESCAPE '\';
(update dept set dname='SO_FT_WARE' where deptno=50;)
43 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
ORDER BY Clause:
The Order of rows returned in a query result is undefined.
The ORDER BY clause can be used to sort the rows.
The ORDER BY clause must be the last clause of the SQL statement.
An expression, or an alias, or column position as the sort condition.
Default ordering of Data is Ascending.
**Number 1-999
**Dates Earlist-Latest
**String A-Z;NULLSLast.
Syntax:
SELECT expr FROM table [WHERE condition(s) ]
[ORDER BY {column, expr} [ASC|DESC]];
44 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
**SELECT List
**WHERE List
**Start With Clause
**CONNECT BY Clause
These function manipulate group of rows to give one result per group of
rows.
Func_Name(Column/Expr,[Arg1,Arg2,])
45 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Can be nested.
Character Functions:
LOWER(column|expression)
46 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Sql> Select 'The '||Ename || '''s Designation is '|| Job From Emp
Where LOWER(Job)='manager'
Upper Function:
Syntax:
UPPER(Column | Expression)
47 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
INITCAP Function:
It returns a string with the first letter of each word in upper case,
keeping all othered
letters in Lower case.
CONCAT Function:
It Concatenates the first characters value to the second character
value. Only two parameters accept.
It return the character data type.
48 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
Points to Remember
If m is 0, it is treated as 1.
If m is positive, Oracle counts from the beginning of char to
find the first character.
If n is Omitted, Oracle returns all characters to the end of
char.
If n is less than 1 or 0, A null is returned.
Floating points numbers passed as arguments to substr are
automatically converted to Integers.
Syntax: SUBSTR(Col/Exp,m,[n])
49 Oracle 11g/12c
By:Mr.Krishna Reddy DURGASOFT
LENGTH Function:
Syntax:
LENGTH(Column|Expression)
INSTRING Function:
50 Oracle 11g/12c