RD SQL Notes
RD SQL Notes
Introduction to Relational
Database
Introduction
Database is a collection of related data and data is a collection
of facts and figures that can be processed to produce
information.
Earlier data models were not so scientific, hence they were prone to
introduce lots of duplication and update anomalies.
What is SQL (pronounced ess-que-ell, or sequel).
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization (ISO) in 1987
What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
Database – View
Relation
The data in RDBMS is stored in database objects called tables. A table is a collection of
related data entries and it consists of columns and rows.
Example: Relation/Table
Example – Relation/Table
Most of the SQL database programs also have their own proprietary
extensions in addition to the SQL standard!
E-R: Peterchen and Bachman
notations
When entities in one entity set can take part only once in the relationship set
and entities in other entity set can take part more than once in the relationship
set, cardinality is many to one.
The above fig represents that a student can take only One Course but one course can
be taken by Many students. So the cardinality will be n to 1. It means that for one
course there can be n students but for one student, there will be only one course.
Sample tables for E-R diagram
In the above diagram we have total participation at E1 end. Only the primary key of E1,
which is in total participation should be allowed as the primary key of the reduced table,
since if the primary key of E2 is used, it might have null values for many of its entries, since
its participation is only partial and may not have corresponding entries for all its values.
E-R diagram [order-products]
S.Srinivas
BUISNESS CHALLENGE ?
2
INTELLIGENCE-FUNDAMENTAL PRINCIPLES
5
WHAT BI DO….CONTINUE
6
BI ADVANTAGES
Improve Decision making
Optimizing internal business processes
9
DWH IS ENVIRONMENT NOT
PRODUCT
The end result is the creation of a new computing
environment for the purpose of providing the strategic
information for every business needs
10
DATA WAREHOUSE DEFINITION
According to Bill Inmon, A data warehouse is a
subject oriented, integrated, nonvolatile, and time
variant collection of data in support of
management decisions.
11
SUBJECT ORIENTED
Data is not stored by operational applications, but
by business subjects.
Example operational applications: order processing,
customer billing, customer accounts, savings
accounts, consumer loans, Account receivables ,
claim processing etc.,
DWH Subjects: sales, products, customer, account,
policy etc.,
12
INTEGRATED
Savings
A/C
Subject=Account
Checking
A/C
14
TIME-VARIANT
Operational systems support day-to-day data.
DWH data is used analysis and decision making
Example Query: what are the total sales of product A for the
15
last 3 years on New Year day across region “Y”
NONVOLATILE
Loads
OLTP DWH
16
EXERCISES
17
WHAT IS BUSINESS INTELLIGENCE?
18
BI DEFINITION
19
BI SOFTWARES
20
BI-BUSINESS QUESTIONS
Business Intelligence: Finance
What is the net income, expenses, gross profit,
and net profit for this quarter, year?
22
EXAMPLE.
23
KPI’S
24
KPI’S
Customer attrition
Turnover
25
BUSINESS INTELLIGENCE -CORPORATE
Business Intelligence Tools help to gather, store, access and
analyze corporate data to aid in decision-making.
Example applications:
Customer profiling
Customer support
Market research
Market segmentation
Product profitability
Statistical analysis
Inventory and distribution analysis.
26
BUSINESS INTELLIGENCE -CORPORATE
Business Intelligence Tools help to gather, store, access and
analyze corporate data to aid in decision-making.
Example applications:
Customer profiling
Customer support
Market research
Market segmentation
Product profitability
Statistical analysis
Inventory and distribution analysis.
27
CORPORATE BI SYSTEM
28
Source: IBM Redbook, IBM Framework for e-business, SG-24-6248-00, Sept., 2001, ibm.com/redbooks
29
BI SOLUTIONS ARCHITECTURE
Relationship
Solutions Management
Billing/Payment
Enabling OLAP Systems
MOLAP Data Mining
Technologies
XML Advertising/
Data Warehousing Promotions
Core Messaging
Technologies Databases Data Marts
Virtual Data Views Personalization
Meta Data
Data Networks
Expert Systems Security Supply Chain
Management
CORBA/IIOP
COM/DCOM Knowledge
Business Management
Performance
Source: Kalakota, e-business 2.0 Measurement
Roadmap for Success 30
MAJOR BI INFORMATION COMPONENTS
DATA WAREHOUSE
ARCHITECTURES
32
DWH ARCHITECTURE –1 FOR BI
EIS /DSS
Metadata
Web Browsers
Operational
Systems/Data Middleware/
API Data Mining
Data
Preparation 33
WAREHOUSE ARCHITECTURE - 2
Metadata
EIS /DSS
Data Mart
Metadata
Select Query Tools
Extract
Transform Data Mart
Integrate OLAP/ROLAP
Maintain Metadata
Web Browsers
Operational Data Mart
Systems/Data Middleware/
Data API Data Mining
Preparation 34
REFERENCE BOOK
Data warehousing, Fundamentals for IT
Professions, Paulraj Punniah, Wiley publications.
2 nd edition, ISBN: 978-81-265-3729-7(Print),
2015 (reprint).
35
HOW IT DIFFERS FROM
OPERATIONAL DATABASES
Data Warehousing Operational data usually
Accessing data organized by application
Transforming data
Distributing data
requirements
Storing data Data needs to be organized
Automating processes
Managing metadata
differently for decision-making
Finding and understanding data purposes
Data Analysis BI requires consolidation of data
Getting answers
Creating OLAP applications
across multiple systems.
Analyzing the data Tools are needed to extract,
Other analysis tools
cleanse, and transform data
36
SQL Basics
Module - III
SQL Introduction
Standard language for querying and manipulating data
Tables in SQL
Product
Tuples or rows
SQL CONSTRAINTS
•NOT NULL Constraint − Ensures that a column cannot have NULL value.
•DEFAULT Constraint − Provides a default value for a column when none is
specified.
•UNIQUE Constraint − Ensures that all values in a column are different.
•PRIMARY Key − Uniquely identifies each row/record in a database table.
•FOREIGN Key − Uniquely identifies a row/record in any of the given database
table.
•CHECK Constraint − The CHECK constraint ensures that all the values in a
column satisfies certain conditions.
•INDEX − Used to create and retrieve data from the database very quickly.
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
Simple SQL Query
Product PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
SELECT *
FROM Product
WHERE category=‘Gadgets’
Output Schema
Details
• Case insensitive:
– Same: SELECT Select select
– Same: Product product
– Different: ‘Seattle’ ‘seattle’
• Constants:
– ‘abc’ - yes
– “abc” - no
The LIKE operator
SELECT *
FROM Products
WHERE PName LIKE ‘%gizmo%’
Compare to:
Category
Gadgets
SELECT category Gadgets
FROM Product Photography
Household
Ordering the Results
SELECT pname, price, manufacturer
FROM Product
WHERE category=‘gizmo’ AND price > 50
ORDER BY price, pname
GizmoWorks 25 USA
Key
Canon 65 Japan
Hitachi 15 Japan
Product
PName Price Category Manufacturer
Foreign
Gizmo $19.99 Gadgets GizmoWorks
key
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Aggregation
SELECT avg(price) SELECT count(*)
FROM Product FROM Product
WHERE maker=“Toyota” WHERE year > 1995
We probably want:
SELECT product,
sum(price * quantity) AS SumSales
max(quantity) AS MaxQuantity
FROM Purchase
GROUP BY product
HAVING Clause
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak Why ?
HAVING C2
Evaluation steps:
1. Evaluate FROM-WHERE, apply condition C1
2. Group by the attributes a1,…,ak
3. Apply condition C2 to each group (may have aggregates)
4. Compute aggregates in S and return the result
Insertions
General form:
camera - -
Insertion: an Example
INSERT INTO Product(name, listPrice)
camera 200 -
UPDATE PRODUCT
SET price = price/2
WHERE Product.name IN
(SELECT product
FROM Purchase
WHERE Date =‘Oct, 25, 1999’);
NULL values