Database Management & Administration - Lecture 4
Database Management & Administration - Lecture 4
the columns being selected must be identical Union Returns all the tuples that are in any of the table eliminating the duplicates Intersect Returns all the tuples that are in both the tables Minus Returns all the tuples that are in Table 1 but not in Table 2
Employee
Employee_Name Mr. A Mr. B Mr. C Mr. D Salary 12000 12300 14000 15070
Employee2 Table
Employee_Name Mr. B Mr. F Mr. E Mr. D Mr. G Salary 12300 14600 17250 15070 18250
Insert
Select Employee_Name, Salary from Employee
Union
Select Employee_Name, Salary from Employee
Minus
Select Employee_Name, Salary from Employee
Join
Joins are very important for relational
databases. Mainly used when we need to find information that distributes over multiple tables.
Employee
Branch
Employee Table
create table employee
( employee_id number primary key, name varchar2(50), branc_id number references branch(branch_id) );
10
Branch Table
create table branch
11
12
13
Joining
select * from employee, branch;
join
14
Employee
Branch
15
should be used with join To uniquely identify the column name (if same name), the name should be preceded with table_nameand dot(.). If the column names do not conflict then they can be used without the table name. select * from employee, branch where employee.branch_id = branch.branch_id;
Pre p a re dIAC,ru p R a to n R o y B y: A CSE, BUET
16
17
Joining More
Any number of tables can be joined in joining select * from
table1, table2, ....... tableN where table1.col11 =table2.col21 and ......and tablen1 .coln1 =tablem1 .colm1 ;
18
employees manager
Employee Manager
Employee_id 1 2 3 4 5
Manager_id 2 4 null 3 2
19
Example
List the names of all employees together with
the name of their manager. Two same tables. S0 removing the conflict alias should be given to table
20
Inner Join
The joining done so far also called Inner Join. Oracle has another syntax. select * from
21
Natural Join
Automatically joins the two tables that have a
commonly named and defined field. The join returns the tuples which have the same value in the columns of same name. select * from employee natural join branch; Here, branch_id is the same column exists in both employee and branch table.
22
Outer Join
Three types
Left Outer Join Right Outer Join Full Outer Join
23
tuples selected. If a tuple from the left table is not matched with any tuples in the right table, it will have null in all the fields for the right table column
24
tuples selected. If a tuple from the right table is not matched with any tuples in the left table, it will have null in all the fields for the left table column
25
selected tuple. If a tuple from the left table is not matched with any tuple from the right table, then it will have null values in the right table column If a tuple from the right table is not matched with any tuple from the left table, then it will have null values in the left table column
26
Branch_id 2 1 1 1 2 5
27
28
29
30
31
employee.branch_id = branch.branch_id;
32
33
Sub Query
a query result can also be used in a condition
of a where clause. in such a case the query is called a subquery and the complete select statement is called a nested query. We can refer the top query table from sub query. View the sub query as if it is executed for each tuple in top query
34
In Where Clause
A respective condition in the where clause then can
1. Set-valued subqueries <expression> [not] in (<subquery>) <expression> <comparison operator> [any|all] (<subquery>)
computed value.
35
Example
Select the employees who works in Palashi
Branch
select * from employee where branch_id
Palashi Branch
select * from employee where branch_id not
36
Caution
You can use (=) for checking condition, if you
are sure enough that your subquery returns a single value. Better, use in. Because it eliminates the necessity of single return value. And also works for the single return value.
37
Exists/Not Exists
Often a query result depends on whether
certain rows do (not) exist in (other) tables. Such type of queries is formulated using the exists operator. List all branch that have no employees
select * from branch
38
39
Any
For the clause any, the condition evaluates
to true if there exists at least on row selected by the sub query for which the comparison holds. If the sub query yields an empty result set, the condition is not satisfied. Retrieve all employees who are working in branch 1 and who earn at least as much as any employee working in department 2.
select * from employee where branch_id = 1
40
All
For the clause all, the condition evaluates
to true if for all rows selected by the sub query the comparison holds. In this case the condition evaluates to true if the sub query does not yield any row or value. select the employee with maximum salary
select * from employee where salary
41
thus subquery can be joined with a table. select ............ from table1, table2...., (select ..... from table_s ......)subqueryName, ..... here, subqueryNamecan be used to reference a column from subquery. select the employee, salary and average salary. Employee_name Salary Average_salary
select employee_name, salary,
42
Sequence
Oracle provides an object called a Sequence
descending order The maximum number that can be generated by sequence The increment value for generating the next number
43
Syntax
CREATE SEQUENCE <sequenceName>
[INCREMENT BY <integerValue> START WITH <integerValue> MAXVALUE <integerValue>/ NOMAXVALUE MINVALUE <integerValue> / NOMINVALUE CYCLE/NOCYCLE CACHE <integerValue>/NOCACHE ORDER/NOORDER]
44
Example
create sequence employee_id_seq Incremented
45
Referencing a Sequence
Every time nextval references a sequence its
output automatically incremented. select <sequenceName>.NextVal from dual; select employee_id_seq .nextval from dual;
46
Altering a Sequence
Alter Sequence <sequenceName>
[ <propertyName> newValue]; property name is any property that is used in sequence parameter. start value cannot be altered alter sequence employee_id_seq Incremeneted by 2;
47
Dropping a Sequence
drop sequence <sequenceName> drop sequence employee_id_seq ;
48
Greatest
Greatest(expr1, expr2, ......, exprn)
select greatest(4, 5, 17) from dual;
value row wise. [note: Max search column wise] select greatest(A, B) from table1;
Table1
A 5 25
B 10 15
49
Least
Least(expr1, expr2, ......, exprn)
select greatest(4, 5, 17) from dual;
row wise. [note: Min search column wise] select least(A, B) from table1;
Table1
A 5 25
B 10 15
50