0% found this document useful (0 votes)
23 views

Tutorial DDB

Uploaded by

mburli ivan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Tutorial DDB

Uploaded by

mburli ivan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

TUTORIAL:DISTRIBUTED DATABASE WITH ORACLE DATABASE

EXPRESS EDITION (ORACLE DATABASE XE) AND SQL


DEVELOPER.
This guide assumes that you have installed Express Edition on your Windows; the user
interface to Oracle Database XE includes the following: System Menu Commands and
Database Home Page
The Database home page is a web browser-based interface for performing various database
administration operations, including Monitoring database storage, Monitoring database
sessions, viewing database initialization parameters, Getting started with Oracle Application
Express.
1) Creating a Database User
You should create at least one database user that you will use to create database objects. A
database user is a type of database object: a user is associated with a database schema, you
connect to the database as a database user, and the database user is the owner of any database
objects (tables and so on) that you create in the schema associated with the user.
 How to start SQLPlus* client ?
Start->All Programs->Accessories->Command Prompt
sqlplus /nolog
Step 2 How to connect a client to a database server ?
 Connect as the SYSTEM user:
CONNECT SYSTEM
 Create the user.
create user CSC400 identified by oracle;
 Grant the user the necessary privileges. For example:
SQL> grant CREATE SESSION, ALTER SESSION, CREATE DATABASE LINK, CREATE
MATERIALIZED VIEW, CREATE PROCEDURE, CREATE PUBLIC SYNONYM, CREATE
ROLE, CREATE SEQUENCE, CREATE SYNONYM, CREATE TABLE, CREATE TRIGGER,
CREATE TYPE, CREATE VIEW, UNLIMITED TABLESPACE to CSC400;
 exit SQL*Plus: SQL> exit
USING JOIN IN SQL
1. INTRODUCTION TO JOIN
Technically Joins are SQL operations which help us in retrieving data from two or more
tables that share a common field. There several Types of Joins such as Inner Join,Outer
Join, Cross Join and Self-Join.
Outer Join can further be categorize into 3 more categories:

based on join conditions, join can be either qui Joins or Non-Equi Joins.
Equi Join: this type of Join looks for common records in two tables on the basis of equality
condition and then combines them. Equi-Join is constructed with the help of equality operator
(=) where the values of Primary key and Foreign key are compared. Hence the common or
matching records from the two tables are presented in the result.
Non Equi Join: Here equality operator is not used, instead operators such as <, >,
BETWEEN etc. are employed. Therefore Non Equi-Join is the opposite of Equi-Join, and
uses joining conditions excluding equal operator. For example, in a non Equi-Join condition
you can use !=, <=,>=,<,> or BETWEEN etc. operators can be used for joining. For
implementation you can see Inner-Join.
Outer Join: Outer joins are the SQL operation which definitely return all the rows from
Source table no matter whether there is a matching join condition hit or not. At the same time
it returns only those rows of the target table that fulfils the matching join condition otherwise
just shows ‘null’ in the rows
Source table: Table which comes after FROM clause in the select statement
Target table: All the tables which come after JOIN clause in the query.
2. Natural Joins
A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on
the common columns in the two tables being joined. Common columns are columns that have
the same name in both tables.
A NATURAL JOIN can be an INNER join, a LEFT OUTER join, or a RIGHT
OUTER join. The default is INNER join.
Department table has 4 columns and Locations table has 6 columns
Desc departments ;
Desc locations;
Scenario 1: When there is only one identical name column between source and target tables.
query to find the City for all your Departments:
SELECT department_name, city FROM departments NATURAL JOIN locations;
Scenario 2: when our source and target tables have more than one identical name column.
To demonstrate this scenario we will use employees and departments table these two tables
shares two common columns which are department id and manager id. We want to see the
name of employee and the name of department in which he or she is working.
SELECT first_name, department_name FROM employees NATURAL JOIN
departments;
It can also be expanded using the following query
SELECT first_name,department_name FROM employees JOIN departments ON
(employees.manager_id = departments.manager_id AND employees.department_id =
departments.department_id);
Scenario 3: Natural join with USING clause
SELECT first_name, department_name FROM employees NATURAL JOIN
departments;
SELECT first_name, department_name FROM employees JOIN departments
USING(manager_id);
3. Right Outer Join In SQL
it takes the matching row of the left side and all the unmatched rows of the right side.

---Query 1: Right Outer Join with ‘ON’ clause


