Working With Oracle and Postgres
Working With Oracle and Postgres
Oracle and
PostgreSQL
Agenda
• Scope in Industry
• Database Design: Aspects and Real time
cases.
• Queries: Simple, self and complex joins,
Subqueries
• Programming: PL/SQL programming and
PL/PGSQL
PRESENTATION TITLE 2
Introduction
• Database Developer
• Database Administrator
• Data Analyst
• Data Scientist
PRESENTATION TITLE 4
Database Objects
• Tables
• Indexes
• Views
• Sequences
• Triggers
• Synonyms
……….
PRESENTATION TITLE 5
Database Design
PRESENTATION TITLE 6
Database Design- Real time approach
• Goal: Ensuring integrity, consistency and removing
redundancy
• Duplicate values should be eliminated
• Only one column should be designated for the purpose.
• Keep the tables as simple as possible.
• Create multiple tables and establish relationships between
them
• Allowing denormalized data intentionally.[Why?]
PRESENTATION TITLE 7
Database Design- Real time approach
• Identity by Objects/Entities
• For identified objects/entities, identify the unique
attributes/fields.
• Unless any specific purposes, the fields should not have
composite information.
PRESENTATION TITLE 8
Database -Application Design Steps
• Identity by Objects and fields
• Establishing Relationships
• Creating Indexes/Partitions
• Creating Views
• Creating Users/Roles
• Granting privileges
PRESENTATION TITLE 9
Tables Relationships
• One to One (PK to PK)
PRESENTATION TITLE 10
Tables Relationships (One to One)
• Categories (StudentID,Name)
StudentIDName
1 Ramesh S
2 Haja Mohideen K
3 Babu K
PRESENTATION TITLE 11
Tables Relationships (One to Many)
• Categories (CategoryID,CategoryName)
CategoryID CategoryName
1 Books
2 Stationeries
3 Beverages
• Products(CateogryID,ProductID, ProductName)
CategoryID ProductID ProductName
1 1 Operating System
2 2 192 Page ruled notebook
1 3 A deep dive into Datascience
3 4 Coca cola 100 ml.
1 5 C# Programming
PRESENTATION TITLE 12
What Relationship here?
Part Name VendorName
Part#:9039343 ABC and co
Part#:489768 ABC and co
Part#:9039343 ABC and co
Part#:657564 ABC and co
Part#:489768 XYZ and Co.,
Part#:657564 XYZ and Co.,
Part#:657564 XYZ and Co.,
Part#:9039343 XYZ and Co.,
Part#:489768 Raj Merchandisers
Part#:489768 Raj Merchandisers
Part#:489768 Raj Merchandisers
Part#:9039343 Raj Merchandisers
PRESENTATION TITLE 13
Types of Statements
DATA DEFINITION LANGUAGE:
CREAT, DROP, ALTER,RENAME
DATA MANIPULATION LANGUAGE:
INSERT,UPDATE, DELETE
DATA QUERY LANGUAGE: SELECT
DATA CONTROL LANGUAGE:
GRANT, REVOKE
TRASACTION CONTROL LANGUAGE:
COMMIT, ROLLBACK,SAVEPOINT
PRESENTATION TITLE 14
Database in Application Architecture
Queries and SP
PRESENTATION TITLE 15
Queries
Keywords : SELECT, FROM, WHERE, AND, OR, ORDER BY,
GROUP BY, CASE WHEN, HAVING
Aggregate Functions: SUM,AVG, MAX, MIN
Joins:
Inner join
Outer join(Left,Right)
Self-Join
Cross Join
Subqueries
DML: INSERT,DELETE,UPDATE
PRESENTATION TITLE 16
Queries - Conventions
• Use alias to objects(tables/views)
• Use specific column names instead of “* from”
• Don’t use manipulated columns in WHERE clause and try to use
them in wrapping query
• Use EXISTS and NOT EXISTS instead of IN and NOT IN
• Use CTE if you want to write complex queries and want to use
same query multiple times.
PRESENTATION TITLE 17
Query Optimization
• Indexes
• Partitions
• Divide and Conquer techniques(programmatically)
• Split the complex joins into few simple joins and get
the results
• This can be achieved by writing anonymous
blocks/stored procedures
PRESENTATION TITLE 18
PL/SQL
Stands for Procedural language extension to SQL
PL/SQL Units:
•Anonymous Block
•Function
•Library
•Procedure
•Package Body
•Package Specification
•Trigger
•Type
•Type Body
PRESENTATION TITLE 19
PL/SQL
PL/SQL Data Types:
PRESENTATION TITLE 20
PL/SQL
PL/SQL Data Types :
CHAR (2000) BLOB (128 TB)
VARCHAR CLOB(128 TB)
VARCHAR2(2000,32767) BFILE
NCHAR(2000)
NVARCHAR2
LONG/LONG RAW(2GB)
NUMBER
BOOLEAN
DATE
PRESENTATION TITLE 21
PL/SQL
PL/SQL Data Types example:
Empname varchar2(50);
Is_selected char(1);
Salary number(10,2);
Result Boolean;
PRESENTATION TITLE 22
PL/SQL
PL/SQL Block structure:
DECLARATION
variables declaration
BEGIN
<EXECUTION CODES>
EXCEPTION
<EXCEPTION HANDLING>
PRESENTATION TITLE 23
PL/SQL
PL/SQL Block Structure:
DECLARATION
variables declaration
BEGIN
<EXECUTION CODES>
EXCEPTION
<EXCEPTION HANDLING>
PRESENTATION TITLE 24
Programming in Postgres
Programming can be done using
• SQL
• PL/PGSQL
• Python
• C
PRESENTATION TITLE 25
Case Study I
We have large number of records in an excel sheet.
Storing each of the record in a database takes time as it
requires a database connection establishment each time.
PRESENTATION TITLE 26
Case Study II
We have a table called Rules which are used to update the
values to a column of another tables?
Rules may have multiple conditions and appended by
AND/OR operators.
We need to find out the following info from each rules:
Field Name
Operator
Condition value.
PRESENTATION TITLE 27
Case Study III
How do you establish a hierarchical relationships in a
database for the following Example?
We have list of products in general, but which is viewed
under different hierarchies in different areas:
In one area, it will be like CategorySub
categoryProducts
In another area, it will be like
SuperCategoryCategorySubcategoryProducts
So, the level of hierarchy need to be dynamic and not static.
PRESENTATION TITLE 28