Download Session 5

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Clusterpoint wikipedia , lookup

Relational algebra wikipedia , lookup

Ingres (database) wikipedia , lookup

Functional Database Model wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Oracle Database wikipedia , lookup

Relational model wikipedia , lookup

Join (SQL) wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Database model wikipedia , lookup

Transcript
Oracle - Practical 5.
Joins for Beginners
Equi-Joins, Table Aliases, Non-Equi Joins, UNION, UNION ALL, INTERSECT, MINUS.
Also have some more information on the Data Dictionary and Indexes.
Joins
You use a join when you want to retrieve data from more than one table.
Rows in one table may be joined to rows in another according to common values existing in
corresponding columns, that is to say Primary and Foreign Key columns. There are two main
types of Join Condition:
 Equi-join.
 Non Equi-join.
There are other methods of Joining tables, we will get onto these some other day hopefully:
 Outer Joins.
 Self Joins.
 Set Operators.
Product:
A product is the result of more than one table being accessed with no Join condition.
SELECT
FROM
*
weather, region;
When a join condition is omitted completely, the result is a PRODUCT, and all combinations
of rows will be displayed. All rows in the first table are joined to all rows in the second table.
The result is usually of no use.
Equi-Join
In the case of Region and Weather, the join columns are CITY, (REGION.city and
WEATHER.city)
SELECT
FROM
WHERE
region.city,
region.province,
weather.temperature
region, weather
region.city = weather.city;
You can also apply conditions in the usual way…
SELECT
FROM
WHERE
AND
region.city,
region.province,
weather.temperature
region, weather
region.city = weather.city
region.city != ‘CORK’;
Notice that the join condition specifies table names prior to the column name. This is a
requirement when column names are the same in both tables. It is necessary to tell Oracle
exactly which column you are referring to.
Note: If you are joining two tables, you have one join condition, three tables, two join
conditions four tables, three join conditions. This is roughly true. Sometimes however, you
may have to link through several tables to get at the data you want.
Page 1 of 6
Oracle - Practical 5.
Table Aliases
To avoid this huge repetition in typing, the following method can be used to create aliases for
the table names:
SELECT
FROM
WHERE
AND
R.city,
R.province,
W.temperature
region
R,
Weather
W
R.city = W.city
R.city != ‘CORK’;
By placing the letters R and W after the table names within the FROM clause, Oracle will
recognise R and W as aliases for the table names within the SELECT clause.
Non-Equi Join
Any join condition where you don’t use an ‘equals’ sign. That is a bit general. A good example
would be selecting a value from one table based on whether it is between values given in
another table.
UNION, UNION ALL
Go back to Junior Cert maths and try to remember this stuff from ‘Set Theory’. UNION
combines two queries. It returns all distinct rows for both select statements, or, when ALL is
specified, all rows regardless of duplication.
SELECT
FROM
UNION
SELECT
FROM
R.city
region
R
W.city
weather
W;
SELECT
FROM
UNION ALL
SELECT
FROM
R.city
region
R
W.city
weather
W;
And
The number of columns and Data Types must be identical between SELECT statements,
although the names of columns need not be.
SELECT
FROM
UNION
SELECT
FROM
W.city
weather
W
R.province
region
R;
Page 2 of 6
Oracle - Practical 5.
UNION does not need to apply to just two queries, you can have several queries linked by the
UNION clause.
SELECT
FROM
UNION
SELECT
FROM
UNION
SELECT
FROM
W.city
weather
W
R.province
region
R
R.city
region
R;
Using ORDER BY requires some special attention, you may remember we came across
something similar when dealing with String functions. Oracle ignores the column names in
combining two SELECT statements, so column names cannot be used in ORDER BY. Take
the following example.
SELECT
FROM
UNION
SELECT
FROM
ORDER BY
W.city,
W.outlook
weather
R.city,
R.province
region
W.outlook;
W
R
The column name isn’t common to both examples, so the order by will generate an error.
Instead specify the data location within the query by number.
ORDER BY
2
INTERSECT
INTERSECT combines two queries and returns only those rows from the first SELECT
statement that are identical to at least one row from the second SELECT statement. Again the
number of columns and data types must be identical between the statements, although the
names of columns need not be.
SELECT
FROM
INTERSECT
SELECT
FROM
R.city
region
R
W.city
weather
W;
To get a better idea of the INTERSECT statement, we will now add some rows to the region
table making sure they do not have corresponding rows in the weather table:
INSERT INTO region (
city,
province)
VALUES (
‘Knock’,
‘CONNAUGHT’);
COMMIT;
Try the last query again.
Page 3 of 6
Oracle - Practical 5.
MINUS
MINUS combines two queries. It returns only those rows from the first select statement that
are not produced by the second select statement. (The first select statement minus the
second select statement).
SELECT
FROM
MINUS
SELECT
FROM
R.city
region
R
W.city
weather
W;
Oracle Data Dictionary
“The data dictionary is a comprehensive set of tables and views owned by the DBA users
SYS and SYSTEM, which activates when Oracle is initially installed, and is a central source of
information for the Oracle RDBMS itself and for all users of Oracle. The tables are
automatically maintained by Oracle, and hold a set of views and tables containing information
about database objects, users, privileges, events and use.”
A VIEW is a database object that is a logical representation of a table. It is derived from a
table but has no storage of it’s own and often may be used in the same manner as a table.
SYS and SYSTEM are user accounts created when Oracle is installed. From these accounts
which hold all the power within the database, other users can be created with derivatives of
their power. (When I say power, I mean the ability to do things as in Create users, Create
objects (eg. Tables), insert data, select data and so on…).
Example:
DBA Accounts Sys and System
Owns DBA Tables and Views
User 1
User 2
User 3
Has Tables
and other
objects
Can see
some of
User 1’s
objects and
select from
them
Has some
objects and
can see User
1’s objects.
The above diagram is an example of a typical Oracle scenario. Oracle is installed and
automatically the SYS and SYSTEM accounts are created. They have access to all Data
Dictionary tables such as ALL_TABLES.
Page 4 of 6
Oracle - Practical 5.
A DBA then creates other users. User 1 has been created with the power to create objects
such as Tables.
User 2 has no objects of it’s own but can see the objects owned by User 1, and manipulate
the data in Users 1’s tables.
User 3 has some objects of it’s own, and can also see some of User 1’s objects.
Another name for a user’s account is a Schema. Note that in practice when you talk about
Schema’s you are talking about the objects that exist within a user’s account and how they
relate to each other.
An Instance is everything required for Oracle to run: Background processes (programs),
Memory, and so on. An Instance is the means of accessing a database. For example, in our
case the instance we are dealing with is the server on which the Oracle Database server is
installed. ORABIS.UCC.IE.
Objects
Describe the all_objects table. This, like ‘all_tables’ is part of the Oracle Data Dictionary.
Carry out the following query:
SELECT
FROM
DISTINCT object_type
all_objects;
This will allow you to see all the different types of objects to which you have some access.
Take the complete list and find out what each one is before next week.
Other queries on the all_objects table will let you know what the names of the objects are.
Figure it out.
DICTIONARY and DICT COLUMNS
These are the road maps to the Data Dictionary. Describe them. Note that the table
DICTIONARY has a public synonym ‘DICT’. DICT contains all table names (relevant to the
Data Dictionary) and explanations for those tables.
COLUMN comments
SELECT
FROM
WHERE
FORMAT A35 word_wrapped
table_name, comments
dict
table_name like ‘%TABLE%’;
CLEAR COLUMNS
This report will yield all entries from the Dict table that have to do with other tables, which
have the word ‘TABLE’ in their name. If you are dealing with other objects such as Indexes,
substitute INDEX for TABLE within the query, and the report will tell you which Data
Dictionary tables to query regarding Indexes.
Usually table names in the DICT table will have one of three prefixes. ALL_, USER_ and
DBA_. Records in the USER_ tables usually record information about objects owned by the
account performing the query. Records in ALL_ tables include USER_ records plus
information about objects on which the user has some access to. DBA_ views encompass all
of the database objects, regardless of owner.
Page 5 of 6
Oracle - Practical 5.
DICT_COLUMNS is similar to DICT but has comments on the contents of Data Dictionary
columns within Data Dictionary tables. So more information is available. The usual path of
investigation when you have a problem is as follows.
Example: I have doubts about my indexes. Where can I get information from the Data
Dictionary regarding Indexes?
SELECT
FROM
WHERE
table_name, comments
dict
table_name like ‘%INDEX%’;
USER_INDEXES seems an appropriate place for finding out about my own indexes. What
data does it contain.
SELECT
FROM
WHERE
column_name, comments
dict_columns
table_name = ‘USER_INDEXES’;
The status column looks a likely place to check out my Indexes.
SELECT
FROM
index_name,
status
user_indexes;
What is an INDEX by the way?
Index is a general term for an Oracle/SQL feature, used primarily to speed execution and
(sometimes) impose uniqueness upon certain data. Indexes provide a faster access method
to one table’s data than doing a ‘full table scan’. So instead of a query having to search all of
the data within a table, the presence of an Index tells Oracle exactly where the data is that
matches the query. Same function as the index in a book.
Because Primary Keys uniquely identify each row in a table, Primary Keys automatically
create Indexes on the same columns within Oracle. This is because, when you want to
retrieve data from a table, you will usually have a condition on your data, which attempts to
narrow the results. (The WHERE clause). What better way to narrow your results than with a
condition on the column(s) that contain the unique identifier for each record. However this
may not always be true, in that your ‘where’ clauses could often refer to some other column in
the table, which is not the PK. In this case you can create an Index specifically on that
column.
Foreign Keys do not have INDEXES automatically created upon them.
What is a foreign key?
A Foreign key is a column that defines how tables relate to each other and are created to
enforce relational database design.
Page 6 of 6