SQL Remaning Points
SQL Remaning Points
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:
In this example, there are 4 records in the customers where the customer_id value appears in the
orders table.
In this example, we have a table called products 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:
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).
In this example, we have a table called customers with the following data:
There will be 1 record deleted. Select the data from the orders table again:
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.
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:
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:
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.
Dropping Clusters
To drop a cluster use DROP CLUSTER statement. For example to drop the emp_dept cluster
give the following command.
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.
To see which tables are part of a cluster. Give the following command.
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
A foreign key with a "set null on delete" can be defined in either a CREATE TABLE statement
or an ALTER TABLE statement.
The syntax for creating a foreign key using a CREATE TABLE statement is:
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)
);
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:
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.
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:
A foreign key with a cascade delete can be defined in either a CREATE TABLE statement or an
ALTER TABLE statement.
The syntax for creating a foreign key with cascade delete using a CREATE TABLE statement in
Oracle/PLSQL is:
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:
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:
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.
The syntax for creating a foreign key with cascade delete in an ALTER TABLE statement in
Oracle/PLSQL is:
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:
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: