SQL_MANUAL_NEW
SQL_MANUAL_NEW
1. CHAR(size) : This data type is used to store character strings values of fixed length.
The maximum number of characters (i.e. the size) this data type can hold is 255
characters.
2. VARCHAR (size) / VARCHAR2(size) : This data type is used to store variable length
alphanumeric data. The maximum this data type can hold up to 4000 characters.
3. DATE : This data type is used to represent date and time. The standard format is DD-
MON-YY as in 21 -JUN-04.
4. NUMBER (P, S) : The NUMBER data type is used to store numbers (fixed or floating
point). P:precision, S:scale.
5. LONG : This data type is used to store variable length character strings containing up
to 2 GB.
6. RAW / LONG RAW : The RAW / LONG RAW data types are used to store binary
data, such as digitized picture or image.
( Table Created )
d. Drop Table : This command is used to completely remove the table structure
along with the values from the database.
e. Truncate Table : This command is used to remove all the values present in
the database. It does not remove the table (or) database structure.
Ex : desc student;
Name varchar2(20)
Class varchar2(10)
Sec char(1)
a. Insert : This command is used always to create a new row and insert the values
Syntax :
Update tablename
SET condition
where condition;
If the user wants to update specific records then he should use 'Where' Clause.
c. Delete : Delete Command removes the records from the table temporarily until
a commit command is used or a DDL command is executed.
Syntax : delete
from tablename
where condition;
3 Rows deleted
(2) delete
from student
where rno=103;
1 Record deleted
d. Select : Select command is used to retrieve the values from the database based
on selection criteria.
Syntax :
select * or (column1,column2,..... )
from tablename
where condition;
Ex :1 select *
from student;
Ex :2 select rollno,class
from student
where sec=’c’;
Ex: SQL>commit;
SQL>rollback;
Dual Table
DUAL is a table owned by SYS.
Dual is a small Oracle worktable, which consists of only one row and one column, and
contains the value ‘x’ in that column.
Output:
NAME Null? TYPE
Dummy VARCHAR2 (1)
Ex : 1 select department,sum(salary)
from emp
group by department;
The above SQL Statement groups the employees based on departments and
calculates the sum of the salary.
ORDER BY Clause
Order by clause is used to retrieve the rows from the table in a sorted order.
The order can be either ascending order or descending order depending on the
condition specified in the select statement.
The order by clause by default displays the data in the ascending order.
Ex : select emp_id,ename,sal
from employee
order by sal desc;
ORACLE FUNCTIONS
2.Scalar Functions (Single Row Functions) : Functions that act on only one value at a
time
Aggregate Functions
Syntax : Avg();
Syntax : Count();
3. Count(*) : Returns the number of rows in the table including duplicates and those
with nulls.
4. Max : This function returns the maximum value from a given column
Syntax : max ();
6.Sum : This function returns the sum of values in a given column or expression.
2. Scalar Functions
Department Of Computer Science Page No: 8
Department Of Computer Science Page No: 9
Department Of Computer Science Page No: 10
Department Of Computer Science Page No: 11
Department Of Computer Science Page No: 12
Department Of Computer Science Page No: 13
DATE CONVERSION FUNCTIONS
The DATE data type is used to store date and time information.
Syntax: TO_DATE(char{format})
Ex:
INSERT INTO CUST (DOB) VALUES( TO_DATE(’25-JUN-1952 10:55 A.M.’,
‘DD-MON-YY HH:MI A.M.’));
DATE FUNCTIONS
ADD_MONTHS: Returns date after adding the number of months specified in the
function.
Syntax: ADD_MONTHS(d, n)
Ex:
Output:
Add Months
01-NOV-04
LAST_DAY: Returns the last date of the month specified with the function.
Syntax: LASTDAY(d)
Output:
SYSDATE - LastDay
01-JUL-04 31-JUL-04
NEXT_DAY : Returns the date of the first weekday named by char that is after the
date named by char must be a day of the week.
Example 1:
SELECT TO_CHAR(SYSDATE, ‘DD-MM-YY’) FROM DUAL;
Example 2:
SELECT TO_DATE (‘06/07/02’, ‘DD/MM/YY’) FROM DUAL;
Ex 1:
Create table student (Roll_No number(4) primary key,
Name varchar2(20),
class varchar2(15),
Sec char(1));
DEFAULT:
-------------------------------------------------------------------
Syntax:ColumnName Datatype(Size) default(value)
Stu Table
RNO NAME Group Stu_marks Table
2001 Ram M.P.Cs RNO Maths Computer
2002 Robert M.E.Cs 2001 95 85
2003 Rahim M.S.Cs 2002 75 95
1. Cartesian Join : In Cartesian Join each record of table A is associated with all the records of
table B without any condition.
Syntax : select *
from Table A, Table B; (Press Enter)
Ex: select *
from Stu , Stu_marks ;
2. Equi Join : In Equi Join we combine two tables and get the desired task by applying an equality
condition in the where clause.
Syntax : select *
from Table A, Table B
where Equality_Condition;
Ex : select Stu.Rno,Name,Computers
from Stu,Stu_marks
where Stu.Rno=Stu_marks.Rno;
3. Outer Join : Outer Joins are used to display those rows which are common. ( i.e,
Satisfying the condition ) and even those rows which are not satisfying the condition.
There are 3 types of outer joins :
a. Left Outer Join
b. Right Outer Join
c. Full Outer Join
a.) Left Outer Join : It displays all those rows which are common to both the tables that are
satisfying the condition and also those unmatched rows of the table which is specified at the
left hand side. The values of attributes corresponding to second table are considered as
'NULL' Values.
Syntax : select * from Table A left outer join Table B where condition ;
b.) Right Outer Join : Right Outer Join is same as left outer join but the only difference is the
unmatched rows of 2nd Table which is specified on the right hand side are displayed along
with the common rows of both the tables
Syntax : select * from Table A right outer join Table B where condition ;
Syntax : select * from Table A right outer join Table B where condition ;
4. Self Join : In some situation we find it necessary to join a table to itself, this is referred as Self
Join.
In self Join two rows from the same table combine to form a resultant row.
To join a table to itself, Two Copies of the same table have to be opened in memory in
different locations and opened with temporary names , using aliasing.
Eg :stud
Find all the students who has the same address as Ram.(Ans : Robert)
Select s1.name
From stud s1,stud s2
Where s1.address=s2.address and s2.name=’Ram’;
Output:
Name
Ram
Robert
In this case
SET OPERATORS
Set Operators are used to generate queries based on multiple tables.
1.Intersect : The intersect clause will merge the common rows retrieved by multiple tables into a
single set.
Syntax : select *
from Table A
where Condition
INTERSECT
select *
from Table B
where Condition;
Ex : select deptno
from dept
INTERSECT
select deptno
from emp;
Syntax : select *
from Table A
where Condition
UNION
select *
from Table B
where Condition;
Ex :
select deptno from dept
union
select deptno from emp;
3. Minus : The Minus clause will remove the rows retrieved from Table B that are present in
Table A and outputs the remaining available rows in Table A
Syntax : select *
from Table A
where Condition
Minus
select *
from Table B
where Condition;
NESTED QUERIES
A nested query is a form of an SQL statement that appears inside another SQL
statement.
The statement containing a sub query is called a parent statement.
The parent statement uses the rows returned by the sub query.
Ex: select * from student_marks where rno=(select rno from student where name='Ram');
The above Nested Query will display All the Details of the Student Ram. The inner query will
generate the roll_number of ram which will be passed to the outer query.
Note : If the Inner query uses the value produced by the outer query then that query is called as
Co-related sub query.
A View is an object which is used to prevent all users from accessing all columns of a table for
data security reasons.
A View is stored only as a definition in ORACLE System Catalogue. When a reference is made to
a view , the definition is scanned and base table is opened along with view created on base table.
1 view created.
Destroying a View.
To destroy a view ae use "Drop" Command.
Syntax : Drop view View_Name;
INDEXES
An Address that identifies the location of the record in the oracle database is called as Row_Id.
Row_Id is an integral generated and maintained binary value which identifies a record.
Creation of Index :
Syntax: create index index_name on table_name(col1,col2,....);
Destroying an Index:
Syntax : Drop index index_Name;
In Oracle, you can create an autonumber field by using sequences. A sequence is an object in
Oracle that is used to generate a number sequence. This can be useful when you need to create a
unique number to act as a primary key.
Create Sequence
Syntax
The syntax to create a sequence in Oracle is:
sequence_name
The name of the sequence that you wish to create.
Example
MAXVALUE 999999999999999999999999999
To retrieve the next value in the sequence order, you need to use nextval.
For example:
supplier_seq.NEXTVAL;
This would retrieve the next value from supplier_seq. The nextval statement needs to be used in a
SQL statement. For example:
This insert statement would insert a new record into the suppliers table. The supplier_id field
would be assigned the next number from the supplier_seq sequence. The supplier_name field would
be set to Kraft Foods.
Drop Sequence
Once you have created your sequence in Oracle, you might find that you need to remove it from
the database.
Syntax
The syntax to a drop a sequence in Oracle is:
Ex:
supplier_seq.NEXTVAL;