Vous êtes sur la page 1sur 14

# EXIST and NOT EXIST

Example - Using EXISTS Condition with the SELECT


Statement
Let's start by looking at an example that shows how to use the EXISTS condition with a
SELECT statement.

In this example, we have a customers table with the following data:

customer_id last_name first_name favorite_website


4000 Jackson Joe techonthenet.com
5000 Smith Jane digminecraft.com
6000 Ferguson Samantha bigactivities.com
7000 Reynolds Allen checkyourmath.com
8000 Anderson Paige NULL
9000 Johnson Derek techonthenet.com

And a table called orders with the following data:

order_id customer_id order_date


1 7000 2016/04/18
2 5000 2016/04/18
3 8000 2016/04/19
4 4000 2016/04/20

Now let's find all of the records from the customers table where there is at least one record in the
orders table with the same customer_id. Enter the following SELECT statement:

Try It
SELECT *
FROM customers
WHERE EXISTS
(SELECT *
FROM orders
WHERE customers.customer_id = orders.customer_id);
There will be 4 records selected. These are the results that you should see:

customer_id last_name first_name favorite_website


4000 Jackson Joe techonthenet.com
5000 Smith Jane digminecraft.com
7000 Reynolds Allen checkyourmath.com
8000 Anderson Paige NULL

In this example, there are 4 records in the customers where the customer_id value appears in the
orders table.

Example - Using EXISTS Condition with the UPDATE


Statement
Let's look at an example that uses the EXISTS condition in an UPDATE statement.

In this example, we have a table called products with the following data:

product_id product_name category_id


1 Pear 50
2 Banana 50
3 Orange 50
4 Apple 50
5 Bread 75
6 Sliced Ham 25
7 Kleenex NULL

And a table called summary_data with the following data:

product_id current_category
1 10
2 10
3 10
4 10
5 10

Now let's update the summary_data table with values from the products table. Enter the
following SQL statement:

UPDATE summary_data
SET current_category = (SELECT category_id
FROM products
WHERE products.product_id = summary_data.product_id)
WHERE EXISTS (SELECT category_id
FROM products
WHERE products.product_id = summary_data.product_id);

There will be 5 records update. Select the data from the summary_data table again:

SELECT * FROM summary_data;

These are the results that you should see:

product_id current_category
1 50
2 50
3 50
4 50
5 75
8

This example would update the current_category field in the summary_data table with the
category_id from the products table where the product_id values match. The first 5 records in
the summary_data table have been updated.

TIP: If we hadn't included the EXISTS condition, the UPDATE query would have updated the
current_category field to NULL in the 6th row of the summary_data table (because the products
table does not have a record where product_id=8).

Example - Using EXISTS Condition with the DELETE


Statement
Let's look at an example that uses the EXISTS condition in a DELETE statement.

In this example, we have a table called customers with the following data:

customer_id last_name first_name favorite_website


4000 Jackson Joe techonthenet.com
5000 Smith Jane digminecraft.com
6000 Ferguson Samantha bigactivities.com
7000 Reynolds Allen checkyourmath.com
8000 Anderson Paige NULL
9000 Johnson Derek techonthenet.com
And a table called orders with the following data:
order_id customer_id order_date
1 7000 2016/04/18
2 5000 2016/04/18
3 8000 2016/04/19
4 4000 2016/04/20
5 NULL 2016/05/01
Enter the following DELETE statement:
DELETE FROM orders
WHERE EXISTS
(SELECT *
FROM customers
WHERE customers.customer_id = orders.customer_id
AND customers.last_name = 'Jackson');

There will be 1 record deleted. Select the data from the orders table again:

SELECT * FROM orders;

These are the results that you should see:

order_id customer_id order_date


1 7000 2016/04/18
2 5000 2016/04/18
3 8000 2016/04/19
5 NULL 2016/05/01

This example would delete all records from the orders table where there is a record in the
customers table with the last_name of 'Jackson' and a matching customer_id value in both tables.
In this example, the record for order_id=4 was deleted.

If you want to determine the number of rows that will be deleted, you can run the following
SELECT statement before performing the delete.

SELECT COUNT(*) FROM orders


WHERE EXISTS
(SELECT *
FROM customers
WHERE customers.customer_id = orders.customer_id
AND customers.last_name = 'Jackson');

This will return number of records that will be deleted when you execute the DELETE statement.

COUNT(*)
1
Example - Using NOT with the EXISTS Condition
Finally, the NOT condition can be combined with the EXISTS condition to create a NOT
EXISTS condition. Let's look at an example that shows how to use the NOT EXISTS condition
in SQL.

In this example, we have a table called customers with the following data:

customer_id last_name first_name favorite_website


4000 Jackson Joe techonthenet.com
5000 Smith Jane digminecraft.com
6000 Ferguson Samantha bigactivities.com
7000 Reynolds Allen checkyourmath.com
8000 Anderson Paige NULL
9000 Johnson Derek techonthenet.com

