Starting Oracle: NICT Computer's, Near Kolhapure Circle, Beside Maheswari Blind School, Nehru Nager, Belgaum-10
Starting Oracle: NICT Computer's, Near Kolhapure Circle, Beside Maheswari Blind School, Nehru Nager, Belgaum-10
Starting Oracle
Data :Any valuable information.
Database :Well arranged data, which is populated with data for
specific reason.
DBMS :Data base management system. It is a software which
allows to create database and manipulate the database. Database
manipulation means adding new data, modifying existing data,
deleting & searching. The language used for DBMS operation is called
SQL (Structured Query Language).
Data base terminology
Tuple/Row : A single row in the table is called as tuple.
Attribute/Column : A column stores an attribute of an entity.
Ex.Name, Age etc.
Table Name : Each table is a given a specific name.
Primary Key : Each table should have one or more columns
that can be used to uniquely identifies the rows. That means given a
value in that column. That should not be duplicate.
Composite Primary Key : When a single column can not be used for
unique identification. Then multiple columns can be used together.
Table Employee
Emp. Name Basic Dept.
No. Sal. No.
101
102
103
Sale
Sn. Month Commissio
No. n
Student
Reg. No. Name Marks Dept. No.
ORACLE
What is Oracle:
A DBMS must able to reliable manage a large amount of data in
multi-user environment so that many users concurrently access the
data.
A database system should provide security and failure recovery.
Starting SQL:
SQL is one of the program supplied by oracle this provides an
environment in which user can directly enter SQL command and get
the result.
Column Alias
select name, (sysdate – dob)/365 age from student;
To sort rows
select * from student order by per;
select * from student order by per desc;
);
where constaint_name naming styled is like tablename_colname_type
Method 2
CREATE TABLE tb1
(
name varchar2 (20) NOT NULL,
);
• Dropping Table
Drop Table tablename [Cascade Constraints];
Data Manipulation
Commit Command
- Makes all the changes of database permanent.
- Unlocks the rows that are held during transaction.
Rollback
Used to undo changes made since last commit command
Savepoint
Used to split transaction into smaller blocks, so that we can
rollback upto particular block.
SAVEPOINT savepoint_name;
ROLLBACK to savepoint_name;
Autocommit : issues commit automatically after every insert, delete
and update command.
SET AUTOCOMMIT ON | OFF
Dual Table
This is a table with one row and one column provided by Oracle.
This table can be used with SELECT to test result of a function.
Eg. SELECT sysdate from dual;
- : Date Functions :-
1) Add Months (date, count): Adds count no of months to date
Eg. Select Rollno, dob, add_month (dob,6) from student
Roll no Dob Add_month
11 15-Mar-83 15-Sep_83
Count can be negative to get previous dates.
2) Months between (date1, date2): gives month between two dates.
Eg. Select Rollno, dob, add_months_between (sysdate, dob) Month
from student;
Roll no Dob Month
12 5-Mar-03 3.603
Consider sysdate as 24-June-03
3) Round (Date, [format]) : Rounds up the fractions.
Select rollno, dob, Round (month_between (sysdates, dob))
Month from student;
Roll no Dob Month
12 5-Mar-03 4
-: String :-
Concatenating Strings: (||)
select name, name || ‘ ‘ || Rollno from student;
Length (String): returns no of char in the string
Lower (string) : Converts to lower case
Upper (string) :
Initcap (string) : Make first char capital
LPAD (string, length [,fill string])
RPAD (string, length [,fill string])
LTRIM (string.[,charset])
RTRIM (string.[,charset])
SUBSTR (string, pos, length)
-: Conversion :-
To-char (value [,format]): Converts value which is of no or date
type to char type.
To-Date (char[,format]): Converts character to date type.
To-Number (char): Converts char type to number type.
: TO-CHAR Function :
This function is used to convert the given DATE or NUMBER to
CHAR, also used to format the date or number. To display formatted
date & time portion of SYSDATE.
Select to _char (SYSDATE, ‘dd-month-yyyy’ hh24 : mi :55)
From dual.
-: Other Function :-
DECODE (value, if, then, if, then ---- else)
If value is equivalent to if then substitutes then for value.
Eg.
Select Sno, Sname, course
Decode (status, ‘B’, ‘Break’,
‘C’, Completed
‘S’, studying) status from student;
GREATEST (Value1, Value2, -----) Return greatest of return values
LEAST (Value1, Value2, ------) Return smallest of return values
or
Select student.rollno, sname, subname,marks from student
t1,subject t2 where
t1.rollno=t2.rollno;
-: Set Operators : -
UNION, UNION ALL, INTERSECT, MINUS.
Consider tables
Rollno positio Cricket
n
Rollno positio football
n
Q. List all students in cricket , football or both
Select rollno from cricket
Union
Select rollno from football;
VIEWS
A view is a window through which you access a portion of a
table. View it self does not contain any data, it refers to the data of
table on which it is based.
Creating View
Create or replace view viewname
As Query
[with check option];
SUBQUERIES
A query within another query is called subquery. The outer query
is called as main query and inner query is called as subquery.
Eg.1) select subcode, faculty, marks
From studmarks
Where sno = (select sno from students where sname = ‘xyz’);
- Subquery is always executed first and the result is passed to
the main query.
- If there is possibility of duplicates in subquery the replace =
by in.
Eg.2) Multi-Row subqueries
select sno, sname
from students
where sno in
(select sno from studmarks
where subcode = ‘Oracle’)
- Multiple suqueries
select sno, sname, course from students
where sno in
(select sno from ----------)
and sno in
(select sno from ---------)
- Nesting Subqueries
select sno, ------ from student
where sno in
(select sno from -----------
where subcode in
(select subcode --------));
- Dropping index
[DROP INDEX indexname;]
Clustering: - Clustering is a method of storing tables, that are often
used together (joined), in one are of the disk to improve performance.
Sequence: -
Sequence is a database object which is used by multiple users to
generate unique numbers.
Create Sequence sequencename
[Increment by integer]
[start with integer]
[maxvalue integer : Nomaxvalue]
[Minvalue integer : Nominvalue]
Pseudo Columns: -
Currval -> returns current value of a sequence.
Nextval-> returns next value of a sequence.
Row ID-> returns rowid of a row.
User -> returns name of current user.
Select seq. nextval from dual;
SECURITY
Privilege: - A privilege is a right to access an object such as a table
view etc.
Privilege Meaning
1) Alter Changing the definition of the
object
2) Delete Deleting rows from the object
3) Execute Execute the object
4) Index Creating index on the object
5) Insert Inserting rows into object
6) References References the object
7) Select Selecting rows from object
8) Update Updating data of object
Spool st.1st
Select a.subcode, a.sno, sname, stdate, enddate-stdate, nod, marks
From studmarks a, students b
Where a.sno = b.sno
Order by subcode, sno;
Spool off;
Break Command: -
Used to break the rows selected by SELECT into different groups.
Compute Command: -
Used to compute & print summary information.
[Compute function of column on column;]
Function can be AVG, COUNT, MAX, MIN, STD, SUM, VAR.