desc departments;
desc employees;
SELECT FIRST_NAME, LAST_NAME,DEPARTMENT_NAME FROM departments
RIGHT OUTER JOIN employees ON (employees.DEPARTMENT_ID =
departments.DEPARTMENT_ID);
SELECT * FROM departments RIGHT OUTER JOIN employees ON
(employees.DEPARTMENT_ID = departments.DEPARTMENT_ID);
---Query 2: Right Outer Join with WHERE clause
SELECT FIRST_NAME, LAST_NAME,DEPARTMENT_NAME FROM departments
RIGHT OUTER JOIN employees ON (employees.DEPARTMENT_ID =
departments.DEPARTMENT_ID) WHERE salary < 50000;
--Query 3: Right Outer Join with USING clause
SELECT FIRST_NAME, LAST_NAME,DEPARTMENT_NAME FROM departments
RIGHT OUTER JOIN employees USING (DEPARTMENT_ID);
SELECT * FROM departments RIGHT OUTER JOIN employees USING
(DEPARTMENT_ID);
--4. Left Outer Join In SQL
--5. Full Outer Join In SQL
--it makes a union of left and right join.
--Query 1 : Full Outer Join With ON clause
SELECT FIRST_NAME, LAST_NAME,DEPARTMENT_NAME FROM departments
FULL OUTER JOIN employees ON (employees.DEPARTMENT_ID =
departments.DEPARTMENT_ID);

---Query 2 : Full Outer Join With USING clause


SELECT FIRST_NAME, LAST_NAME,DEPARTMENT_NAME FROM departments
FULL OUTER JOIN employees USING (DEPARTMENT_ID);
---Full Outer Join With WHERE clause
SELECT FIRST_NAME, LAST_NAME,DEPARTMENT_NAME FROM departments
FULL OUTER JOIN employees USING (DEPARTMENT_ID) WHERE salary < 50000;
---Full Outer Join With ORDER BY clause
SELECT FIRST_NAME, LAST_NAME,DEPARTMENT_NAME FROM departments
FULL OUTER JOIN employees USING (DEPARTMENT_ID) WHERE salary < 50000
ORDER BY FIRST_NAME;
--If you write DESC right after the column, your result will be sorted in descending
order:
SELECT FIRST_NAME, LAST_NAME,DEPARTMENT_NAME FROM departments
FULL OUTER JOIN employees USING (DEPARTMENT_ID) WHERE salary < 50000
ORDER BY FIRST_NAME DESC;

--6. Inner Join


--Inner Join is the join which returns all those rows from both the participating tables that
satisfy the Join condition or for that matter expression of ON/USING clause.
--QUERY 1: INNER JOIN with ON clause
SELECT FIRST_NAME, LAST_NAME,DEPARTMENT_NAME FROM departments
INNER JOIN employees ON (employees.DEPARTMENT_ID =
departments.DEPARTMENT_ID);
SELECT * FROM departments INNER JOIN employees ON
(employees.DEPARTMENT_ID = departments.DEPARTMENT_ID);

--As here in this query we are using ON join condition thus we can use any column as
expression of ON clause as long as columns share same data types. The name of the column
doesn’t matter here.
SELECT * FROM departments INNER JOIN employees
ON(EMPLOYEES.EMPLOYEE_ID = departments.DEPARTMENT_ID);
--QUERY 2: INNER JOIN with USING clause.
SELECT * FROM departments INNER JOIN employees USING(DEPARTMENT_ID);
--Cross Join produces Cartesian product of the tables which are participating in the
Join queries that’s why it’s also known by the name of Cartesian Join. Generally we use
CROSS JOIN when there is no relationship between participating tables.
SELECT * FROM departments CROSS JOIN employees;
SELECT * FROM departments CROSS JOIN employees WHERE
departments.DEPARTMENT_NAME = 'IT';
CONNECTING TO A REMOTE DATABASE AND DISTRIBUTING WITH DBLINK
A client can connect directly or indirectly to a database server. A direct connection occurs
when a client connects to a server and accesses information from a database contained on that
server.
PART A : HOW TO CONNECT TO A REMOTE DB

CLIENT COMPUTER
2 SERVER COMPUTER
WITH ORACLE CLIENT WITH ORACLE DB
IP : 192.168.199.99 IP : 192.168.199.100

- set the IPaddress of each site


