oracle database study notes (five)

Chapter V multi-table query

1. What is a multi-table query?
At the same time query more than one table.
To query data from different tables.

Single-table query: All contents of the queries come from the same table
the SELECT the above mentioned id, last_name, salary
from s_emp
the WHERE dept_id = 41 and salary> 1400
the Order by salary desc, the above mentioned id asc;
single-table queries can be found in the content is limited .

Multi-table query syntax:
Use a comma in the from clause tied behind more than one table to be queried
select field 1, field 2, field .... 3
from table 1 and table 2
the WHERE condition ...
the Order by field ordering rule;

All employees of the query id, last_name?
The SELECT id, last_name
form s_emp;

Query id in all sectors, name?
ID SELECT, name
from s_dept;

Requirements: Query all employees id, last_name and
id's department, name?
ID SELECT, last_name, ID, name
from s_emp, s_dept;

When the field name conflict,
need to use the table name. Field declare the column name to query
specific tables which comes from.
s_emp.id SELECT, last_name, s_dept.id, name
from s_emp, s_dept;

can also give a starting alias table.
select field
from table 1 an alias, the alias table 2 ..... 2;
select s1.id, last_name, s2.id, name
from s_emp S1, S2 s_dept;

2. Elimination of Cartesian product
Cartesian product is a result of mathematical operation multiplication set generated.

A = { 1 , 2 , 3}
B = { a , b , c}

A X B = {(. 1, A), (. 1, B), (. 1, C),
(2, A), (2, B), (2, C),
(. 3, A), (. 3, B), (. 3, C)}
3x3 =. 9 results.

Multi-table queries in the database will produce a Cartesian product.

Table 1: Table Student Name
Student ID Name
1 Joe Smith
2 John Doe
3 Zhao six

Table 2: Table student achievement
school achievement No.
1 90
2 80
3 59

Query: each student's name and achievement?

Student ID Name School No. score
1 Joe Smith 190
2 John Doe 190
3 190 Zhao six

Zhang 1 2 80
2 John Doe 280
. 3 Zhao six 280

Zhang 1 359
2 John Doe 359
. 3 Zhao six 359

Ideas:
by the conditions of the where clause to add query limit,
to eliminate invalid, no significant results.

1) Equijoins
the equal sign in the two tables associated connecting field.
Relationship: the dept_id field is equal to the employee table id column in the department table
SELECT s1.id, last_name, s2.id, name
from s_emp S1, S2 s_dept
WHERE s1.dept_id = s2.id;

2) are not equivalent connection
query: All id employees, salary and income levels?
s_emp, s_gender Correlative query

SELECT e.id, e.salary, g.name
from s_emp E, G s_gender
WHERE e.salary BETWEEN g.minSal and g.maxSal;

Table employee
ID the salary
. 1 1200
2 2400
. 3 1700

Income level table
Min Max grade name
01000 Blue Collar
1000 1500 white-collar
15,002,500 Jinling

Blue-1120001000
2240001000 blue-
3170001000 blue-

1120010001500 collar
2240010001500 collar
3170010001500 collar

1120015002000 jinling
2240015002000 jinling
317001500200 jinling

3) outer joins
a) a left outer join
query id for all employees, last_name and
id's department, name?
e.id SELECT, e.LAST_NAME, d.id, d.name
from s_emp E, D s_dept
WHERE e.dept_id = d.id;

Query id, last_name as well as all employees of
her department id, name? We requested that no department employees
also check out?
Ideas: the need for staff left outer sheet is connected to the Department table.
You can find out there is no department employee.
e.id SELECT, e.LAST_NAME, d.id, d.name
from the Join s_dept left s_emp E D
ON = e.dept_id d.id;

A left connected to the B-sheet form, can not find out the B A.

Syntax:
standard SQL: a set of standards and specifications, in any relational database
SQL commands can be executed.
.... SELECT
from Table 1 Alias 1 left [outer] join alias 2 Table 2
ON connection conditions;