And a table called orders with the following data:

order_id customer_id order_date


1 7000 2016/04/18
2 5000 2016/04/18
3 8000 2016/04/19
4 4000 2016/04/20
5 NULL 2016/05/01

Enter the following SQL statement:

Try It
SELECT *
FROM customers
WHERE NOT EXISTS
(SELECT *
FROM orders
WHERE customers.customer_id = orders.customer_id);

There will be 2 records selected. These are the results that you should see:

customer_id last_name first_name favorite_website


6000 Ferguson Samantha bigactivities.com
9000 Johnson Derek techonthenet.com

This example would return all records from the customers table where there are no records in the
orders table for the given customer_id.
# CLUSTERS
Clustering is an important concept for improving Oracle performance. Whenever a database is
accessed, any reduction in i/o always helps in improving it’s throughput and overall
performance. The concept of cluster is where member records are stored physically near parent
records.

Clusters are used to store data from different tables in the same physical data blocks. They are
appropriate to use if the records from those tables are frequently queried together. By storing
them in the same data blocks, the number of database block reads needed to fulfill such queries
decreases, thereby improving the performance.

If you two are more tables are joined together on a single column and most of the time you issue
join queries on them, then consider creating a cluster of these tables.

A cluster is a group tables that share the same data blocks i.e. all the tables are physically stored
together.

For example EMP and DEPT table are joined on DEPTNO column. If you cluster them, Oracle
physically stores all rows for each department from both the emp and dept tables in the same
data blocks.

• Since cluster stores related rows of different tables in same data blocks, Disk I/O is
reduced and access time improves for joins of clustered tables.

• Each cluster key value is stored only once each in the cluster and the cluster index, no
matter how many rows of different tables contain the value.

Therefore, less storage might be required to store related table and index data in a cluster than is
necessary in non-clustered table format.

CREATING A CLUSTER
To create clustered tables. First, create a cluster and create index on it. Then create tables in it.

For example to create a cluster of EMP and DEPT tables in which the DEPTNO will be cluster
key, first create the cluster by typing the following command.

create cluster emp_dept (deptno number(2));

Then create index on it.

create index on cluster emp_dept;

Now create table in the cluster like this


create table dept (deptno number(2),
name varchar2(20),
loc varchar2(20))
cluster emp_dept (deptno);

create table emp (empno number(5),


name varchar2(20),
sal number(10,2),
deptno number(2)) cluster emp_dept (deptno)

Dropping Clusters
To drop a cluster use DROP CLUSTER statement. For example to drop the emp_dept cluster
give the following command.

drop cluster emp_dept;

This will drop the cluster, if the cluster is empty i.e. no tables are existing it it. If tables are there
in the cluster first drop the tables and then drop the cluster. If you want to drop the cluster even
when tables are there then give the following command.

drop cluster emp_dept including tables;

Listing Information about Clusters


To see how many clusters are there in your schema give the following statement.

select * from user_clusters;

To see which tables are part of a cluster. Give the following command.

select * from tab

TABLE_NAME TYPE CLUSTER_ID


---------- ---- -----------
EMP TABLE 1
SALGRADE TABLE
CUSTOMER TABLE
DEPT TABLE 1

In the above example notice the CLUSTER_ID column, for EMP and DEPT table the cluster_id
is 1. That means these tables are in cluster whose cluster_id is 1. You can see the cluster_id’s
name in USER_CLUSTERS table.
# Data Query Language (DQL)

The commands of SQL that are used to retrieve data from the database are collectively called as
DQL. So all Select statements comes under DQL.

DQL commands are basically SELECT statements. SELECT statements let you query the
database to find information in one or more tables, and return the query as a result set. A result
set is an array structure; or more precisely, a result set is a two-dimensional array. The internal
index for each row of data is the rowid pseudo-column, which maps to the physical address for
where the data is written.

Select

To retreive data from the database table.


# Foreign Keys with Set Null on Delete
This Oracle tutorial explains how to use Foreign Keys with "set null on delete" in Oracle with
syntax and examples.

What is a foreign key with "Set NULL on Delete" in Oracle?


A foreign key with "set null on delete" means that if a record in the parent table is deleted, then
the corresponding records in the child table will have the foreign key fields set to null. The
records in the child table will not be deleted.

A foreign key with a "set null on delete" can be defined in either a CREATE TABLE statement
or an ALTER TABLE statement.

Using a CREATE TABLE statement


Syntax

The syntax for creating a foreign key using a CREATE TABLE statement is:

CREATE TABLE table_name


(
column1 datatype null/not null,
column2 datatype null/not null,
...

CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE SET NULL
);

Example
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);

CREATE TABLE products


( product_id numeric(10) not null,
supplier_id numeric(10),
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE SET NULL
);
In this example, we've created a primary key on the supplier table called supplier_pk. It consists
of only one field - the supplier_id field. Then we've created a foreign key called fk_supplier on
the products table that references the supplier table based on the supplier_id field.

