Oracle PL/SQL/Table Joins/Equijoins
Содержание
Two join on clauses
SQL> CREATE TABLE emp(
2 cid NUMBER,
3 fname VARCHAR2(20) ,
4 city VARCHAR2(30) ,
5 customer_state VARCHAR2(40),
6 country_id CHAR(2) ,
7 phone VARCHAR2(25),
8 income_level VARCHAR2(30),
9 customer_credit NUMBER,
10 email VARCHAR2(30)
11 );
Table created.
SQL>
SQL> CREATE TABLE sales(
2 product_id NUMBER(6),
3 cid NUMBER,
4 promo_id NUMBER(6),
5 sold NUMBER(3),
6 amount NUMBER(10,2),
7 cost NUMBER(10,2)
8 );
Table created.
SQL>
SQL>
SQL> CREATE TABLE products(
2 product_id NUMBER(6),
3 product_name VARCHAR2(50)
4 );
Table created.
SQL>
SQL>
SQL> select c.cid, c.customer_state, s.sold, p.product_name
2 from sales s
3 join emp c on s.cid = c.cid
4 join products p on s.product_id = p.product_id
5 where p.product_id = 117
6 and c.country_id = 52790;
no rows selected
SQL>
SQL>
SQL> drop table sales;
Table dropped.
SQL> drop table emp;
Table dropped.
SQL> drop table products;
Table dropped.
SQL>
SQL>
Two Table Equi-Joins for ids
SQL>
SQL>
SQL>
SQL> CREATE TABLE emp (
2 empID INT NOT NULL PRIMARY KEY,
3 Name VARCHAR(50) NOT NULL);
Table created.
SQL> INSERT INTO emp (empID,Name) VALUES (1,"Tom");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (2,"Jack");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (3,"Mary");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (4,"Bill");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (5,"Cat");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (6,"Victor");
1 row created.
SQL>
SQL>
SQL> CREATE TABLE empExam (
2 empID INT NOT NULL,
3 ExamID INT NOT NULL,
4 Mark INT,
5 Taken SMALLINT,
6 Comments VARCHAR(255),
7 CONSTRAINT PK_empExam PRIMARY KEY (empID, ExamID));
Table created.
SQL>
SQL>
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (1,1,55,1,"Satisfactory");
1 row created.
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (1,2,73,1,"Good result");
1 row created.
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (2,3,44,1,"Hard");
1 row created.
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (2,5,39,0,"Simple");
1 row created.
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken) VALUES (2,6,63,1);
1 row created.
SQL>
SQL>
SQL> SELECT empExam.ExamID,
2 empExam.Mark,
3 emp.Name AS empName
4 FROM empExam
5 JOIN emp
6 ON empExam.empID = emp.empID
7 ORDER BY ExamID;
EXAMID MARK EMPNAME
---------- ---------- --------------------------------------------------
1 55 Tom
2 73 Tom
3 44 Jack
5 39 Jack
6 63 Jack
5 rows selected.
SQL>
SQL>
SQL> drop table empExam;
Table dropped.
SQL> drop table emp;
Table dropped.
SQL>
Two Table Equi-Joins(join on)
SQL>
SQL>
SQL> CREATE TABLE Class (
2 ClassID INT NOT NULL PRIMARY KEY,
3 CourseID INT NOT NULL,
4 InstructorID INT NOT NULL,
5 RoomID INT NOT NULL,
6 Time VARCHAR(50));
Table created.
SQL> INSERT INTO Class (ClassID,CourseID,InstructorID,RoomID,Time) VALUES (1,1,1,6,"Mon 09:00-11:00");
1 row created.
SQL> INSERT INTO Class (ClassID,CourseID,InstructorID,RoomID,Time) VALUES (2,2,1,5,"Mon 11:00-12:00, Thu 09:00-11:00");
1 row created.
SQL> INSERT INTO Class (ClassID,CourseID,InstructorID,RoomID,Time) VALUES (3,3,2,3,"Mon 14:00-16:00");
1 row created.
SQL> INSERT INTO Class (ClassID,CourseID,InstructorID,RoomID,Time) VALUES (4,4,3,2,"Tue 10:00-12:00, Thu 14:00-15:00");
1 row created.
SQL> INSERT INTO Class (ClassID,CourseID,InstructorID,RoomID,Time) VALUES (5,5,2,9,"Tue 14:00-16:00");
1 row created.
SQL> INSERT INTO Class (ClassID,CourseID,InstructorID,RoomID,Time) VALUES (6,6,3,2,"Tue 16:00-17:00, Thu 15:00-17:00");
1 row created.
SQL> INSERT INTO Class (ClassID,CourseID,InstructorID,RoomID,Time) VALUES (7,7,4,3,"Wed 09:00-11:00");
1 row created.
SQL> INSERT INTO Class (ClassID,CourseID,InstructorID,RoomID,Time) VALUES (8,8,5,8,"Wed 11:00-13:00, Fri 09:00-11:00");
1 row created.
SQL> INSERT INTO Class (ClassID,CourseID,InstructorID,RoomID,Time) VALUES (9,9,1,5,"Fri 11:00-13:00");
1 row created.
SQL> INSERT INTO Class (ClassID,CourseID,InstructorID,RoomID,Time) VALUES (10,10,6,9,"Fri 14:00-16:00");
1 row created.
SQL>
SQL> CREATE TABLE Room (
2 RoomID INT NOT NULL PRIMARY KEY,
3 Comments VARCHAR(50),
4 Capacity INT);
SQL>
SQL>
SQL> SELECT Class.ClassID, Class.Time, Room.RoomID
2 FROM Room
3 JOIN Class ON Room.RoomID = Class.RoomID;
CLASSID TIME ROOMID
---------- -------------------------------------------------- ----------
1 Mon 09:00-11:00 6
2 Mon 11:00-12:00, Thu 09:00-11:00 5
3 Mon 14:00-16:00 3
4 Tue 10:00-12:00, Thu 14:00-15:00 2
5 Tue 14:00-16:00 9
6 Tue 16:00-17:00, Thu 15:00-17:00 2
7 Wed 09:00-11:00 3
8 Wed 11:00-13:00, Fri 09:00-11:00 8
9 Fri 11:00-13:00 5
10 Fri 14:00-16:00 9
10 rows selected.
SQL>
SQL> drop table class;
Table dropped.
SQL> drop table room;
Table dropped.
Two Table Equi-Joins with where clause and order by clause
SQL>
SQL> CREATE TABLE emp (
2 empID INT NOT NULL PRIMARY KEY,
3 Name VARCHAR(50) NOT NULL);
Table created.
SQL>
SQL>
SQL> INSERT INTO emp (empID,Name) VALUES (1,"Tom");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (2,"Jack");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (3,"Mary");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (4,"Bill");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (5,"Cat");
1 row created.
SQL> INSERT INTO emp (empID,Name) VALUES (6,"Victor");
1 row created.
SQL>
SQL> CREATE TABLE empExam (
2 empID INT NOT NULL,
3 ExamID INT NOT NULL,
4 Mark INT,
5 Taken SMALLINT,
6 Comments VARCHAR(255),
7 CONSTRAINT PK_empExam PRIMARY KEY (empID, ExamID));
Table created.
SQL>
SQL>
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (1,1,55,1,"Satisfactory");
1 row created.
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (1,2,73,1,"Good result");
1 row created.
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (2,3,44,1,"Hard");
1 row created.
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken,Comments) VALUES (2,5,39,0,"Simple");
1 row created.
SQL> INSERT INTO empExam (empID,ExamID,Mark,Taken) VALUES (2,6,63,1);
1 row created.
SQL>
SQL>
SQL> SELECT empExam.ExamID,
2 empExam.Mark,
3 emp.Name AS empName
4 FROM empExam JOIN emp
5 ON empExam.empID = emp.empID
6 WHERE empExam.Mark >= 80
7 ORDER BY ExamID;
no rows selected
SQL>
SQL>
SQL>
SQL> drop table empExam;
Table dropped.
SQL> drop table emp;
Table dropped.
Working with equijoins
SQL> CREATE TABLE Employee(Employee_ID NUMBER(4) NOT NULL,
2 Employee_Name VARCHAR2(10),
3 JOB VARCHAR2(9),
4 Manager_ID NUMBER(4),
5 Start_Date DATE,
6 Salary NUMBER(7, 2),
7 COMM NUMBER(7, 2),
8 Deparment_ID NUMBER(2)
9 );
Table created.
SQL>
SQL> INSERT INTO Employee VALUES (7369, "SMITH", "CLERK", 7902, TO_DATE("17-DEC-1980", "DD-MON-YYYY"), 800, NULL, 20);
1 row created.
SQL> INSERT INTO Employee VALUES (7499, "ALLEN", "SALESMAN", 7698, TO_DATE("20-FEB-1981", "DD-MON-YYYY"), 1600, 300, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7521, "WARD", "SALESMAN", 7698, TO_DATE("22-FEB-1981", "DD-MON-YYYY"), 1250, 500, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7566, "JONES", "MANAGER", 7839, TO_DATE("2-APR-1981", "DD-MON-YYYY"), 2975, NULL, 20);
1 row created.
SQL> INSERT INTO Employee VALUES (7654, "MARTIN", "SALESMAN", 7698, TO_DATE("28-SEP-1981", "DD-MON-YYYY"), 1250, 1400, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7698, "BLAKE", "MANAGER", 7839, TO_DATE("1-MAY-1981", "DD-MON-YYYY"), 2850, NULL, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7782, "CLARK", "MANAGER", 7839, TO_DATE("9-JUN-1981", "DD-MON-YYYY"), 2450, NULL, 10);
1 row created.
SQL> INSERT INTO Employee VALUES (7788, "SCOTT", "ANALYST", 7566, TO_DATE("09-DEC-1982", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO Employee VALUES (7839, "KING", "PRESIDENT", NULL, TO_DATE("17-NOV-1981", "DD-MON-YYYY"), 5000, NULL, 10);
1 row created.
SQL> INSERT INTO Employee VALUES (7844, "TURNER", "SALESMAN", 7698, TO_DATE("8-SEP-1981", "DD-MON-YYYY"), 1500, 0, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7876, "ADAMS", "CLERK", 7788, TO_DATE("12-JAN-1983", "DD-MON-YYYY"), 1100, NULL, 20);
1 row created.
SQL> INSERT INTO Employee VALUES (7900, "JAMES", "CLERK", 7698, TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 950, NULL, 30);
1 row created.
SQL> INSERT INTO Employee VALUES (7902, "FORD", "ANALYST", 7566, TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO Employee VALUES (7934, "MILLER", "CLERK", 7782, TO_DATE("23-JAN-1982", "DD-MON-YYYY"), 1300, NULL, 10);
1 row created.
SQL>
SQL> CREATE TABLE Department(Deparment_ID NUMBER(2),
2 Department_Name VARCHAR2(14),
3 Location VARCHAR2(13)
4 );
Table created.
SQL>
SQL> INSERT INTO Department VALUES (10, "ACCOUNTING", "NEW YORK");
1 row created.
SQL> INSERT INTO Department VALUES (20, "RESEARCH", "DALLAS");
1 row created.
SQL> INSERT INTO Department VALUES (30, "SALES", "CHICAGO");
1 row created.
SQL> INSERT INTO Department VALUES (40, "OPERATIONS", "BOSTON");
1 row created.
SQL>
SQL> alter table Employee add constraint Employee_pk primary key(Employee_ID);
Table altered.
SQL> alter table Department add constraint Department_pk primary key(Deparment_ID);
Table altered.
SQL> alter table Employee add constraint Employee_fk_Department foreign key(Deparment_ID) references Department;
Table altered.
SQL> alter table Employee add constraint Employee_fk_Employee foreign key(Manager_ID) references Employee;
Table altered.
SQL>
SQL> select * from Employee;
EMPLOYEE_ID EMPLOYEE_N JOB MANAGER_ID START_DAT SALARY COMM DEPARMENT_ID
----------- ---------- --------- ---------- --------- ---------- ---------- ------------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
EMPLOYEE_ID EMPLOYEE_N JOB MANAGER_ID START_DAT SALARY COMM DEPARMENT_ID
----------- ---------- --------- ---------- --------- ---------- ---------- ------------
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL> select * from Department;
DEPARMENT_ID DEPARTMENT_NAM LOCATION
------------ -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
SQL>
SQL>
SQL> -- Working with equijoins
SQL>
SQL> SELECT Employee.Employee_ID, Employee.Start_Date,
2 Employee.Employee_Name, Department.Department_Name, Department.Location
3 FROM Employee, Department
4 WHERE Department.Deparment_ID = Employee.Deparment_ID;
EMPLOYEE_ID START_DAT EMPLOYEE_N DEPARTMENT_NAM LOCATION
----------- --------- ---------- -------------- -------------
7369 17-DEC-80 SMITH RESEARCH DALLAS
7499 20-FEB-81 ALLEN SALES CHICAGO
7521 22-FEB-81 WARD SALES CHICAGO
7566 02-APR-81 JONES RESEARCH DALLAS
7654 28-SEP-81 MARTIN SALES CHICAGO
7698 01-MAY-81 BLAKE SALES CHICAGO
7782 09-JUN-81 CLARK ACCOUNTING NEW YORK
7788 09-DEC-82 SCOTT RESEARCH DALLAS
7839 17-NOV-81 KING ACCOUNTING NEW YORK
7844 08-SEP-81 TURNER SALES CHICAGO
7876 12-JAN-83 ADAMS RESEARCH DALLAS
EMPLOYEE_ID START_DAT EMPLOYEE_N DEPARTMENT_NAM LOCATION
----------- --------- ---------- -------------- -------------
7900 03-DEC-81 JAMES SALES CHICAGO
7902 03-DEC-81 FORD RESEARCH DALLAS
7934 23-JAN-82 MILLER ACCOUNTING NEW YORK
14 rows selected.
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> drop table Department cascade constraints;
Table dropped.
SQL> drop table Employee cascade constraints;
Table dropped.
SQL>
SQL>