b) right outer connecting
left outer Table A Table B are connected to, the equivalent of
the outer Table B Table A is connected to the right.
Syntax:
Standard the SQL:
SELECT ....
from Table 1 Alias 1 right [outer] join alias 2 Table 2
ON connection conditions;

select e.id,e.last_name,d.id,d.name
from s_emp e left join s_dept d
on e.dept_id = d.id;
等同于:
select e.id,e.last_name,d.id,d.name
from s_dept d right join s_emp e
on e.dept_id = d.id;


Xianxiang insert a data in the database:
INSERT INTO s_dept (ID, name)
values (1000, 'Teaching');
the commit;

Exercise: query for all employees of the id, last_name and
id's department, name? There is no requirement to all
department employees also check out?
Relationship: department table connected to the left outer employee table;
outer employee table is connected to the right department table;
SELECT e.id, e.LAST_NAME, d.id, d.name
from the Join s_emp left s_dept D E
ON e.dept_id = D. ID;

SELECT e.id, e.LAST_NAME, d.id, d.name
from the Join s_dept right s_emp E D
ON = e.dept_id d.id;

Oracle Features Syntax:
left outer join:
Use keyword to declare a connection relationship where two tables,
and then the (+) into the less data side.
Query ..... requested that no sector employees show up?
e.id SELECT, e.LAST_NAME, d.id, d.name
from s_emp E, D s_dept
WHERE e.dept_id d.id = (+);

Query ..... requested that all departments are displayed?
e.id SELECT, e.LAST_NAME, d.id, d.name
from s_emp E, D s_dept
WHERE e.dept_id (+) = d.id;

c) fully connected
full connection is equivalent to using both left and right outer outside connection.
Note: Oracle does not fully connected features syntax.
You must use a standard SQL (full join ... on ...) .

For example: query id for all employees, last_name, and
id correspondence department, name?
We requested that no department employees and not employees of department are displayed?
e.id SELECT, e.LAST_NAME, d.id, d.name
from the Join s_dept Full s_emp E D
ON = e.dept_id d.id;

select e.id,e.last_name,d.id,d.name
from s_dept d full join s_emp e
on e.dept_id = d.id;

Syntax:
SELECT ...
from Table 1 Alias 1 full [outer] join alias 2 Table 2
ON connection conditions;

A fully connected to B, you can not check out with no B and A, B of A.

4) Since the connection
a Table themselves and their associate.
Although it is a table, but as two tables using queries.

For example:
query id for all employees, last_name
and manager of id, last_name?

s1.id SELECT, s1.last_name, s2.id, s2.last_name
from s_emp S1, S2 s_emp
WHERE s1.manager_id = s2.id;

Question: How do I find the manager is not all employees?
How do I find all the staff no manager?

5) connected to a set of

3.Oracle database pseudo column
in an Oracle database, there is a special field, called pseudo columns.
Not a true meaning of the field.
It is not included in any one table, but appear at any time select the query
in the results.

1) rowid
used to identify the physical storage location of the current data.
For example:
SELECT ID, last_name, ROWID
from s_emp;
2) rownum
used to identify the number of pieces of data in the current table.
Note: rownum field must start with 1.
First, the result set, only the label.
ID SELECT, last_name, rownum
from s_emp;

use?
Paging query
such as:
Query table in the first ten employee data?
the above mentioned id the SELECT, last_name
from s_emp
the WHERE rownum <= 10;

inquiry 11-20 pieces of data?
Using a subquery (nested query)
Error writing:
SELECT ID, last_name
from s_emp
WHERE rownum> =. 11 and rownum <= 20 is;


Page in the web application implementation process:
1) on the front page of
the code behind to find out all the data from the database,
then all returned to the front page.
In front page by JS technology, controls the display and the hidden data.
In order to achieve the effect of paging.
Not recommended.
2)
3)


 

 

 

Guess you like

Origin www.cnblogs.com/DennySmith/p/12189191.html