Because of the set null on delete, when a record in the supplier table is deleted, all corresponding
records in the products table will have the supplier_id values set to null.

We could also create a foreign key "set null on delete" with more than one field as in the
example below:

CREATE TABLE supplier


( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id, supplier_name)
);

CREATE TABLE products


( product_id numeric(10) not null,
supplier_id numeric(10),
supplier_name varchar2(50),
CONSTRAINT fk_supplier_comp
FOREIGN KEY (supplier_id, supplier_name)
REFERENCES supplier(supplier_id, supplier_name)
ON DELETE SET NULL
);

In this example, our foreign key called fk_foreign_comp references the supplier table based on
two fields - the supplier_id and supplier_name fields.

The delete on the foreign key called fk_foreign_comp causes all corresponding records in the
products table to have the supplier_id and supplier_name fields set to null when a record in the
supplier table is deleted, based on supplier_id and supplier_name.

Using an ALTER TABLE statement


Syntax
The syntax for creating a foreign key in an ALTER TABLE statement is:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE SET NULL;

Example
ALTER TABLE products
ADD CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE SET NULL;
In this example, we've created a foreign key "with a set null on delete" called fk_supplier that
references the supplier table based on the supplier_id field.

We could also create a foreign key "with a set null on delete" with more than one field as in the
example below:

ALTER TABLE products


ADD CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id, supplier_name)
REFERENCES supplier(supplier_id, supplier_name)
ON DELETE SET NULL;

# Foreign Keys with Cascade Delete


This Oracle tutorial explains how to use Foreign Keys with cascade delete in Oracle with
syntax and examples.

What is a foreign key with Cascade DELETE in Oracle?


A foreign key with cascade delete means that if a record in the parent table is deleted, then the
corresponding records in the child table will automatically be deleted. This is called a cascade
delete in Oracle.

A foreign key with a cascade delete can be defined in either a CREATE TABLE statement or an
ALTER TABLE statement.

Using a CREATE TABLE statement


Syntax

The syntax for creating a foreign key with cascade delete using a CREATE TABLE statement in
Oracle/PLSQL is:

CREATE TABLE table_name


(
column1 datatype null/not null,
column2 datatype null/not null,
...

CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE
);
Example

Let's look at an example of how to create a foreign key with cascade delete using the CREATE
TABLE statement in Oracle/PLSQL.

For example:

CREATE TABLE supplier


( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);

CREATE TABLE products


( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE CASCADE
);

In this example, we've created a primary key on the supplier table called supplier_pk. It consists
of only one field - the supplier_id field. Then we've created a foreign key called fk_supplier on
the products table that references the supplier table based on the supplier_id field.

Because of the cascade delete, when a record in the supplier table is deleted, all records in the
products table will also be deleted that have the same supplier_id value.

We could also create a foreign key (with a cascade delete) with more than one field as in the
example below:

CREATE TABLE supplier


( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id, supplier_name)
);

CREATE TABLE products


( product_id numeric(10) not null,
supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
CONSTRAINT fk_supplier_comp
FOREIGN KEY (supplier_id, supplier_name)
REFERENCES supplier(supplier_id, supplier_name)
ON DELETE CASCADE
);

In this example, our foreign key called fk_foreign_comp references the supplier table based on
two fields - the supplier_id and supplier_name fields.
The cascade delete on the foreign key called fk_foreign_comp causes all corresponding records
in the products table to be cascade deleted when a record in the supplier table is deleted, based
on supplier_id and supplier_name.

Using an ALTER TABLE statement


Syntax

The syntax for creating a foreign key with cascade delete in an ALTER TABLE statement in
Oracle/PLSQL is:

ALTER TABLE table_name


ADD CONSTRAINT constraint_name
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE;

Example

Let's look at an example of how to create a foreign key with cascade delete using the ALTER
TABLE statement in Oracle/PLSQL.

For example:

ALTER TABLE products


ADD CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE CASCADE;

In this example, we've created a foreign key (with a cascade delete) called fk_supplier that
references the supplier table based on the supplier_id field.

We could also create a foreign key (with a cascade delete) with more than one field as in the
example below:

ALTER TABLE products


ADD CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id, supplier_name)
REFERENCES supplier(supplier_id, supplier_name)
ON DELETE CASCADE;
# Difference between Difference between inner join and
equi join and natural join.
1. Inner join can have equality (=) and other operators (like <,>,<>) in the join condition.
2. Equi join only have equality (=) operator in the join condition.
3. Equi join can be an Inner join, Left Outer join, Right Outer join.
4. The USING clause is not supported by SQL Server and Sybase. This clause is supported by
Oracle and MySQL.
5. In Natural join, you can't see what columns from both the tables will be used in the join. In
Natural join, you might not get the desired result what you are expecting.
6. Natural join clause is not supported by SQL Server, it is supported by Oracle and MySQL.

Vous aimerez peut-être aussi