- from client Ping 192.168.199.100
- desactivate the firewall
- connect to sqlplus client sqlplus /nolog
- connect system@”192.168.199.100:1521//xe”; the port can be omitted unless it has
been changed
- proceed with some select and join querries
PART B: HOW TO DISTRIBUTE A DATABASE
Each computer in a network is a node that can host one or more databases. Each node in a
distributed database system can act as a client, a server, or both, depending on the
situation.
In Figure 31–2, the host for the hq database is acting as a database server when a
statement is issued against its local data (for example, the second statement in each
transaction issues a statement against the local dept table), but is acting as a client when it
issues a statement against remote data (for example, the first statement in each transaction
is issued against the remote table emp in the sales database).
To connect to the hq database and access the dept table on this database as in Figure 31–
2, one can issue the following: SELECT * FROM dept;
This query is direct because you are not accessing an object on a remote database.
to connect to the hq database but access the emp table on the remote sales database as in
Figure 31–2, you can issue the following: SELECT * FROM emp@sales;
A database link is a connection between two physical database servers that allows a client to
access them as one logical database. it is also a pointer that defines a one-way communication
path from an Oracle Database server to another database server.
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. One principal
difference among database links is the way that connections to a remote database occur. Users
access a remote database through the following types of links:
CASE STUDY: A VERY SIMPLE DISTRIBUTED DATABASE

SERVER
IP : 192.168.199.100

DBLINK1 DBLINK2

SITE 2
SITE 1 IP : 192.168.199.102
IP : 192.168.199.101 Oxford
Seattle

1) set static IP address and check the connection between server and site using the ping
command and disable the firewall
2) Configure listener on site1 and site 2
- On site 2 go to C:\oraclexe\app\oracle\product\11.2.0\server\network\ADMIN and
open the file listener.ora. and edit using site2.txt file (Create a backup of the
content of listener.ora before editing; replace localhost by your machine name as
indicated in the original listner)
- open the prompt as admin and restart the oracle listener control
$ lsnrctl stop
$ lsnrctl start
If you get error: address already in use change the port to 1522 and restart the listener
- Do the same for site 1 using file site1.txt.
3) Configure service_name on server: open the tnsname and add service name
on the server go to C:\oraclexe\app\oracle\product\11.2.0\server\network\ADMIN and
open the file tnsnames.ora. and edit using server.txt file
4) Create a user and table on site
you can either create a connection using sqldevelopper or the prompt. To connect using
prompt, proceed as follow:
To connect on site1 from server type:
- connect to sqlplus client: sqlplus /nolog
- connect system@”192.168.199.101:1521//xe
On another prompt connect to site2.
 on each site and server unlock the hr Account
$ alter user hr identified by oracle Account unlock
$ conn hr/oracle
 we will create table from hr:
On site 1 connection
$ CREATE TABLE Seattle_DATA AS SELECT employee_id,first_name,last_name, email,
phone_number, departments.DEPARTMENT_NAME, JOBS.job_title,LOCATIONS.CITY
FROM departments, employees, Locations, JOBS Where
departments.DEPARTMENT_ID=employees.DEPARTMENT_ID and
Locations.LOCATION_ID=departments.LOCATION_ID and
JOBS.JOB_ID=employees.JOB_ID and LOCATIONS.CITY='Seattle';
On site 2 connection
$ CREATE TABLE Oxford_DATA AS SELECT employee_id,first_name,last_name, email,
phone_number, departments.DEPARTMENT_NAME, JOBS.job_title,LOCATIONS.CITY
FROM departments, employees, Locations, JOBS Where
departments.DEPARTMENT_ID=employees.DEPARTMENT_ID and
Locations.LOCATION_ID=departments.LOCATION_ID and
JOBS.JOB_ID=employees.JOB_ID and LOCATIONS.CITY='Oxford';

5) Create a DBLINK on server


Connect to server using hr Account
$ CREATE DATABASE LINK LINK1 CONNECT TO HR identified by “oracle” using
‘SITE1’;
$ CREATE DATABASE LINK LINK2 CONNECT TO HR identified by “oracle” using
‘SITE2’;
6) Using query on the distributed system
$ Select * From Seattle_DATA@LINK1;
$ Select * From Oxford_DATA@LINK2;

PRACTICAL Exercise

Using the relations Department, Locations, Employees and a decentralized architecture


as illustrated in the previous figure, writte a trigger that proceed as follow:
- If an employee is inserted updated or deleted from the local side, the insertion is
automatiquely done the on the server site side.
- If an employee is inserted updated or deleted from the server site side, the
insertion is automatiquely done on specified local site side.
NB: you will create three(03) local site namely Seattle_site, Oxford_site and Toronto_site

You might also like