Tutorial DDB
Tutorial DDB
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.
--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
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';
PRACTICAL Exercise