SQL Plus: Topics Covered in This Unit
SQL Plus: Topics Covered in This Unit
P1 10 10
P4 3 3
P6 7 7
P2 4 4
P5 10 10
P3 2 2
P1 6 6
P6 4 4
P4 1 1
P6 8 8
SELECT P-NO, SUM (QTY_ORDERD) TOTAL QTY FROM
SALES_DETAIL GROUP BY P_NO;
OUTPUT: -
P_NO TOTAL QTY
P1 16
P2 4
P3 2
P4 4
P5 10
P6 19
HAVING CLAUSE:
The HAVING clause can be used in combination with the GROUP BY
clause. HAVING imposes a condition on the GROUP BY clause, which
further filters the groups created by GROUP BY clause.
SELECT P-NO, SUM (QTY_ORDERD) TOTAL QTY FROM
SALES_DETAIL GROUP BY P_NO HAVING P_NO=P1 OR P_NO=P4;
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 15 of 54
OUTPUT: -
P_NO TOTAL QTY
..
P1 16
P4 4
() INTEGRITY CONSTRAINTS:
INTIGRITY DATA CONSTRAINTS: -
1. Some of the restriction has to be applying on the database to ensure the
validity and integrity of database.
2. Rule, which are enforced on the data to be entered and prevents the user
from entering invalid data into the table. This mechanism is handled by
the constraints.
3. ORACLE checks the data using entered into the table columns against the
data constraints. If it passes the check than data is entered into the table
else data is rejected.
4. If a single column fails against a constraint entire record is rejected and
not stored into the table.
5. There are mainly two types of data constraints are there.
CATEGARIES OF CONSTRAINTS: -
DOMAIN CONSTRAINT (Associate with COLUMN)
NOT NULL
CHECK
DEFAULT
ENTITY CONSTRAINT (Associate with ROW)
PRIMARY KEY
UNIQUE
REFERENTIAL INTEGRITY CONSTRAINT (Link between two table)
FOREIGN KEY
INPUT OUTPUT CONSTRAINTS:
It determines the speed at which the data can be inserted or accessed from
oracle table.
It is dividing into two categories.
Primary Key Constraints: -
It is attached to one column or more than one column of the table. The
constraints that apply to the columns are unique and not null.
PRIMARY KEY = UNIQUE + NOT NULL.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 16 of 54
Foreign Key Constraints: -
It creates the relationship between the master table and detail table and
it ensures.
Records cant be inserted into the detail table, if corresponding record
in the master table doesnt exist.
Record of the master table cant be deleted, if corresponding record in
the detail table exist.
1. PRIMARY KEY: -
1. The Primary key is one or more column in a table use to uniquely identify
each row in the table.
2. The constraints that apply to the columns are unique and not null.
3. A single column primary key is called Simple Primary Key.
4. A multi column primary key is called Composite Primary Key.
5. Only when a record cant be uniquely identify the value in a single column
than composite primary key is required.
6. PRIMARY KEY = UNIQUE + NOT NULL.
Primary key constraints at column level: -
Syntax: -
COLUMN-NAME DATA-TYPE (SIZE) PRIMARY KEY
Example:
CREATE TABLE EMP (E_NO NUMBER (3) PRIMARY KEY,
E_NAME VARCHAR2 (15));
Primary key constraints at table level: -
Syntax: -
PRIMARY KEY (COL1, COL2, .., COLN);
Example:
CREATE TABLE EMP (E_NO NUMBER (3),
E_NAME VARCHAR2 (15), PRIMARY KEY (E_NO));
To ADD constraints when the table is already there: -
Syntax: -
ALTER TABLE <TABLE-NAME> ADD PRIMARY KEY (COL-NAME)
Example: -
ALTER TABLE EMP ADD PRIMARY KEY (E_NO);
To DROP a constraints: -
Syntax: -
ALTER TABLE <TABLE-NAME> DROP <CONSTRAINT-NAME>;
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 17 of 54
Example: -
ALTER TABLE EMP DROP PRIMARY KEY;
To give the constraints name on the Primary key: -
Syntax: -
COLUMN-NAME DATA-TYPE (SIZE)
CONSTRAINT CONS-NAME PRIMARY KEY
Example: -
CREATE TABLE EMP (E_NO NUMBER (3) CONSTRAINT P_KEY
PRIMARY KEY, E_NAME VARCHAR2 (15));
2. FOREIGN KEY: -
1. A Foreign key is a column or group of columns whose values are derive
from the primary key or unique key of some other table.
2. The table in which the foreign key is defined is called foreign key table or
Detail table.
3. The table which define primary key is called Primary key table or Master
table.
4. It creates the relationship between the Master table and Detail table.
5. It ensures:
Records cant be inserted into the detail table, if corresponding
record in the master table doesnt exist.
Record of the master table cant be deleted, if corresponding
record in the detail table exist.
Foreign key constraints at column level: -
Syntax: -
COLUMN-NAME DATA-TYPE (SIZE)
REFERANCES <TABLE-NAME>
[COL-NAME] [ON DELETE CASECADE]
Example: -
CREATE TABLE EMP_DETAIL (ENO NUMBER (3) REFERENCES
ENAME VARCHAR2 (15));
* Foreign key constraints at table level: -
Syntax: -
FOREIGN KEY (COL1, COL2, .., COLN)
REFERENCES <TABLE-NAME> [COL-NAME] ;
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 18 of 54
Example: -
CREATE TABLE EMP_DETAIL (ENO NUMBER (3),
ENAME VARCHAR2 (15)
FOREIGN KEY (ENO) REFERANCES EMP (E_NO));
* To ADD constraints when the table is already there.
Syntax: -
ALTER TABLE EMP ADD PRIMARY KEY (E_NO);
Example: -
ALTER TABLE EMP_DETAIL CONSTRIANT FORAIGN KEY
REFERANCES EMP (E_NO);
3. NOT NULL: -
1. This constraint is applied only at column level and not for table level.
2. This constraint can be applied to single column or more than one column.
3. Because of that the NULL value is restricted to be entered into the
column.
Principles of NULL value: -
1. Setting a NULL value is appropriate when the actual value is
unknown.
2. A NULL value is not equivalent to zero for the number data-type
or space for Character data-type.
3. A NULL value will evaluated to null in any expression.
Example: - NULL * 10 = NULL
4. NULL value can be inserted into the columns of any data-type.
5. If a column has NULL values the oracle ignores the UNIQUE,
FOREIGN KEY, and CHECK constraints.
Syntax: -
COLUMN-NAME DATA-TYPE (SIZE) NOT NULL
Example: -
CREATE TABLE EMP (E_NO NUMBER (3) NOT NULL,
E_NAME VARCHAR2 (15));
To ADD constraints when the table is already there: -
Example: -
ALTER TABLE EMP ADD CONSTRIANT EMPNO (E_NO) NOT NULL;
4. UNIQUE: -
1. The purpose of UNIQUE constraint is to ensure the information in the
column must be UNIQUE.
2. One table can contain many UNIQUE constraints.
3. The main aim of the UNIQUE constraint is to uniquely identify each row
from the table.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 19 of 54
Unique constraints at column level: -
Syntax: -
COLUMN-NAME DATA-TYPE (SIZE)
[CONSTRAINT <CONSTRAINT-NAME>] UNIQUE;
Example: -
CREATE TABLE EMP (E_NO NUMBER (3) CONSTRAINT
E_UNQ UNIQUE, E_NAME VARCHAR2 (15));
* Unique constraints at table level: -
Syntax: -
UNIQUE (COL-NAME-1, .., COL-NAME-2)
Example: -
CREATE TABLE EMP (E_NO NUMBER (3),
E-NAME VARCHAR2 (15), UNIQUE (E_NO, E_NAME));
* To ADD a constraint when the table is already there: -
Example: -
ALTER TABLE EMP ADD CONSTRIANT E_UNQ (E_NO) UNIQUE;
BUSINESS CONSTRAINTS: -
1. For the integrity of data some of the rules must be implemented that is
known as Business rules or Business constraints.
Example: your employee no must start with a letter E.
2. Constraints can be connected to one column or more than one column of a
table.
3. A business rule constraint can be implemented through CHECK keyword.
4. The CHECK constraint evaluate the condition and gives true or false result.
5. It takes longer time to execute compare to NOT NULL, PRIMARY KEY,
FOREIGN KEY and UNIQUE constraint.
5. CHECK: -
Check constraints at column level: -
Syntax: -
COLUMN-NAME DATA-TYPE (SIZE)
CHECK (LOGICAL EXPRESSION)
Example: -
CREATE TABLE EMP (E_NO NUMBER (3) CHECK (E_NO LIKE E%),
E_NAME VARCHAR2 (15));
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 20 of 54
Check constraints at table level: -
Syntax: -
CHECK (LOGICAL EXPRESSION)
Example: -
CREATE TABLE EMP (E_NO NUMBER (3),
E-NAME VARCHAR2 (15), CHECK (E_NO LIKE E%));
6. DEFAULT: -
1. When we are creating any table at that time we can assign a default value
to a column.
2. When user is inserting value in the columns and by mistake leaves the
column empty to which we have assign Default keyword, then the
oracle engine will automatically insert the default value in that column.
Syntax: -
COLUMN-NAME DATA-TYPE (SIZE) DEFAULT (VALUE);
Example: -
CREATE TABLE EMP
(E_NO NUMBER (3) DEFAULT 100 PRIMARY KEY,
E_NAME VARCHAR2 (15));
() JOIN OPERATIONS:
Join means to access rows from two or more tables. A Join operation involves
two table and table must be JOIN with a WHERE clause in which the common key
field must be specify.
The following are different types of joins.
JOINS
Equi-Join Self-Join Outer-Join
(Inner Join)
1. Equi-Join (Inner Join, Joining Multiple Table): -
When matching record are displayed from both the table is called as Equi
Join.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 21 of 54
Example: -
SALES_ORDER
O_NO CLIENT_NO ORDER_DATE
ARPIT RAVI
LAXMAN RAHUL
SUMIT ARPIT
3. Outer Join (+): -
In previous two join operations, if the matching records are not present in
the second table, certain records from the first table will be not displayed.
To retrieve this record also we have to perform the outer join operation
using the outer join operator (+). The data, which is not available in the
second table, will be presented as the NULL values.
Example:
PLAYER MATCH
ROLL_NO NAME MATCH_NO ROLL_NO MATCH-DATE OPPONENT
-------------------------------- ----------------------------------------------------------------------------
10 VIJAY 1 20 10-JUL-96 WASINGTON
20 PEAS 2 30 12-JAN-96 SAHPRAS
30 ANAND 3 20 12-AUG-96 VIJAY
40 MAHESH 4 30 20-MAR-96 BORG
SELECT PLAYER.ROLL_NO, NAME, MATCH_DATE, OPPONENT
FROM PLAYER, MATCH WHERE PLAYER.ROLL_NO = MATCH.ROLL_NO (+);
Output: -
ROLL_NO NAME MATCH_DATE OPPONENT
--------------- -----------------------------------------------------------------------------
10 VIJAY - -
20 PAES 10-JUL-96 WASHINGTON
20 PAES 12-AUG-96 VIJAY
30 ANAND 12-JAN-96 SAHPRAS
30 ANAND 20-MAR-96 BORG
40 MAHESH - -
(^) SET OPERATORS: -
The oracle supports 4 set operators. All set operators combine the result of two
or more queries into a single set of result.
UNION: -
The UNION operator returns all the rows by the either query eliminating
the duplicate rows.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 23 of 54
Multiple queries can be put together and their output combines using the
union clause. The union clause merges the output of two or more query into a
single set of rows and columns.
OUTPUT = Records in query 1 + Records in query 2.
Example: -
CLIENT_MASTER SALES_MASTER
C_NO NAME CITY S_NO NAME CITY
C001 ASHOK MUMBAI S001 MANISH MUMBAI
C002 VISHAL DELHI S002 KIRAN DELHI
C003 AJAY MUMBAI S003 NITESH MUMBAI
C004 ROHIT CALCUTTA S004 MAHESH CALCUTTA
C005 NALINI MUMBAI S005 NALINI MUMBAI
C006 PARESH DELHI
C007 RAHUL BARODA
SELECT S_NO ID, NAME FROM SALES_MASTER WHERE CITY = MUMBAI
UNION
SELECT C_NO ID, NAME FROM CLIENT_MASTER WHERE CITY =
MUMBAI;
UNION ALL
The UNION ALL operator displays all the rows return by either query
and does not eliminate the duplicate rows.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 24 of 54
OUTPUT = Records in query 1 + Records in query 2 + Common records in both the
query
Example: -
FY SY
LANG1 LANG2
C ORACLE
C++ C++
PASCAL JAVA
SELECT LANG1 LANGUAGE FROM FY
UNION ALL SELECT LANG1 LANGUAGE FROM SY;
SELECT S_NO ID, NAME FROM SALES_MASTER WHERE CITY = MUMBAI
UNION ALL
SELECT C_NO ID, NAME FROM CLIENT_MASTER WHERE CITY =
MUMBAI;
INTERSECT
The INTERSECT operator returns all the rows which are in the both the
query.
Multiple query can be put together and their output are combine using the
intersect clause. The intersect clause displays the rows produced by both the
query intersected. That is it output only those rows that are retrieve by both the
query.
OUTPUT = Common records in both the query.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 25 of 54
Example:
SALES_MASTER SALES_ORDER
S_NO NAME CITY O_NO O_DATE S_NO
-------------------------------------- --------------------------------------------
S001 MANISH MUMBAI O1001 12-APR-04 S001
S002 KIRAN DELHI O1002 25-DEC-04 S003
S003 NITESH MUMBAI O1003 03-OCT-04 S001
S004 MAHESH CALCUTTA O1004 18-JUN-04 S004
O1005 20-AUG-04 S003
O1006 12-JAN-04 S002
SELECT S_NO, NAME FROM SALES_MASTER WHERE CITY = MUMBAI
INTERSECT
SELECT SALES_MASTER.S_NO, NAME FROM SALES_MASTER,
SALES_ORDER
WHERE SALES_MASTER.S_NO = SALES_ORDER.S_NO;
SELECT LANG1 LANGUAGE FROM FY
INTERSECT SELECT LANG1 LANGUAGE FROM SY;
MINUS
The MINUS operator produces the rows return 1st query but not the 2nd
query.
Multiple queries can be put together and their output combines using the
minus clause. The minus clause output the record produce by the 1st query after
filtering the rows return by the 2nd query.
OUTPUT = Records in query 1.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 26 of 54
Example: -
SALES_ORDER_DETAIL PRODUCT_MASTER
O_NO P_NO P_NO DESCRIPTION
----------------------------------- -------------------------------------------
O001 P001 P001 1.44 FLOPPIES
O001 P004 P002 MONITOR
O001 P006 P003 MOUSE
O002 P002 P004 1.22 FLOPPIES
O002 P005 P005 KEY BOARD
O003 P003 P006 CD DRIVE
O004 P001 P007 HDD
O005 P006 P008 1.44 DRIVE
O005 P004 P009 1.22 DRIVE
O006 P006
SELECT P_NO FROM PRODUCT_MASTER
MINUS SELECT P_NO FROM SALES_ORDER_DETAIL;
SELECT LANGUAGE1 LANGUAGE FROM FY
MINUS SELECT LANGUAGE2 LANGUAGE FROM SY;
(10) SUB QUERY: -
A SUB Query is a query within another query.
A SUB Query is a form of an SQL statement that appears inside another
SQL statement. It is also know as NESTED QUERY. The statement containing a
sub query is called PARENT STATMENT. The parent statement uses the rows
return by the sub query.
SUB QUERY can be used by the following commands: -
1. To Insert the records to a target table
2. To Create tables and To Insert the records in the new created table.
3. To Update records in a target table
4. To Create views
5. To provide values for conditions in WHERE, HAVING, IN, etc. used with
SELECT, UPDATE and DELETE statement.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 27 of 54
Example: -
SALES_ORDER CLIENT_MASTER
O_NO CLIENT_NO ORDER_DATE CLIENT_NO NAME BAL_DUE
O001 C006 12-APR-04 C001 ASHOK 500
O002 C002 25-DEC-04 C002 VISHAL 1000
O003 C001 03-OCT-04 C003 AJAY 0
O004 C005 18-JUN-04 C004 ROHIT 0
O005 C002 20-AUG-04 C005 NALINI 0
O006 C001 12-JAN-04 C006 PARESH 0
C007 RAHUL 0
SELECT * FROM SALES_ORDER WHERE CLIENT_NO =
(SELECT CLIENT_NO FROM CLIENT_MASTER WHERE NAME = ASHOK);
OUTPUT: -
O_NO CLIENT_NO O_DATE
100 RAM
300 LAXMAN
500 RAHUL
600 RAVI
700 RAHUL
SELECT EMP_NO, EMP_NAME FROM EMP WHERE EMP_NO IN
(SELECT EMP_NO FROM EMP WHERE DEPT = MARKETING);
OUTPUT: -
EMP_NO EMP_NAME
200 SITA
400 BHARAT
SELECT * FROM EMP WHERE EMP_NO IN (SELECT EMP_NO FROM EMP
WHERE SALARY > 5000);
OUTPUT: -
EMP_NO EMP_NAME DEPT SALARY
20
SELECT SYSDATE FROM DUAL;
OUTPUT: -
SYSDATE
19-AUG-06
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 30 of 54
(12) ORACLE FUNCTION: -
An oracle function serves the purpose of manipulating the data items and
returning a function. Function are also capable of accepting user supplied
variables or constants and operates on them. Such variables and constants are
called as arguments. Any number of arguments can be passing to a function in
the following format.
FUNCTION-NAME (Arg-1, Arg-2, ., Arg-N)
Oracle function can be group to gather depending upon whether they
operate a single row or group of rows retrieve from a table.
Group Function / Aggregate Function: -
Function that act on a set of values are called as Group Function.
For Example: -
SUM is a function, which calculates the total set of values
provided as a argument in a function. A Group Function returns a single
result row for a group of query rows.
Categories of Group Function: -
AVG Function: -
Syntax: - AVG ([Distinct / All] N)
Purpose: - It returns the average value of N and it ignores the
NULL value.
Example: - SELECT AVG (SALARY) Avg - Salary FROM
EMP;
Output: - Avg - Salary
4142.85714
MIN Function: -
Syntax: - MIN ([Distinct / All] Expression)
Purpose: - It returns the minimum value of Expression.
Example: - SELECT MIN (SALARY) Minimum - Salary FROM
EMP;
Output: - Minimum - Salary
2500
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 31 of 54
MAX Function: -
Syntax: - MAX ([Distinct / All] Expression)
Purpose: - It returns the maximum value of Expression.
Example: - SELECT MAX (SALARY) Maximum - Salary
FROM EMP;
Output: - Maximum - Salary
6500
SUM Function: -
Syntax: - SUM ([Distinct / All] N)
Purpose: - It returns the total of value N.
Example: - SELECT SUM (SALARY) Total - Salary FROM
EMP;
Output: - Total - Salary
29000
COUNT Function: -
Syntax: - COUNT (* [Distinct / All] Expression)
Purpose: - It counts the number of expression in a given
column.
The * option returns number of rows including
duplicate values and NULL values. It gives number
of row count where expression is not Null.
Example: - SELECT COUNT (*) Total No of Rows FROM
EMP;
Output: - Total No Of Rows
7
Example: - SELECT COUNT (SALARY) Total No Of Salary
FROM EMP;
Output: - Total No Of Salary
7
Example of all Group Function: -
SELECT AVG (SALARY), MIN (SALARY), MAX (SALARY),
SUM (SALARY), COUNT (SALARY) FROM EMP;
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 32 of 54
Output: -
AVG MIN MAX SUM COUNT
(SALARY) (SALARY) (SALARY) (SALARY) (SALARY)
oracle
UPPER Function: -
Syntax: - UPPER (Char Set)
Purpose: - It returns set of character with all the letters in
the upper case.
Example: - SELECT UPPER (oracle) Upper Fun. FROM
DUAL;
Output: - Upper Fun.
ORACLE
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 33 of 54
INITCAP Function: -
Syntax: - INITCAP (Char Set)
Purpose: - It returns the string with the first letter in capital
for every word in a string.
Example: - SELECT INITCAP (I am happy) Initcap Fun.
FROM DUAL;
Output: - Initcap Fun
I Am Happy
SUBSTR Function: -
Syntax: - SUBSTR (String, M, [N])
Purpose: - It returns the position of the character beginning
at character M exiting up to N character. If N is
omitted result is return up to the end character.
Example: - SELECT SUBSTR (SECURE,3,4) SUBSTR
FROM DUAL;
Output: - SUBSTR
CURE
LENGTH Function: -
Syntax: - LENGTH (String)
Purpose: - It returns the number of character in a given
string.
Example: - SELECT LENGTH (BCA) NO. OF CHAR
FROM DUAL;
Output: - NO. OF CHAR
3
LTRIM Function
Syntax: - LTRIM (String [SET])
Purpose: - It removes the character from the left of the
string with initial character remove up to the
first character.
Example: - SELECT LTRIM (_ _ _BCA) LTRIM FROM
DUAL;
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 34 of 54
Output: - LTRIM
BCA
RTRIM Function: -
Syntax: - LTRIM (String [SET])
Purpose: - It returns the character with final character the
last character not in the set.
Example: - SELECT RTRIM (BCA_ _ _) RTRIM FROM
DUAL;
Output: - RTRIM
BCA
LPAD Function: -
Syntax: - LPAD (char-1, N, [char-2])
Purpose: - It returns char-1 left pedal to length N with the
sequence of characters in char-2 by default, if
char-2 is not provided then it takes blank
character.
Example: - SELECT LPAD (BCA, 10,*) LPAD
FROM DUAL;
Output: - LPAD
*******BCA
RPAD Function: -
Syntax: - RPAD (char-1, N, [char-2])
Purpose: - It returns char-1 right pedal to length N with the
character in char-2. It replicates as many times
as necessary, if char-2 is omitted then it takes
blank character.
Example: - SELECT RPAD (BCA, 5,*) RPAD FROM
DUAL;
Output: - RPAD
BCA**
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 35 of 54
INSTR Function: -
Syntax: - INSTR (string, [set])
Purpose: - It returns the occurrence of character in a given
string.
Example: - SELECT INSTR (EMP_NAME,A) First
INSTR (EMP_NAME,A, 1,1) Second
INSTR (EMP_NAME,A, 3,2) Third
INSTR (EMP_NAME,A, 1,2) Fourth
FROM EMP WHERE EMP_NAME=RAM;
SOUNDEX Function: -
Syntax: - SOUNDEX (string)
Purpose: - It gives the strings, which have the nearest
meaning in speaking.
Example: - CREATE TABLE SOUND (SOUND1
VARCHAR2 (10));
SELECT * FROM SOUND WHERE SOUNDEX
(SOUND1) =
SOUNDEX (COLOR);
2. Numeric Function: -
This type of function works for numeric data type.
Following are the various Numeric Functions.
ABS Function: -
Syntax: - ABS (N)
Purpose: - It returns the absolute value of N.
Example: - SELECT ABS -15) Absolute Value FROM
DUAL;
Output: - Absolute Value
15
POWER Function: -
Syntax: -POWER (M, N)
Purpose: - It returns M raised N
th
power. Must be N is
integer else the error will return.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 36 of 54
Example: - SELECT POWER (3,2) 3 Raised to 2 FROM
DUAL;
Output: - 3 Raised to 2
9
ROUND Function: -
Syntax: - ROUND (N [M])
Purpose: -It returns N rounded to the M places right side
of the decimal point. If M is omitted then N is
rounded to ZERO places. M can be a negative
number to round the digit left of the decimal
point. M must be an integer.
Example: - SELECT ROUND (123.55,1) Rounded Value
FROM DUAL;
Output: - Rounded Value
123.6
Example: -SELECT ROUND (123.55,2) Rounded Value
FROM DUAL;
Output: - Rounded Value
123.55
Example: - SELECT ROUND (123.55,0) Rounded Value
FROM DUAL;
Output: - Rounded Value
124
Example: - SELECT ROUND (0,1) Rounded Value FROM
DUAL;
Output: - Rounded Value
0
Example: - SELECT ROUND (123.55,-2) Rounded Value
FROM DUAL;
Output: - Rounded Value
100
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 37 of 54
Example: - SELECT ROUND (123, -2) Rounded Value
FROM DUAL;
Output: - Rounded Value
100
SQRT Function: -
Syntax: - SQRT (N)
Purpose: - It returns square root of N. If N is less than 0 or
NULL than the square root function returns real
result.
Example: - SELECT SQRT (25) SQRT Value FROM
DUAL;
Output: - SQRT Value
5
Example: - SELECT SQRT (0) SQRT Value FROM DUAL;
Output: - SQRT Value
0
Example: - SELECT SQRT (NULL) SQRT Value FROM
DUAL;
Output: - SQRT Value
123
Example: - SELECT TRUNC (123.55,1) Truncated Value
FROM DUAL;
Output: - Truncated Value
123.5
Example: - SELECT TRUNC (123, -2) Truncated Value
FROM DUAL;
Output: - Truncated Value
100
Example: - SELECT TRUNC (123, -3) Truncated Value
FROM DUAL;
Output: - Truncated Value
0
Example: - SELECT TRUNC (123, -4) Truncated Value
FROM DUAL;
Output: - Truncated Value
0
Example: - SELECT TRUNC (123, 0) Truncated Value
FROM DUAL;
Output: - Truncated Value
123
CEIL Function: -
Syntax: - CEIL (N)
Purpose: - It returns ceiling value of the given number N.
Example: - SELECT CEIL (44.778) CEIL Value FROM
DUAL;
Output: - CEIL Value
45
Example SELECT CEIL (44.228) CEIL Value FROM
DUAL;
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 39 of 54
Output CEIL Value
45
FLOOR Function: -
Syntax: - FLOOR (N)
Purpose: - It returns flooring value of the given number N.
Example: - SELECT FLOOR (44.778) FLOOR Value
FROM DUAL;
Output: - FLOOR Value
44
Example: - SELECT FLOOR (44.228) FLOOR Value
FROM DUAL;
Output: - FLOOR Value
44
MOD Function: -
Syntax: - MOD (D, d)
Purpose: - It gives modulo or reminder of two expression.
Example: - SELECT MOD (10,3) MOD Value FROM
DUAL;
Output: - MOD Value
1
Example: - SELECT MOD (10,4) MOD Value FROM
DUAL;
Output: - MOD Value
2
3. Conversion Function: -
This type of function works for conversion of one data
type value to another data type value. Following are varies
Conversion Functions.
TO_NUMBER Function: -
Syntax: - TO_NUMBER (Char)
Purpose: - It converts character value to the number data-
type format or in numeric format.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 40 of 54
Example: - SELECT TO_NUMBER (SUBSTR ($100, 2, 3))
NUM FROM DUAL;
Output: - NUM
100
TO_CHAR Function (For Number): -
Syntax: - TO_CHAR (N, [Format])
Purpose: - It converts a value of number data-type to a
value of char data-type using the optional
format string.
Example: - SELECT TO_CHAR (17145, $099999.99)
CHAR FROM DUAL;
Output: - CHAR
$017145.00
TO_CHAR Function (For Date): -
Syntax: - TO_CHAR (Date, [Format])
Purpose It converts a value of date data-type to character
data-type using the optional format string.
Example SELECT TO_CHAR (SYSDATE, MONTH-DD-
YYYY) Format FROM DUAL;
Output: - Format
AUGUST-30-2006
4. Date Function: -
This type of function works on date data type.
Following are varies Date Functions.
TO_DATE Function: -
Syntax: - TO_DATE (char, [Format])
Purpose: - It converts characters data fields to date data
fields in specified format.
Example: - SELECT TO_DATE (30/JUN/06,DD MM YY)
FROM DUAL;
Output: - TO_DATE
30-JUN-06
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 41 of 54
ADD_MONTHS Function: -
Syntax: - ADD_MONTHS (Date, N)
Purpose: - This function adds number of months to a
specific date and it returns the date after adding
number of months specified with the function.
Example: - SELECT ADD_MONTHS (SYSDATE, 2) FROM
DUAL;
Output: - ADD_MONTH
29-OCT-06
LAST_DAY Function: -
Syntax: - LAST_DAY (Date)
Purpose: - It returns the last date of the month specified
with function.
Example: - SELECT SYSDATE, LAST_DAY (SYSDATE)
FROM DUAL;
Output: - SYSDATE LAST_DAY
-------------- ----------------
29-AUG-06 31-AUG-06
MONTHS_BETWEEN Function: -
Syntax: - MONTHS_BETWEEN (Date-1, Date-2)
Purpose: - It returns the numbers of months between Date-
1 and Date-2.
Example: - SELECT MONTHS_BETWEEN (2-FEB-04, 2-
JAN-04) MONTH FROM DUAL;
Output: - MONTH
1
NEXT_DAY Function: -
Syntax: - NEXT_DAY (Date, Char Day)
Purpose: - It returns the date of first weekday named by
character day. The Char Day must be a day of
the week.
Example: - SELECT NEXT_DAY (sysdate, Sunday)
NEXT_DAY FROM DUAL;
Output: - NEXT_DAY
03-SEP-06
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 42 of 54
(1) VIEWS: -
View is a restricted-right of a table.
It is nothing but the mirror image of a original table.
Views are logical table of data that are extracted from the existing tables.
Why views are created?
1. After a table is created and populated with data, It is necessary to prevent
all the users from accessing all the columns of a table for the data security
region.
2. One solution of the above problem is to create several duplicate tables
having number of columns and assign specific rights to the users.
3. Due to the above solution the redundant data increases in the oracle.
4. We are reducing the redundant data and provide the data security with the
use of object views. Because to prevent the unauthorized access without
redundancy is the prime objective of views.
5. Views can be building up with the use of select statement to select number
of columns in the specified view.
6. View is stored as a definition in the oracle system catalogue.
7. When reference is made to the view its definition is scanned from a
catalogue, based table is open and view is created on the top of the based
table.
8. View cannot hold data until specific call is made to the view.
9. When view is used to manipulate the table data based table is completely
invisible and provides the security.
Types of views
1. A view that is used to look at the table data and nothing else that view is
called READ ONLY VIEW.
2. A view that is used to look at the table data as well as To Insert, Update
and Delete the data is called UPDATABLE VIEW.
Syntax: -
CREATE OR [REPLACE] [FORCE/NOFORCE] VIEW <View-name>
AS [SELECT Statement] [WHERE Condition] [WITH CHECK OPTION]
[CONSTRAINT Constraint-name] [WITH READ ONLY] [GROUP BY]
[HAVING Clause];
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 43 of 54
Replace View already exists than it recreates the existing view.
Force It allows the creation of view even when the base table does not
exist and user does not have privileges to create it.
NO Force It allows the creation of view only when the based table is exists.
With Check Option
The check option apply to the WHERE Clause condition in sub
query. It allows the Insertion and Updating of rows based on the
condition that satisfies the view.
Views that is define from a single table: -
CREATE VIEW EMP_VIEW AS SELECT * FROM EMP;
CREATE VIEW EMP_VIEW1 AS SELECT * FROM EMP WHERE
ENO=200;
CREATE OR REPLACE VIEW EMP_VIEW2 AS SELECT * FROM
EMP;
To display the column data: -
SELECT * FROM EMP_VIEW;
SELECT * FROM EMP_VIEW1;
SELECT * FROM EMP_VIEW2;
To see the structure of the view: -
DESCIBE EMP_VIEW;
DESCIBE EMP_VIEW1;
DESCIBE EMP_VIEW2;
To remove the view: -
DROP VIEW EMP_VIEW;
DROP VIEW EMP_VIEW1;
DROP VIEW EMP_VIEW2;
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 44 of 54
UPDATABLE VIEW
View cannot be UPDATABLE, if it contains SET Operators,
GROUP Function, GROUP BY Clause and DISTINCT Clause.
Rules for updating a view define on a single table: -
If user wants to inserts the records in the view then PRIMARY KEY
and NOT NU LL columns must be included in the view.
The user can update and delete the records with the help of a view
even if PRIMARY KEY and NOT NULL columns are not included in
the view.
Examples of views that is define from a single table: -
CREATE VIEW CLIENT_VIEWAS SELECT CLIENT_NO, NAME, BAL_DUE
FROM CLIENT_MASTER;
To Insert Operation: -
INSERT INTO CLIENT_VIEW VALUES (C001,RAMESH, 2500);
To Update Operation: -
UPDATE CLIENT_VIEW SET BAL_DUE =500 WHERE NAME=RAMESH;
To Delete Operation: -
DELETE FROM CLIENT_VIEW WHERE NAME=RAMESH;
Creation of view with CHECK OPTION: -
CREATE OR REPLACE VIEW CUST_VIEW AS SELECT * FROM CUST
WHERE CITY=UDP WITH CHECK OPTION;
NOTE: The CHECK OPTION will validate the data during the insert or update based on
the CHECK condition.
For Example: - If CITY=UDP
When a row is inserted into the view the CITY has to be UDP, similarly
when view is updated the value in the column CITY has to be UDP.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 45 of 54
Creation of view with Read Only Option: -
CREATE OR REPLACE VIEW CUST1_VIEW AS SELECT * FROM CUST
WITH READ ONLY;
NOTE: The READ ONLY OPTION does not allow the insert, update and delete
operation on the view. We can only see the data of the view.
Views that is define from multiple tables (The table which have no referential
clause): -
If view is created from multiple tables which were not created using
referential integrity constraints (No logical relationship between the tables) than
all though Primary key and Not Null columns included in the view definition, the
view reacts in the following way.
1. The INSERT, UPDATE and DELETE operation is not allow.
Views that is define from multiple tables (The table which has no referential
clause): -
If view is created from multiple tables which were created using
referential integrity constraints (Logical relationship between the tables) than all
though Primary key and Not Null columns included in the view definition, the
view reacts in the following way.
1. The INSERT, UPDATE and DELETE operation is not allow.
2. An INSERT operation is not allowed.
3. The DELETE or UPDATE operation do not effect the master table.
4. The view can be used UPDATE the columns of the detail table included in
the view.
5. If a DELETE operation is executed on the view the corresponding records
from the detail table will be deleted.
Examples of views that is define from a multiple tables: -
CREATE VIEW VIEW_SALES AS SELECT ORDER_NO, ORDER_DATE,
DELY_ADDR, PRODUCT_NO, QTY_ORDERED, QTY_DISP FROM
SALES_ORDER, SALES_ORDER_DETAIL WHERE SALES_ORDER.ORDER_NO =
SALES_ORDER_DETAIL.D_ORDER_NO;
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 46 of 54
To Insert Operation: -
Not allowed in both table.
To Update Operation: -
UPDATE VIEW_SALES SET QTY_DISP = 100 WHERE
D_ORDER_NO = O002;
To Delete Operation: -
DELETE FROM VIEW_SALES WHERE PRODUCT_NO = P001
AND D_ORDER_NO = O003;
(1!) INDEXES: -
Definition: -
An IDEX is an ordered list of constants of a column or a group of columns
of a table.
When user fires a SELECT statement to search for a particular record the
oracle engine must first locate the table on the HDD. The oracle engine reads the
system information and locates the starting location of table's record on the current
storage media. Then performs sequential search to locate the records that match the
user criteria.
For Example To locates the Client no = C004 in Sales order table; the
oracle engine must first locate the Sales order table and then performs a sequential
search on the Client no column to find the value C004.
So, Oracle engine has to search entire table to find column that contain the
value C004.
So, An Indexing a table is an access strategy that is a way to sort and
search the records in the tables. Index is essential to improve the speed with which the
records can be located and retrieve from a table.
Index is a two dimensional matrix that contains: -
A column, which holds the sorted data.
An address field, which identified the record, which is called ROWID.
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 47 of 54
RowID: -
RowID is an internal generated and maintained binary value, which
identified a record. It provides the location of the particular record in oracle
database.
Example: - SELECT RowID, EMP_NAME FROM EMP;
Output: -
ROWID EMP_NAME
AAAGE1AABAAAIUsAAA RAM
AAAGE1AABAAAIUsAAB SITA
AAAGE1AABAAAIUsAAC LAXMAN
AAAGE1AABAAAIUsAAD BHARAT
AAAGE1AABAAAIUsAAE RAHUL
AAAGE1AABAAAIUsAAF RAVI
AAAGE1AABAAAIUsAAG RAHUL
7 rows selected.
Creation of Index: -
An Index can be created on one or more then one columns. So, it is
divided in two categories.
There are two types of Index.
Simple Index: -
An Index, which is created on single column of a table, is called
Simple Index.
Syntax: -
CREATE INDEX <INDEX-NAME> ON <TABLE-NAME>
(COLUMN-NAME);
Example: -
CREATE INDEX Idx_Client_No ON Client_master (Client_no);
CREATE INDEX Idx_Emp_Name ON Emp (Emp_no);
Composite Index: -
An Index, which is created on more then one column of a table,
is called Composite Index.
Syntax: -
CREATE INDEX <INDEX-NAME> ON <TABLE-NAME>
(COLUMN1, , COLUMNN);
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 48 of 54
Example:
CREATE INDEX Idx_Client ON Client_master (Client_no,
Client_name);
CREATE INDEX Idx_Emp ON Emp (Emp_no, Emp_name);
Creation of unique Index: -
There are two types of Index.
Simple Unique Index: -
If the Index is created on only one column and the values of the
column is not repeated then this type of Index is called Simple Unique
Index.
Syntax: -
CREATE UNIQUE INDEX <INDEX-NAME> ON <TABLE-NAME>
(COLUMN-NAME);
Example:
CREATE UNIQUE INDEX Idx_Client_No ON Client_master
(Client_no);
CREATE UNIQUE INDEX Idx_Emp_Name ON Emp (Emp_no);
Composite Unique Index: -
If the Index is created on more then one column and the values
in this column are not repeated then this type of Index is called
Composite Unique Index.
Syntax: -
CREATE UNIQUE INDEX <INDEX-NAME> ON <TABLE-NAME>
(COLUMN1,,COLUMNN);
Example:
CREATE UNIQUE INDEX Idx_Client ON Client_master (Client_no,
Client_name);
CREATE UNIQUE INDEX Idx_Emp ON Emp (Emp_no, Emp_name);
To see the Index record: -
SELECT * FROM Client_master;
SELECT * FROM Emp;
To see the list of Indexes: -
SELECT * FROM User_Indexes;
RDBMS SQL * Plus CCMCA-Wadhwan
Developed By: Jaypalsinh A. Gohil Page 49 of 54
To see the structure of Indexes: -
DESCRIBE User_Indexes;
Name Null? Type
505
SELECT Seq_Emp.NEXTVAL FROM DUAL;
Output: - NEXTVAL
506
To Insert the record in the Sequences: -
INSERT INTO EMP VALUES (TOCHAR
(SEQ_EMP.NEXTVAL),GAJOO,COMPUTER, 10000);
To See the list of Sequences: -
SELECT * FROM USER_SEQUENCES;
SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER
EMP E