Day Wise Notes-1
Day Wise Notes-1
What is Data?
Collection of Raw material/facts/information/entities/attributes/values
Types Of Data
Structured data in structure manner/primitive/
Unstructured data format is not limitations
images-soundfiles-video-files-m
Demerits
Data Management not good for large data sets-ba
Data Redundancy duplicacy
Data Inconsistency scattered data
Data Integrity accurate data in fasten way
Data Atomicity
DBMS
Software--->User---->Create+Read+Update+Delete(CRUD)---->Database
Minimal Data Redundancy
Improved Data Consistency
Data Sharing
Enforced standards
Retrival of data is fast
ACID Rules
Atomicity-table is in atomic/ consistent state
Consistency-failure of data is there then all the inconsistent data is removed from db
Isolation-two or more transactions on the db
Durability-effect of the change is made in db
DBMS Architecture
3-levels
Physical Level 1-tier
Conceptual Level 2-tier
Application level 3-tier
Data Modeling
“A data model is a way of finding the tools for both
business and IT professionals, which uses a set of
symbols and text to precisely explain a subset of real
information to improve communication within the
organization and thereby lead to a more flexible and
stable application environment”
Keys
Super Keys
Primary Key
Unique Key
manner/primitive/rows-columns/rdbms/
ot limitations
ndfiles-video-files-mp3/mp4-
{
emp-id : 101,
emp-
name:'suhas'
} account
ta in fasten way
emp-id
101
102
removed from db
Airline Reservation Sy
physical storage structure of data in db Entities
entier database conceptually Passengers
View the database Airlines
Prices of Ticket
Flight
Studs---->Course Colleges----->University
Auth<--->Books Prod----->Orders
Emp-id Emp-naEmp-adEmp-dessal
10001
ssengers
ces of Ticket
datatypes
Data Type SQL
1 VARCHAR2(size [BYTE | CHAR]) DDL
1 NVARCHAR2(size) DML
2 NUMBER[(precision [, scale]]) 69.85 TCL
8 LONG 60000
12 DATE
21 BINARY_FLOAT
22 BINARY_DOUBLE
23 RAW(size)
24 LONG RAW
69 ROWID
96 CHAR [(size [BYTE | CHAR])]
96 NCHAR[(size)]
112 CLOB
112 NCLOB
113 BLOB
114 BFILE
180 TIMESTAMP [(fractional_seconds)]
181 TIMESTAMP [(fractional_seconds)] WITH TIME ZONE
182 INTERVAL YEAR [(year_precision)] TO MONTH
183 INTERVAL DAY [(day_precision)] TO SECOND[(fractional_seconds)]
208 UROWID [(size)]
231 TIMESTAMP [(fractional_seconds)] WITH LOCAL TIMEZONE
DDL Create table,Alter table,DROP,
DML Insert data into table
Update data Table DROP Table table name
Delete DROP Column alter table tablename drop column c
Truncate,where Contraints alter table tablename drop constrain
Multi Row Group By and Having clause
Joins and Subqueries
Delete Rollback not undo the structure of the table it is used w
Order By
Group By
avg(sal) Having
min(sal)
max(sal)
le table name delete complete structure of the table along with the data
e tablename drop column columnname
e tablename drop constraint contraintname
ure of the table it is used with to get the undo the data part only.
Constraints
Primary key – Explain the primary key concept and show you how to use the primary key constraint to manage the primary
Foreign key – Introduce you to the foreign key concept and show you use the foreign key constraint to enforce the relations
NOT NULL constraint – Show you how to ensure a column that does not accept null values.
UNIQUE constraint – Discuss how to ensure data stored in a column or a group of columns is unique among rows within a ta
CHECK constraint – Walk you through the process of adding logic for checking data before storing them in tables.
to manage the primary key of a table.
em in tables.
Day-4
Revise-DDL-DML-DCL
Rename-Alias
Difference between Truncate and Delete
Joins and Subqueries
TCL-commit Rollback Savepoint
Task - Case Study
DDL
R1 R2
A B C C D E
1 2 3 3 4 9
2 1 4 2 1 2
4 5 7
6 4 7
Here, SID and CID are Composite Primary Keys select Ename Emp,Dept where Emp.eno = Dept.eno and Emp.Add = D
Matching values
Right OuterEname DeptNo. DeptNo. Dname Loc
Varun D1 D1 IT Delhi
ENo. Amrit D2 D2 HR Hyderabad
E1 Ravi D3 D3 Finance Pune
E2 D4 Testing Bangalore
E3
Joins : To get the data from both the tables
Cross Join/ Cartesian Product Basic rule of joins is: cartesian produc
Natural/ Inner Join
Conditional Join
Equi
Self Left
Outer Right
Full
ould be same
R1 x R2
How many columns are there in both the tables? A B C
3+2 5 Columns 1 2 3
3x2 6 Rows 1 2 3
2 1 4
M+N 2 1 4
R1xR2 4 5 7
4 5 7
6 4 7
6 4 7
Select Ename, DeptNo. from Emp,Dept where emp.eno = dept.eno select Ename, DeptNo Emp
Employee Department
Eno. Ename DeptNo. Eno. Eno. Ename
1 Ram D1 1 1 Ram
1 Ram D2 2 2 Varun
1 Ram D3 4 4 Amrit
2 Varun D1 1
2 Varun D2 2
2 Varun D3 4
3 Ravi D1 1
3 Ravi D2 2
3 Ravi D3 4
4 Amrit D1 1
4 Amrit D2 2
4 Amrit D3 4
pt where Emp.eno = Dept.eno and Emp.Add = Dept.Location;
he left table select eno,ename,dname,loc from emp,dept left outer join on (Emp.deptno = dept.deptno)
C D E
3 4 9
2 1 2
3 4 9
2 1 2
3 4 9
2 1 2
3 4 9
2 1 2
DeptNo.
D1
D2
D3
o = dept.deptno)
Subquery Query within a query
2) Write a query to display name of the employee who has the maximum salalry
select Ename from Emp where Salar(select max(salary) from Emp); Varun
50000
3) Write a query to display second highest salary
select max(salary) from emp where Salary IN (select salary from emp where Salary <> (select max(s
select * from emp where exists (Select * from dept where dept.EID = emp.EID);
WHERE clause
Most often, the subquery will be found in the WHERE clause. These subqueries are also called nested subqueries.
FROM clause
A subquery can also be found in the FROM clause. These are called inline views.
Correlated Subquery :
Correlated Query is nothing but the subquery whose output is depending on the inner query used
Correlated query is the query which is executed after the outer query is executed.
The outer query is always dependent on inner query.
The approach of the correlated subquery is bit different than normal subqueries.
In normal subqueries the inner queries are executed first and then the outer query is executed
But in Correlated Subquery outer query is always dependent on inner query so first outer query is e
Correlated Subqueries always uses operator like Exist,Not
“Correlated Queries are also called as Synchronized queries…”
Nested Subqueries:
The Subqueries are called as nested subqueries when another subquery is used in where or having condition of the Outer Que
The Execution of Nested suubquery always follows bottom up approach.
Real Life Example:
Select * from Employee
where Employee_No Exist
(Select * from Employee
where Department_Name=
(Select Department_Name from Employee where Department_Name=’OBIEE’));
Execution of Query:
Step 1:
Executed Bottom query:
Select Department_Name from Employee where Department_Name=’OBIEE’;
Step 2:
Executed The Second Query which is above bottom query:
Select * from Employee
where Department_Name=’OBIEE’;
Step 3:
Excecuted the Top Query
Select * from Employee
where Employee_No Exist
(Select * from Employee
where Department_Name=’OBIEE’);
Scalar Sub-queries :
Definition of Scalar Subquery:
A scalar sub-query expression is a sub-query that returns exactly one column value from one row.
What if the oracle failed to return scalar sub-query?There are some specific conditions.
Usages of Scalar Query :
1.The scalar sub-queries are most used for removing the outer joins.
2.If user want to aggregate multiple tables then scalar sub-queries are useful.
3.Table insertion based on other table values.
Real Life Example:
If user want to find out the Department_name and Number_of_Departments using scalar query you can use following express
Select D.Department_name(Select Count(E.Department_name) From Employee E Where E.Department_no=D.De
ary <> (select max(salary) from emp));
lause cannot be added into a subquery. You can use an ORDER BY clause in the main SELECT statement (oute
query will not return any rows when using certain comparison operators in a WHERE clause
nested subqueries.
Besides a table, you can use a subquery as shown in the following example:
SELECT
column_list
FROM
(
SELECT
*
FROM
table_name
) t;
The subquery specified in the FROM clause of a query is called an inline view.
Limitations
Oracle allows an unlimited number of subqueries in the FROM clause.
of a SELECT statement.
query data.
ine view.
schemaname.S
Creating Other Schema Objects tudents
Logical
Physical
Username-ORA1
Grant all privileges to ORA1 ORACLE
View
Index Students-View1-
along with courses
Sequences
Synonyms
Students-View2-
along with courses
RollNO Sub
schemaname.S
tudents
RollNO Sub Marks
Admno Course
Username-Priyanka
ORACLE
2KB
Students-View2-
along with courses Students-View3-
along with marks RollNo Sub1 Sub2 Marks-1 Marks-2
Sub2 Marks-1 Marks-2
What is a VIEW in Oracle?
An Oracle VIEW, in essence, is a virtual table that does not physically exist.
a view is a “virtual” table whose data is the result of a stored query, which is derived each time when you q
A view is a virtual table because you can use it like a table in your SQL queries.
Every view has columns with data types so you can execute a query against views
or manage their contents (with some restrictions) using the INSERT, UPDATE, DELETE, and MERGE stateme
Unlike a table, a view does not store any data.
View only behaves like a table.
And it is just a named query stored in the database.
When you query data from a view, Oracle uses this stored query to retrieve the data from the underlying ta
You can use views in many cases for different purposes. The most common uses of views are as follows:
Creating a view – use the CREATE VIEW statement to create a new view.
Drop a view – use the DROP VIEW statement to drop a view from the database.
Updatable views – discuss how to create updatable views.
Inline views – learn how to use inline views to simplify complex queries and condense several separate q
WITH CHECK OPTION – how to protect the view using the WITH CHECK OPTION clause of the CREATE VIEW
Materialized view – covers the materialized views that help you improve query response times.
ach time when you query against the view.
ws are as follows:
When you create a new table with a primary key, Oracle automatically creates a new index for the primary key colu
Unlike other database systems, Oracle does not automatically create an index for the foreign key columns.
CREATE INDEX
SQL> CREATE IND
CREATE INDEX ename_index ON Employees(first_name);
Index created.
Removing an index SQL> EXPLAIN PL
To remove an index, you use the DROP INDEX statement: 2 SELECT * FRO
DROP INDEX index_name; 3 WHERE first_na
Explained.
Creating an index on multiple columns example This explains the exe
CREATE INDEX emp_name_indx1 ON employees(last_name,first_name);
SQL> SELECT
2 PLAN_TABL
3 FROM
4 TABLE(DBM
PLAN_TABLE_OU
-------------------------
| 0 | SELECT STAT
00:00:01 |
| 1 | TABLE ACCE
00:00:01 |
|* 2 | INDEX RAN
00:00:01 |
use Oracle indexes to speed up your queries.
ex for one or more columns in a table.
r columns.
w cardinality.
Index created.
Explained.
SQL> SELECT
2 PLAN_TABLE_OUTPUT
3 FROM
4 TABLE(DBMS_XPLAN.DISPLAY());
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 69 | 2 (0)|
00:00:01 |
CACHE : CACHE mentioned that how many no of sequence values will be stored
and they are stored in memory for faster access.
Create synonym – show you how to create a new synonym for a table.
Drop a synonym – describe how to drop a synonym from the database.
The name of the object for which you are creating the synonym. It can be one of the following:
table
view
sequence CREATE PUBLIC SYNONYM suppliers
stored procedure FOR app.suppliers;
function
package Drop synonym
materialized view DROP PUBLIC SYNONYM suppliers;
java class schema object
user-defined object
synonym
med PUBLIC
Equi Join
Non-Equi Join
Self Join
The syntax for NON-ANSI joins:
SELECT * FROM TABLE NAME1, TABLE NAME2 WHERE <JOIN CONDITION>;
Inner Join
Outer Join (Left Outer Join, Right Outer Join, and Full Outer Join)
Cross Join (or) Cartesian Join
Natural Join
Syntax for ANSI joins:
SELECT * FROM <TABLE NAME1> <JOIN KEY> <TABLE NAME2 > ON <JOIN CONDITION>;
Please use the below SQL script to create and populate the Course and Student tables with the required sa
When we perform any join operation between tables there is no need to have a relationship(optional) (i.e.
only matching data/matching rows.
re as follows.
conditions then we called as the join as ANSI format join. ANSI joins are portability (move
NDITION>;
e', 3500);
L', 3000);
erver', 4500);
mes', 10);
mith', 20);
arner', 30);
ra', 10);
am', 20);
d an EQUI join. When we use EQUI join between two (or) more than two tables the common
mmend). The common column (or) common field datatype must be matched.
ve a relationship(optional) (i.e. primary key & foreign key relation). EQUI join always retrieves