Unit 5
Unit 5
Database links
The central concept in distributed database systems is a database
link. A database link is a connection between two physical
database servers that allows a client to access them as one logical
database.
What Are Database Links?
A database link is a pointer that defines a one-way communication path from
an Oracle Database server to another database server. The link pointer is
actually defined as an entry in a data dictionary table. To access the link, you
must be connected to the local database that contains the data dictionary
entry.
A database link connection allows local users to access data on a remote database.
For this connection to occur, each database in the distributed system must have a
unique global database name in the network domain. The global database name
uniquely identifies a database server in a distributed system.
Database links are either private or public. If they are private, then only the user who created the
link has access; if they are public, then all database users have access.
Type of
Link Description
Connected Users connect as themselves, which means that they must have an
user link account on the remote database with the same user name and
password as their account on the local database.
Fixed user Users connect using the user name and password referenced in the
link link. For example, if Jane uses a fixed user link that connects to
the hq database with the user name and password scott/password,
then she connects as scott, Jane has all the privileges in hq granted
to scott directly, and all the default roles that scott has been granted
in the hq database.
Type of
Link Description
SQL Snapshots
SQL snapshots are a recent read-only copy of the table from the database or a
subset of rows/columns of a table. The SQL statement that creates and subsequently
maintains a snapshot normally reads data from the source database server.
To create a simple snapshot based on a single table or a straightforward SELECT query from a
single table, you can use the following syntax:
1. Simple snapshots
2. Complex snapshots
Complex snapshot
FROM student
UNION ALL
FROM new_student;
Advantages of Snapshots in SQL
• Data subsetting.
• Disconnected computing.
• Mass deployment.
Applications of Snapshots
• Protects data.
Data dictionary
A Data Dictionary comprises two words i.e. Data which simply means
information being collected through some sources and Dictionary means where
this information is available.
Keys Values
Name Passenger
Aliases None
Keys Values
Where or how it’s
Passenger’s query (input) Ticket (output)
used?
EQUI JOIN creates a JOIN for equality or matching column(s) values of the relative tables. EQUI
JOIN also create JOIN by using JOIN with ON and then providing the names of the columns with
their relative tables to check equality using equal sign (=).
Syntax :
SELECT column_list
FROM table1
JOIN table2
[ON (join_condition)]
4 Megha 2 Delhi
6 Gouri 2 Delhi
id class city
9 3 Delhi
10 2 Delhi
12 2 Delhi
SELECT student.name, student.id, record.class, record.city
FROM
student
JOIN
record
ON student.city = record.city;
name id classcity
Hina 3 3 Delhi
Megha 4 3 Delhi
Gouri 6 3 Delhi
Hina 3 2 Delhi
Megha 4 2 Delhi
Gouri 6 2 Delhi
Hina 3 2 Delhi
Megha 4 2 Delhi
Gouri 6 2 Delhi
NON EQUI JOIN performs a JOIN using comparison operator other than equal(=)
sign like >, <, >=, <= with conditions.
Syntax:
SELECT *
FROM table_name1, table_name2
WHERE table_name1.column [> | < | >= | <= ] table_name2.column;
name id city
name id city
Hina 9 Delhi
Megha 9 Delhi
Gouri 9 Delhi
Hina 10 Delhi
Megha 10 Delhi
Gouri 10 Delhi
Hina 12 Delhi
Megha 12 Delhi
Gouri 12 Delhi
OUTPUT
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
• The percent sign % represents zero, one, or multiple characters
• The underscore sign _ represents one, single character
• SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
Return all customers from a city that starts with 'L' followed by one wildcard character, then 'nE' and then
two wildcard characters:
SELECT * FROM Customers
WHERE city LIKE 'L_nE__';
Return all customers from a city that contains the letter 'L':
SELECT * FROM Customers
WHERE city LIKE '%L%';
Return all customers that starts with 'a' or starts with 'b':
ANY means that the condition will be true if the operation is true for any of the values in the range.
The following SQL statement lists the ProductName if it finds ANY records in the OrderDetails
table has Quantity equal to 10 (this will return TRUE because the Quantity column has some
values of 10):
SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
The following SQL statement lists the ProductName if it finds ANY records in
the OrderDetails table has Quantity larger than 99 (this will return TRUE
because the Quantity column has some values larger than 99):
SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM OrderDetails
WHERE Quantity > 99);
The SQL ALL Operator
The following SQL statement lists the ProductName if ALL the records in the OrderDetails table has Quantity equal to
10. This will of course return FALSE because the Quantity column has many different values (not only the value of 10):
SELECT ProductName
FROM Products
WHERE ProductID = ALL
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID =
Suppliers.supplierID AND Price < 20);
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.Suppli
erID = Suppliers.supplierID AND Price = 22);
The SQL IN Operator
Return all customers that have NOT placed any orders in the Orders table: