database management & administration_lecture 4

Upload: tanoycomet

Post on 07-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Database Management & Administration_Lecture 4

    1/50

    IAC, CSE, BUET:

    P re p a re d B y A ru p R a to n R o y

    Join & Subquery

    Database Management &

    Administration

  • 8/6/2019 Database Management & Administration_Lecture 4

    2/50

    IAC, CSE, BUET 2:

    P re p a re d B y A ru p R a to n R o y

    The number of columns and the data types ofthe columns being selected must be identical

    Union Returns all the tuples that are in any ofthe table eliminating the duplicates

    Intersect Returns all the tuples that are inboth the tables

    Minus Returns all the tuples that are in Table

    1 but not in Table 2

    Union, Intersection & Minus

  • 8/6/2019 Database Management & Administration_Lecture 4

    3/50

    IAC, CSE, BUET 3:

    P re p a re d B y A ru p R a to n R o y

    Employee_Name Salary

    Mr. A 12000

    Mr. B 12300

    Mr. C 14000

    Mr. D 15070

    Employee

  • 8/6/2019 Database Management & Administration_Lecture 4

    4/50

    IAC, CSE, BUET 4:

    P re p a re d B y A ru p R a to n R o y

    Employee2 TableEmployee_Name SalaryMr. B 12300

    Mr. F 14600

    Mr. E 17250

    Mr. D 15070Mr. G 18250

  • 8/6/2019 Database Management & Administration_Lecture 4

    5/50

    IAC, CSE, BUET 5:

    P re p a re d B y A ru p R a to n R o y

    Select Employee_Name, Salary from Employeeunion select Employee_Name, Salary fromEmployee 2

    Insert

    Employee_Name SalaryMr. C 14000

    Mr. D 15070

  • 8/6/2019 Database Management & Administration_Lecture 4

    6/50

    IAC, CSE, BUET 6:

    P re p a re d B y A ru p R a to n R o y

    Select Employee_Name, Salary from Employeeintersect select Employee_Name, Salaryfrom Employee 2

    Union

    Employee_Name SalaryMr. A 12000

    Mr. B 12300

    Mr. C 14000

    Mr. D 15070

    Mr. F 14600

    Mr. E 17250

    Mr. G 18250

  • 8/6/2019 Database Management & Administration_Lecture 4

    7/50

    IAC, CSE, BUET 7:

    P re p a re d B y A ru p R a to n R o y

    Select Employee_Name, Salary from Employeeminus select Employee_Name, Salary fromEmployee 2

    Minus

    Employee_Name SalaryMr. A 12000

    Mr. B 12300

  • 8/6/2019 Database Management & Administration_Lecture 4

    8/50

    IAC, CSE, BUET 8:

    P re p a re d B y A ru p R a to n R o y

    Joins are very important for relationaldatabases.

    Mainly used when we need to find informationthat distributes over multiple tables.

    Join

  • 8/6/2019 Database Management & Administration_Lecture 4

    9/50

    IAC, CSE, BUET 9:P re p a re d B y A ru p R a to n R o y

    Employee Branch

    Employee & Branch

  • 8/6/2019 Database Management & Administration_Lecture 4

    10/50

    IAC, CSE, BUET 10:P re p a re d B y A ru p R a to n R o y

    create table employee( employee_id number primary key,name varchar2(50),

    branc_id number references branch(branch_id));

    Employee Table

  • 8/6/2019 Database Management & Administration_Lecture 4

    11/50

    IAC, CSE, BUET 11:P re p a re d B y A ru p R a to n R o y

    create table branch(branch_id number primary key,

    branch_name varcha2(50));

    Branch Table

  • 8/6/2019 Database Management & Administration_Lecture 4

    12/50

    IAC, CSE, BUET 12:P re p a re d B y A ru p R a to n R o y

    Employee_id Name Brach_id1 Mr. A 2

    2 Mr. B 1

    3 Mr. C 1

    4 Mr. D 15 Mr. E 2

    Employee Table With Data

  • 8/6/2019 Database Management & Administration_Lecture 4

    13/50

    IAC, CSE, BUET 13:P re p a re d B y A ru p R a to n R o y

    Branch_id Branch_Name

    1 Polashi2 Bokshibazar

    Branch Table With Data

  • 8/6/2019 Database Management & Administration_Lecture 4

    14/50

    IAC, CSE, BUET 14:P re p a re d B y A ru p R a to n R o y

    select * from employee, branch;

    Joining

    join

  • 8/6/2019 Database Management & Administration_Lecture 4

    15/50

  • 8/6/2019 Database Management & Administration_Lecture 4

    16/50

    IAC, CSE, BUET 16:P re p a re d B y A ru p R a to n R o y

    All the tuples from a join are not consistent.We have to eliminate the inconsistent tuples.To select the consistent tuples, where clause

    should be used with joinTo uniquely identify the column name (if same

    name), the name should be preceded withtable_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;

    To Selecting The Tuples

  • 8/6/2019 Database Management & Administration_Lecture 4

    17/50

    IAC, CSE, BUET 17:P re p a re d B y A ru p R a to n R o y

    Eliminitating Inconsistent Data

    Employee_id

    Name Branch_id

    Branch_id

    Branch_Name1 Mr. A 2 2 Bokshibazar

    2 Mr. B 1 1 Polashi3 Mr. C 1 1 Polashi

    4 Mr. D 1 1 Polashi

    5 Mr. E 2 2 Bokshibazar

  • 8/6/2019 Database Management & Administration_Lecture 4

    18/50

    IAC, CSE, BUET 18:P re p a re d B y A ru p R a to n R o y

    Any number of tables can be joined in joiningselect * from table1, table2, ....... tableN

    where table1.col11=table2.col21 and ......andtablen1 .coln1=tablem1 .colm1 ;

    Joining More

  • 8/6/2019 Database Management & Administration_Lecture 4

    19/50

    IAC, CSE, BUET 19:P re p a re d B y A ru p R a to n R o y

    Here Manager_id is the employee_id of aemployees manager

    Joining Table with itself

    Employee_id Name Manager_id1 Mr. A 2

    2 Mr. B 4

    3 Mr. C null

    4 Mr. D 3

    5 Mr. E 2

    Employee

    Manager

  • 8/6/2019 Database Management & Administration_Lecture 4

    20/50

    IAC, CSE, BUET 20:P re p a re d B y A ru p R a to n R o y

    List the names of all employees together withthe name of their manager.

    Two same tables. S0 removing the conflict aliasshould be given to table

    select e1.name, e2.name from employeeas e1, employee as e2wheree1.manager_id=e2.employee_id;

    Example

  • 8/6/2019 Database Management & Administration_Lecture 4

    21/50

    IAC, CSE, BUET 21:P re p a re d B y A ru p R a to n R o y

    The joining done so far also called Inner Join.Oracle has another syntax.

    select * from

    table1 inner join table2 on table1.col1 = table2.col2select * from employee inner join branch

    on employee.branch_id =

    branch.branch_id;

    Inner Join

  • 8/6/2019 Database Management & Administration_Lecture 4

    22/50

    IAC, CSE, BUET 22:P re p a re d B y A ru p R a to n R o y

    Automatically joins the two tables that have acommonly named and defined field.

    The join returns the tuples which have thesame value in the columns of same name.

    select * from employee natural join branch;

    Here, branch_id is the same column exists inboth employee and branch table.

    Natural Join

  • 8/6/2019 Database Management & Administration_Lecture 4

    23/50

    IAC, CSE, BUET 23:P re p a re d B y A ru p R a to n R o y

    Three typesLeft Outer JoinRight Outer JoinFull Outer Join

    Outer Join

  • 8/6/2019 Database Management & Administration_Lecture 4

    24/50

    IAC, CSE, BUET 24:P re p a re d B y A ru p R a to n R o y

    All tuples from the left table must be in thetuples selected.

    If a tuple from the left table is not matchedwith any tuples in the right table, it will have

    null in all the fields for the right table column

    Left Outer Join

  • 8/6/2019 Database Management & Administration_Lecture 4

    25/50

    IAC, CSE, BUET 25:P re p a re d B y A ru p R a to n R o y

    All tuples from the right table must be in thetuples selected.

    If a tuple from the right table is not matchedwith any tuples in the left table, it will have

    null in all the fields for the left table column

    Right Outer Join

  • 8/6/2019 Database Management & Administration_Lecture 4

    26/50

    IAC, CSE, BUET 26:P re p a re d B y A ru p R a to n R o y

    All tuples from both the table must be in theselected tuple.

    If a tuple from the left table is not matchedwith any tuple from the right table, then it will

    have null values in the right table columnIf a tuple from the right table is not matched

    with any tuple from the left table, then it willhave null values in the left table column

    Full Outer Join

  • 8/6/2019 Database Management & Administration_Lecture 4

    27/50

    IAC, CSE, BUET 27:P re p a re d B y A ru p R a to n R o y

    Employee and Branch DataModifiedEmployee_id Name Branch_id1 Mr. A 2

    2 Mr. B 1

    3 Mr. C 1

    4 Mr. D 15 Mr. E 2

    6 Mr. F 5

    Branch_id Branch_Name1 Polashi

    2 Bokshibazar

    3 Motijheel

  • 8/6/2019 Database Management & Administration_Lecture 4

    28/50

    IAC, CSE, BUET 28:P re p a re d B y A ru p R a to n R o y

    select * from employee left outer join branchon employee.branch_id = branch.branch_id;

    select * from employee, branch whereemployee.branch_id = branch.branch_id(+);

    Left Outer Join

  • 8/6/2019 Database Management & Administration_Lecture 4

    29/50

  • 8/6/2019 Database Management & Administration_Lecture 4

    30/50

    IAC, CSE, BUET 30:P re p a re d B y A ru p R a to n R o y

    select * from employee right outer join branchon employee.branch_id = branch.branch_id;

    select * from employee, branch whereemployee.branch_id(+) = branch.branch_id;

    Right Outer Join

  • 8/6/2019 Database Management & Administration_Lecture 4

    31/50

  • 8/6/2019 Database Management & Administration_Lecture 4

    32/50

    IAC, CSE, BUET 32:P re p a re d B y A ru p R a to n R o y

    select * from employee full outer join branch onemployee.branch_id = branch.branch_id;

    Full Outer Join

  • 8/6/2019 Database Management & Administration_Lecture 4

    33/50

  • 8/6/2019 Database Management & Administration_Lecture 4

    34/50

    IAC, CSE, BUET

    34:P re p a re d B y A ru p R a to n R o y

    a query result can also be used in a conditionof a where clause.

    in such a case the query is called a subqueryand 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 eachtuple in top query

    Sub Query

  • 8/6/2019 Database Management & Administration_Lecture 4

    35/50

    IAC, CSE, BUET

    35:P re p a re d B y A ru p R a to n R o y

    A respective condition in the where clause then canhave one of the following forms: 1. Set-valued subqueries [not] in () [any|all]

    ()An can either be a column or a

    computed value. 2. Test for (non)existence [not] exists ()

    In a where clause conditions using subqueries canbe combined arbitrarily by using the logicalconnectives and and or.

    In Where Clause

  • 8/6/2019 Database Management & Administration_Lecture 4

    36/50

    IAC, CSE, BUET

    36:P re p a re d B y A ru p R a to n R o y

    Select the employees who works in PalashiBranchselect * from employee where branch_id

    in(select branch_id from branch where

    name=Palashi);Select the employees who does not work in

    Palashi Branchselect * from employee where branch_id not

    in(select branch_id from branch wherename=Palashi);

    Example

  • 8/6/2019 Database Management & Administration_Lecture 4

    37/50

    IAC, CSE, BUET

    37:P re p a re d B y A ru p R a to n R o y

    You can use (=) for checking condition, if youare sure enough that your subquery returns asingle value.

    Better, use in. Because it eliminates the

    necessity of single return value. And alsoworks for the single return value.

    Caution

  • 8/6/2019 Database Management & Administration_Lecture 4

    38/50

    IAC, CSE, BUET

    38:P re p a re d B y A ru p R a to n R o y

    Often a query result depends on whethercertain rows do (not) exist in (other) tables.Such type of queries is formulated using theexists operator.

    List all branch that have no employeesselect * from branch

    where not exists (select * from employee where branch_id = branch.branch_id);

    Exists/Not Exists

  • 8/6/2019 Database Management & Administration_Lecture 4

    39/50

    IAC, CSE, BUET

    39:P re p a re d B y A ru p R a to n R o y

    Modified Employee Table

    Employee_id Name Branch_id Salary

    1 Mr. A 2 12000

    2 Mr. B 1 140003 Mr. C 1 12000

    4 Mr. D 1 15000

    5 Mr. E 2 14000

    6 Mr. F 5 13000

  • 8/6/2019 Database Management & Administration_Lecture 4

    40/50

    IAC, CSE, BUET

    40:P re p a re d B y A ru p R a to n R o y

    For the clause any, the condition evaluatesto true if there exists at least on rowselected by the sub query for which thecomparison holds.

    If the sub query yields an empty result set, thecondition is not satisfied.

    Retrieve all employees who are working inbranch 1 and who earn at least as much as

    any employee working in department 2.select * from employee where branch_id = 1

    and salary>=any(select salary fromemployee where branch_id=2);

    Any

  • 8/6/2019 Database Management & Administration_Lecture 4

    41/50

    IAC, CSE, BUET

    41:P re p a re d B y A ru p R a to n R o y

    For the clause all, the condition evaluatesto true if for all rows selected by the subquery the comparison holds.

    In this case the condition evaluates to true if

    the sub query does not yield any row orvalue.

    select the employee with maximum salaryselect * from employee where salary

    >=all(select salary from employee);

    All

  • 8/6/2019 Database Management & Administration_Lecture 4

    42/50

    IAC, CSE, BUET

    42:P re p a re d B y A ru p R a to n R o y

    As any select query returns a temporary table,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.

    select employee_name, salary,subquery.average_salary from employee,(select avg(salary) as average_salary fromemployee)subquery;

    Subquery in From Clause

    Employee_name Salary Average_salary

  • 8/6/2019 Database Management & Administration_Lecture 4

    43/50

    IAC, CSE, BUET

    43:P re p a re d B y A ru p R a to n R o y

    Oracle provides an object called a Sequencethat can generate numeric value.

    ProvidesGenerating the number in ascending or

    descending orderThe maximum number that can be generated

    by sequenceThe increment value for generating the next

    number

    Sequence

  • 8/6/2019 Database Management & Administration_Lecture 4

    44/50

    IAC, CSE, BUET

    44:P re p a re d B y A ru p R a to n R o y

    CREATE SEQUENCE [INCREMENT BY START WITH MAXVALUE / NOMAXVALUEMINVALUE / NOMINVALUECYCLE/NOCYCLECACHE /NOCACHEORDER/NOORDER]

    Syntax

  • 8/6/2019 Database Management & Administration_Lecture 4

    45/50

    IAC, CSE, BUET

    45:P re p a re d B y A ru p R a to n R o y

    create sequence employee_id_seq Incrementedby 1 start with 1 minvalue 1 maxvalue 999cycle;

    Example

  • 8/6/2019 Database Management & Administration_Lecture 4

    46/50

    IAC, CSE, BUET

    46:P re p a re d B y A ru p R a to n R o y

    Every time nextval references a sequence itsoutput automatically incremented.

    select .NextVal from dual;

    select employee_id_seq .nextval from dual;

    Referencing a Sequence

  • 8/6/2019 Database Management & Administration_Lecture 4

    47/50

    IAC, CSE, BUET

    47:P re p a re d B y A ru p R a to n R o y

    Alter Sequence [ newValue];

    property name is any property that is used insequence parameter.

    start value cannot be alteredalter sequence employee_id_seq Incremeneted

    by 2;

    Altering a Sequence

  • 8/6/2019 Database Management & Administration_Lecture 4

    48/50

    IAC, CSE, BUET

    48:P re p a re d B y A ru p R a to n R o y

    drop sequence drop sequence employee_id_seq ;

    Dropping a Sequence

  • 8/6/2019 Database Management & Administration_Lecture 4

    49/50

    IAC, CSE, BUET

    49:P re p a re d B y A ru p R a to n R o y

    Greatest(expr1, expr2, ......, exprn)select greatest(4, 5, 17) from dual;

    Greatest can also be used to find the maxvalue row wise. [note: Max search column

    wise]select greatest(A, B) from table1;

    Greatest

    A B

    5 1025 15

    Table1

  • 8/6/2019 Database Management & Administration_Lecture 4

    50/50

    IAC, CSE, BUET:P re p a re d B y A ru p R a to n R o y

    Least(expr1, expr2, ......, exprn)select greatest(4, 5, 17) from dual;

    Least can also be used to find the min valuerow wise. [note: Min search column wise]

    select least(A, B) from table1;

    Least

    A B

    5 1025 15

    Table1