Professional Documents
Culture Documents
so... In last two sessions we only see about the basics of sql Okay...
In session 3 we will cover some topics under Advance queries okay.. like Sub
queries ,joins, Flashback queries ,constraints like that...
----------------------------------------------------------------
**************vi sess3.txt***********
Lets us see about what is Sub Query.
Sub queries are an alternate way ..of returning data.. from multiple tables. )))
>> Single row sub query�(Returns zero or one row to the outer query.)
So, in last session we do only single query..like select * from some table like
that... so SQ means query inside the query... So...as a result if i get one row
called Single row SQ
>> Multiple row SQ�(if i get 2 or more rows from the table)
>> Multiple column SQ�(it retns 2 or more columns from the tab)
So, correlated SQ nothing but a query which is going to use the values of another
query is correlated subquery...
>> Nested SQ�(it has sub query is placed another query which means multiple
subqueries...)
Clear...
First is .....
8 rows selected.
So in subquery what i going to do nahh.. I select all the columns from the table
emp_details... so i want to select a max salary right...based on column
salary..so,here see i select the table where salary = we wrote the query right to
get a max salary ...i will wrote that query into the inner query okay...
Here the inner query returns only single row to the outer query. Hence this is said
to be single row subquery.
________________________________________________________________
Syntax: select column_name from table_name where column_name operator (IN,ANY, ALL,
NOT IN) (select column_name from table_name);
8 rows selected.
So,
(( So, what i trying to do nahh.. in this i just want to print the name starting
with r and i don't want to display all ename))
EMPNO ENAME
---------- ----------
1 rohit
2 ricardo
5 raj
_______________________________________________________________
##getting emp details who is getting lowest salary,if im getting one row nah it
look like single rw SQ,so here im getting lowest salary on each dept,we have 3
dept,from 3 dept i want to get lowest salary.
##salary and dept are 2 columns im taking here for conditions.i don't want to
display all deptno with salary i just want to select only min salary in each dept.
##if i want to get info only particular block of row im using groupby clause. .
A query which uses values from the outer query is called as a correlated sub query.
EMP_ID FIRST_NAME
---------- --------------------
1 James
2 Ron
3 Fred
4 Susan
EMP_ID FIRST_NAME
---------- --------------------
1 James
2 Ron
3 Fred
4 Susan
4 rows updated.
EMP_ID FIRST_NAME
---------- --------------------
1
2
3
4
4 rows selected.
4 rows updated.
EMP_ID FIRST_NAME
---------- --------------------
1 James
2 Ron
3 Fred
4 Susan
8 rows selected.
##In ref1 if i want to update first_name.now im select f_n from ref table not all
values only when it is matches emp_id on both the tables.
________________________________________________________________
NESTED SUBQUERIES:
------------------
Sub queries placed with in another queries
((
Outer query can have more than one sub query.
Syntax: Select column_name from table_name where column_name operator (Select
column_name from table_name where column_name operator (Select column_name from
table_name));
))
8 rows selected.
DNAME
----------
sales
Let us consider that we have to find the name of the department which giving
highest salary to employees in an organizaion.so we relate table by common table.
##how i get values,,by common col in each table(deptno).now im select dept no which
is having max salary not all salary.
##50000 is max salary,so tat deptno is 20 it return name of deptno 20 in 2 table.
________________________________________________________________
JOINS:
------
Okay!..Basically what is joins....
Joins can be used to connect any number of tables so that you can retrieve
information from more than one table using the join operator.
There are
* Inner Join
* Outer Join( Left Outer join, Right Outer Join,Full Outer Join)
* self Join
7 rows selected.
DEPTID DEPTNAME
---------- ----------
20
11 admin
12 marketing
13 account
14 finance
17 hr
18 design
19 production
8 rows selected.
Inner Join:
-----------
It returns all rows from multiple tables only when the condition is match..
This means if a row has a null value in one of the columns in the join condition
that row will not be returned.
syntax
-------
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Example:
--------
Syntax
------
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
Example:
-----------
SQL> select a.*,b.* from employe1 a left outer join dept1 b on(a.deptid=b.deptid);
7 rows selected.
________________________________________________________________
RIGHT OUTER JOIN:
------------------
All the rows from right side table and only matched records from left side table.
SQL> select a.*,b.* from employe1 a right outer join dept1 b on(a.deptid=b.deptid);
8 rows selected.
________________________________________________________________
FULL OUTER JOIN:
-----------------
combination of left outer and right outer joins..If any row does not have match we
have a null space there.
SQL> select a.*,b.* from employe1 a full outer join dept1 b on(a.deptid=b.deptid);
10 rows selected.
________________________________________________________________
SELF JOIN:
--------------
A self join is a join made on the same table.
To perform a self join must use a different alias to identify each reference of the
table used in your query.
##to perform self join we have to give 2 diff alias name for same table.
8 rows selected.
SQL> select m.name||' is trainer for '||e.name from org m,org e where
m.man_id=e.emp_id;
M.NAME||'ISTRAINERFOR'||E.NAME
------------------------------------
undertaker is trainer for goldberg
kane is trainer for undertaker
lesner is trainer for kane
batista is trainer for lesner
austin is trainer for batista
jeff hardy is trainer for austin
matt hardy is trainer for jefef hardy
7 rows selected.
##it returns only when the man_id and emp_id get matched,here 100 have no match so
leave this,man_id 101 having name called undertaker,so it print undertaker is
trainer for then it will go and search 101 in emp_id golberg having 101,
______________________________________________________________
SET OPERATORS
----------------
UNION ALL:
----------
It includes all the duplicate values
ID NAME
---------- --------
1 book
2 pencil
3 note
4 pen
5 scale
And I have the another table called product list 2
ID NAME
---------- --------
1 book
2 bag
3 note
6 box
7 paper
ID NAME
---------- --------
1 book
2 pencil
3 note
4 pen
5 scale
1 book
2 bag
3 note
6 box
7 paper
10 rows selected.
UNION :
------
SQL>select * from product_list1 union select * from product_list2;
ID NAME
---------- --------
1 book
2 bag
2 pencil
3 note
4 pen
5 scale
6 box
7 paper
8 rows selected.
In this Union operator, only unique values are selected��
INTERSECT:
----------
ID NAME
---------- --------
1 book
3 note
Here, only common rows are selected�
MINUS:
-----
ID NAME
---------- --------
2 pencil
4 pen
5 scale
SQL>select * from product_list2 minus select * from product_list1;
ID NAME
---------- --------
2 bag
6 box
7 paper
________________________________________________________________
ORACLE DATABASE OBJECTS
=========================
VIEW:
-----
A VIEW is a virtual table.So view is a storage of original table.
Views do not contain data of their own.
It is a logical representation of tables.
View also perform same way as table,u can update,insert,delete etx.,
It has Two types
Simple View
Complex view
Simple View:
-------------
we can perform dml operations on only simple views and not on complex views.
EXAMPLES:
View created.
ID NAME
---------- --------------------
1 sam
2 charanda
3 kiki
4 anu
And I just delete the one row from the view table�.
1 row deleted.
ID NAME
---------- --------------------
1 sam
3 kiki
4 anu
ID NAME
---------- --------------------
1 sam
3 kiki
4 anu
DML operations on a view like INSERT, UPDATE, DELETE affects the data in the
original table upon which the view is based
SQL> rollback;
Rollback complete.
View dropped.
________________________________________________________________
Complex View:
-------------
ID SALARY
---------- ----------
1 3459
2 7634
3 50000
4 899930
5 25000
ID NAME
---------- ----------
1 harsh
2 sri
3 surya
4 vini
5 josh
View created.
ID NAME SALARY
---------- ---------- ----------
1 harsh 3459
2 sri 7634
3 surya 50000
4 vini 899930
5 josh 25000
Constraints:
------------
What is general meaning of constraint ??..
these are some conditions which place some restrictrctions on column of table.
SQL constraints are used to specify rules for the data in a table.
Constraints can be specified when the table is created with the CREATE TABLE
statement, or after the table is created with the ALTER TABLE statement..z
>> NOT NULL�- Ensures that a column cannot have a NULL value
>> UNIQUE�- Ensures that all values in a column are different
>> PRIMARY KEY�- A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table
>> FOREIGN KEY�- Uniquely identifies a row/record in another table
>> CHECK�- Ensures that all values in a column satisfies a specific condition
>> DEFAULT�- Sets a default value for a column when no value is specified
>> INDEX�- Used to create and retrieve data from the database very quickly
NOT NULL:
---------
SQL> create table hb(id number not null);
SQL> insert into hb values(null);
1 row created.
Unique:
-------
Specifies a column should be unique.Here it allows null values but restrict
duplicate values.
Only unique values in the all the row and columns�it doesn�t allows common data�s�
Table created.
SQL>insert into hb values(null);
1 row created.
1 row created.
Table dropped.
PRIMARY KEY:
------------
It is a combination of not null and unique constrainti.e,the table having not null
values and also only unique values�
Table created.
1 row created.
SQL> /
Enter value for id: 1
old 1: insert into hb values(&id)
new 1: insert into hb values(1)
insert into hb values(1)
*
ERROR at line 1:
ORA-00001: unique constraint (VINS.SYS_C007533) violated
SQL> /
Enter value for id: null
old 1: insert into hb values(&id)
new 1: insert into hb values(null)
insert into hb values(null)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("VINS"."HB"."ID")
DEFAULT:
--------
Table created.
SQL>insert into team(id) values(1);
1 row created.
ID NAME
---------- ----------
1 nil
I insert a value for id and I haven�t give any values to the name but by the
default constraint it defaultly takes a value nil
1 row created.
I insert a value for name and I haven�t give any values to the id similarly the
default constraint it default ly takes a value 0
ID NAME
---------- ----------
1 nil
0 dv
Table dropped.
CHECK
------
It is used to place certain conditions for the values to be inserted.
Table created.
1 row created.
Table dropped.
.
Read only:
----------
Read only is a constraint we can�t insert any values in it..but we can only read a
table
Syntax: create view view_name as select * from table_name with read only;
We cannot delete from view because read only constraint is used in it�.
REMOVE A CONSTRAINT.
--------------------
syntax: alter table table_name disable constraint_type;
syntax: alter table table_name drop constraint constraint_name;
syntax: alter table table_name drop constraint_type;
ADD A CONSTRAINT.
----------------
syntax: alter table table_name add constraint_type(column_name);
syntax: alter table table_name add constraint constraint_name constraint_type
(column_name);
syntax: alter table table_name add constraint constraint_name constraint_type
(column_name)references table_name(column_name);
________________________________________________________________
Flashback Query
---------------
Two process:
ID NAME SALARY
---------- ------------------------------ ----------
1 san 58874
2 mathan 87685
3 nimalan 75343
4 peri 8765
5 jamesh 87867
6 parker 8746
6 rows selected.
Table dropped.
once u drop table it move to the recycle bin of db.. To see the table, use the
following command,
SQL>show recyclebin
##after that if u want to recover the table back use this command.
<< Our table space is exceed the max level so we can't retrive back the table,but
this is the exact syntax >>
Flashback complete.
ID NAME SALARY
------------ ----------
1 san 58874
2 mathan 87685
3 nimalan 75343
4 peri 8765
5 jamesh 87867
6 parker 8746
6 rows selected.
SQL>purge recyclebin;
with help of flashback we recover table from recyclebin,for eg,if iam deleting some
records in the table like name=peri.If i make any modifcation or delete any
recoreds in my table,after 2 days,if u want to see the deleted records,for that we
have this process called review the database.
________________________________________________________________
2) Review the Database
-----------------------
ID NAME SALARY
---------- ---------- ----------
1 thiya 3499
2 chin 3400
3 mano 7900
4 sam 9800
4 rows selected.
SQL>commit;
Commit complete.
GET_SYSTEM_CHANGE_NUMBER
------------------------
1174254160
##package always start with dbms_..
4 rows deleted.
SQL> commit;
Commit complete.
no rows selected
After its like 2 or 3 days again i want to view the data inside table,here you
can't recover the past but u able to see wat data tat table had inside by this scn
num.
ID NAME SALARY
---------- ---------- ----------
1 thiya 3499
2 chin 3400
3 mano 7900
4 sam 9800
SQL>execute dbms_flashback.disable();
----------------------------------------------------------------
Global Temporary tables (GTT):
-------------------------------
created with 2 clause.
The ON COMMIT PRESERVE ROWS clause indicates that the data should be deleted at the
end of the session.
1 row created.
SQL>/
1 row created.
SQL>/
1 row created.
ID
----------
1
1
1
SQL>commit;
Commit complete.
ID
----------
1
1
1
SQL>exit
##after exit again if i connect to database and open table tat values will be there
right,..
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
no rows selected
The ON COMMIT DELETE ROWS clause indicates that the data should be deleted when we
commit or rollback the transaction.
Table created.
1 row created.
SQL>/
1 row created.
SQL>/
1 row created.
ID
----------
1
1
1
SQL>commit;
Commit complete.
no rows selected
-------------that's all----------------------
after 4th session you have take an interview.