SlideShare a Scribd company logo
Lecture 7:
Structured Query Language (SQL)
ISOM3260, Spring 2014
2
Where we are now
• Database environment
– Introduction to database
• Database development process
– steps to develop a database
• Conceptual data modeling
– entity-relationship (ER) diagram; enhanced ER
• Logical database design
– transforming ER diagram into relations; normalization
• Physical database design
– technical specifications of the database
• Database implementation
– Structured Query Language (SQL), Advanced SQL
• Advanced topics
– data and database administration
3
Database development activities during SDLC
4
Structured Query Language (SQL)
• What is SQL?
• Creating Tables
• Changing and Removing Tables
• INSERT, DELETE, UPDATE
• Creating Indexes
• SELECT statement
• Using and Defining Views
5
What is SQL?
• Structured Query Language
• The standard language for relational database
management systems (RDBMS)
– SQL-1999 standard (Core, and 8 other levels)
– SQL-2008 standard
• Most RDBMS are in partial compliance with SQL-1999
and SQL-2008
• Each vendor’s version also includes enhancements,
features, and capabilities beyond the core SQL-1999
standard
6
Benefits of a Standardized
Relational Language
• Reduced training costs
– programmers can concentrate on one language
• Productivity
– programmers can more quickly maintain existing programs
• Application portability
– applications can be moved from machine to machine
• Application longevity
– a standard language tend to exist for a long time
• Reduced dependence on a single vendor
– can use different vendors for the DBMS
• Cross-system communication
– different DBMSs and programs can communicate and
cooperate
7
SQL Environment
• Catalog
– a set of schemas that constitute the description of a database
• Schema
– that structure which contains descriptions of objects created by
a user (base tables, views, constraints)
• SQL commands
– Data Definition Language (DDL)
• commands to define a database, including creating, altering, and
dropping tables and establishing constraints
– Data Manipulation Language (DML)
• commands to maintain and query a database
– Data Control Language (DCL)
• commands to control a database, including administering privileges
and committing (saving) data
8
Figure 6-1:
A simplified schematic of a typical SQL environment
Describes all
user schemas
9
Figure 6-4:
DDL, DML, DCL, and the database development process
10
Figure 6-3: Sample Pine Valley Furniture data
Customer_T Order_T
Order_Line_T
Product_T
11
Creating Tables
• Identify appropriate datatype
• Identify columns that should not accept null value
(NOT NULL)
• Identify columns that need to be unique (UNIQUE
and PRIMARY KEY)
• Identify all primary key-foreign key mates
(REFERENCES)
• Identify columns for which a default value is
desired (DEFAULT)
• Identify columns for which domain specifications
may be stated (CHECK)
• Create table using CREATE TABLE command
12
Figure 6-6: SQL database definition commands for Pine Valley Furniture
13
Figure 6-6: SQL database definition commands for Pine Valley Furniture
Defining
attributes and
their data types
14
Figure 6-6: SQL database definition commands for Pine Valley Furniture
Non-nullable
specifications
Note: primary
keys should not
be null
15
Figure 6-6: SQL database definition commands for Pine Valley Furniture
Identifying
primary keys
This is a composite
primary key
16
Figure 6-6: SQL database definition commands for Pine Valley Furniture
Identifying
foreign keys and
establishing
relationships
17
Figure 6-6: SQL database definition commands for Pine Valley Furniture
Default values
and domain
constraints
18
Figure 6-6: SQL database definition commands for Pine Valley Furniture
Overall table
definitions
19
Changing and Removing Tables
• Changing table definitions
– To add customer type column to the CUSTOMER table
ALTER TABLE Customer_T ADD(Cust_Type VARCHAR2(2));
• Removing tables
– To drop a table from a database schema
DROP TABLE Customer_T;
20
INSERT Statement
• Adds data to a table
• Inserting a row of data where every attribute will have a value
INSERT INTO Customer_T VALUES (1,‘Contemporary
Casuals’,‘1355 S. Himes Blvd.’,‘Gainesville’,‘FL’,
‘32601’);
• Inserting a row of data that has some null attributes requires
identifying the fields that actually get data
INSERT INTO Product_T (Product_ID, Product_Description,
Product_Finish, Standard_Price) VALUES (1,‘End
Table’,‘Cherry’,175);
• Inserting data from another table
INSERT INTO CA_Customer_T
SELECT * FROM Customer_T WHERE State = ‘CA’;
21
DELETE Statement
• Removes rows from a table
• Delete rows that meet a certain criterion
DELETE FROM Customer_T
WHERE State = ‘HI’;
• Delete all rows from a table
DELETE FROM Customer_T;
22
UPDATE Statement
• To modify standard price of product 7 in
PRODUCT table to 775
UPDATE Product_T
SET Standard_Price = 775
WHERE Product_ID=7;
23
Creating Indexes
• Speed up access to base table data
• To create an alphabetical index on customer
name in the CUSTOMER table
CREATE INDEX Name_IDX
ON Customer_T(Customer_Name);
• To remove the index
DROP INDEX Name_IDX;
24
SELECT Statement
• Used for queries on single or multiple tables
• Clauses of the SELECT statement
– SELECT
• list the columns (and expressions) that should be returned from the query
– FROM
• indicate the table(s) or view(s) from which data will be obtained
– WHERE
• indicate the conditions under which a row will be included in the result
– GROUP BY
• indicate categorization of results
– HAVING
• indicate the conditions under which a category (group) will be included
– ORDER BY
• sorts the result according to specified criteria
25
SELECT Example
• Find products with standard price less than $275
SELECT Product_Description, Standard_Price
FROM Product_T
WHERE Standard_Price < 275;
• To display all columns
SELECT *
• To display without duplicate rows
SELECT DISTINCT City
Table 6-3:
26
SELECT: Comparison Operators
• Which orders have been placed after 10/24/2011?
SELECT Order_ID, Order_Date
FROM Order_T
WHERE Order_Date > ‘24-OCT-2011’;
• What furniture does Pine Valley carry that isn’t made of
Cherry?
SELECT Product_Description, Product_Finish
FROM Product_T
WHERE Product_Finish != ‘Cherry’;
27
SELECT: Alias
• Alias is an alternative column name or table name
SELECT CUST.Customer_Name AS NAME,
CUST.Customer_Address
FROM Customer_T CUST
WHERE NAME = ‘Home Furnishings’;
NAME CUSTOMER_ADDRESS
Home Furnishings 1900 Allard Ave.
Results:
28
SELECT: Using Expressions
• An expression has an operator acting on numeric columns
• Operators include: *, / , +, –
• Expressions in parentheses are executed first, followed by
‘*’ and ‘/’ and then ‘+’ and ‘-’, from left to right
• What is the total value for each product in inventory?
SELECT Product_Description, Standard_Price,
Quantity_On_Hand, Standard_Price *
Quantity_On_Hand AS VALUE
FROM Product_T;
29
SELECT: Using Functions
• Functions include
– COUNT, COUNT (*), MIN, MAX, SUM, and AVG
– COUNT adds up the number of rows selected by a query that
do not contain NULL
– COUNT (*) adds up all the rows selected by a query
– SUM and AVG can only be used with numeric columns
• Using functions will result in a one-row answer
• How many different items were ordered on order
number 1004?
SELECT COUNT(*) FROM Order_Line_T
WHERE Order_ID = 1004;
30
SELECT: Using Wildcards
• Wildcard used in SELECT clause
* (means all)
SELECT * FROM Customer_T;
• Wildcards used in WHERE clause
% (means any collection of characters)
WHERE Product_Description LIKE ‘%Desk’
will find ‘Computer Desk’, ‘8-Drawer Desk’, etc.
_ (means exactly one character)
WHERE Product_Description LIKE ‘_-drawer’
will find ‘3-drawer’, ‘5-drawer’, etc.
31
SELECT: Boolean Operators
• Include AND, OR, and NOT operators for customizing conditions
in WHERE clause
• If multiple operators are used, NOT is evaluated first, then AND,
then OR
• List product description, finish, and price for all desks and all tables
that cost more than $300.
SELECT Product_Description, Product_Finish,
Standard_Price
FROM Product_T
WHERE Product_Description LIKE ‘%Desk’
OR Product_Description LIKE ‘%Table’
AND Standard_Price > 300;
Note: All desks will be listed; even those that cost 300 or less.
32
SELECT: Boolean Operators
• List product description, finish, and price for all
desks and tables that cost more than $300.
SELECT Product_Description, Product_Finish,
Standard_Price
FROM Product_T
WHERE (Product_Description LIKE ‘%Desk’
OR Product_Description LIKE ‘%Table’)
AND Standard_Price > 300;
33
SELECT: Ranges
• Which products have a price between $200 and
$300?
SELECT Product_Description, Standard_Price
FROM Product_T
WHERE Standard_Price > 199 AND
Standard_Price < 301;
SELECT Product_Description, Standard_Price
FROM Product_T
WHERE Standard_Price BETWEEN 200 AND 300;
34
SELECT: IN and NOT IN Lists
• List all customers who live in warmer states.
SELECT Customer_Name, City, State
FROM Customer_T
WHERE State IN (‘FL’,‘TX’,‘CA’,‘HI’);
Note: The IN operator in this example allows you to include
rows whose STATE value is either FL, TX, CA, or HI. It is more
efficient than separate OR conditions.
35
Sorting Results: ORDER BY
• Referring to the previous query, list the results
alphabetically by state, and alphabetically by
customer within each state.
SELECT Customer_Name, City, State
FROM Customer_T
WHERE State IN (‘FL’,‘TX’,‘CA’,‘HI’)
ORDER BY State, Customer_Name;
Note: (1) If sorting from high to low, use DESC as a keyword
placed after the column to sort. (2) Oracle sorts NULLs last.
36
Categorizing Results : GROUP BY
• GROUP BY is useful when paired with aggregate functions
– divides a table into subsets (by groups); then an aggregate function can be
used to provide summary information for that group
• Scalar aggregate
– a single value returned from an SQL query with an aggregate function
• Vector aggregate
– multiple values returned from an SQL query with an aggregate function (via
GROUP BY)
• Count no. of customers with addresses in each state we ship.
SELECT State, COUNT(State)
FROM Customer_T
GROUP BY State;
Note: You can use single-value fields with aggregate functions
if they are included in the GROUP BY clause.
37
Qualifying Results: HAVING
• Acts like a WHERE clause
• Identifies groups that meet a criterion rather than rows
• Use together with GROUP BY
• Find only states with more than one customer.
SELECT State, COUNT(State)
FROM Customer_T
GROUP BY State
HAVING COUNT(State) > 1;
Note: Only groups with total number of customers greater than
1 are included in final result.
38
Figure 6-10:
SQL statement
processing order
39
Using and Defining Views
• Base table
– a table containing the raw data
• Dynamic view
– a “virtual table” created dynamically upon request by a user
– no data actually stored; instead data from base table made
available to user
– based on SELECT statement on base tables or other views
• Advantages of views
– simplify query commands
– provide data security
– enhance programming productivity
• CREATE VIEW command
40
Example 1: CREATE VIEW
• What are the data elements in a customer invoice?
Save this query as a view named Invoice_V.
CREATE VIEW Invoice_V AS
SELECT Customer_T.Customer_ID,Customer_Name,
Customer_Address,Order_T.Order_ID,Order_Date,
Product_T.Product_ID,Product_Description,
Product_Finish,Standard_Price,Ordered_Quantity
FROM Customer_T,Order_T,Order_Line_T,Product_T
WHERE
Customer_T.Customer_ID=Order_T.Customer_ID AND
Order_T.Order_ID=Order_Line_T.Order_ID AND
Product_T.Product_ID=Order_Line_T.Product_ID;
41
Example 1: CREATE VIEW
• What are the data elements necessary to
create an invoice for order number 1004?
SELECT
Customer_ID,Customer_Name,Customer_Address,
Product_ID,Ordered_Quantity
FROM Invoice_V
WHERE Order_ID=1004;
42
Using and Defining Views
• Some people suggest creating a view for every
base table, even if that view is identical to the
base table
• Greater programming productivity
– If the programs use the CUSTOMER_T and
CUSTOMER_T is renormalized into 2 tables, then
all the programs have to be modified
– If the programs use a view on the CUSTOMER_T
and CUSTOMER_T is renormalized into 2 tables,
then only the view has to be recreated
43
Review Questions
• What are the benefits of SQL?
• What is the SQL environment?
• How to CREATE, ALTER, and DROP tables?
• How to INSERT, UPDATE, and DELETE rows
from tables?
• How to CREATE and DROP indexes?
• How to retrieve data using SELECT statement?
• What are views?
Ad

More Related Content

What's hot (20)

Hospital Management System
Hospital Management SystemHospital Management System
Hospital Management System
Pranil Dukare
 
Sql ppt
Sql pptSql ppt
Sql ppt
Anuja Lad
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
VENNILAV6
 
Importance of data model
Importance of data modelImportance of data model
Importance of data model
yhen06
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
Lakshman Basnet
 
Virtual machine
Virtual machineVirtual machine
Virtual machine
Nikunj Dhameliya
 
Fundamentals of Database system
Fundamentals of Database systemFundamentals of Database system
Fundamentals of Database system
philipsinter
 
1. Introduction to DBMS
1. Introduction to DBMS1. Introduction to DBMS
1. Introduction to DBMS
koolkampus
 
Sql commands
Sql commandsSql commands
Sql commands
Prof. Dr. K. Adisesha
 
SQL: Structured Query Language
SQL: Structured Query LanguageSQL: Structured Query Language
SQL: Structured Query Language
Rohit Bisht
 
Using SQL Queries to Insert, Update, Delete, and View Data.ppt
Using SQL Queries to Insert, Update, Delete, and View Data.pptUsing SQL Queries to Insert, Update, Delete, and View Data.ppt
Using SQL Queries to Insert, Update, Delete, and View Data.ppt
MohammedJifar1
 
Stored procedure
Stored procedureStored procedure
Stored procedure
baabtra.com - No. 1 supplier of quality freshers
 
Advanced sql
Advanced sqlAdvanced sql
Advanced sql
Dhani Ahmad
 
database Normalization
database Normalizationdatabase Normalization
database Normalization
Harsiddhi Thakkar
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Introduction to Data Mining
Introduction to Data MiningIntroduction to Data Mining
Introduction to Data Mining
Si Krishan
 
Advance Database Management Systems -Object Oriented Principles In Database
Advance Database Management Systems -Object Oriented Principles In DatabaseAdvance Database Management Systems -Object Oriented Principles In Database
Advance Database Management Systems -Object Oriented Principles In Database
Sonali Parab
 
Database design
Database designDatabase design
Database design
Dhani Ahmad
 
Database system utilities by dinesh
Database system utilities by dineshDatabase system utilities by dinesh
Database system utilities by dinesh
Dinesh Kumar
 
Introduction to DBMS(For College Seminars)
Introduction to DBMS(For College Seminars)Introduction to DBMS(For College Seminars)
Introduction to DBMS(For College Seminars)
Naman Joshi
 
Hospital Management System
Hospital Management SystemHospital Management System
Hospital Management System
Pranil Dukare
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
VENNILAV6
 
Importance of data model
Importance of data modelImportance of data model
Importance of data model
yhen06
 
Fundamentals of Database system
Fundamentals of Database systemFundamentals of Database system
Fundamentals of Database system
philipsinter
 
1. Introduction to DBMS
1. Introduction to DBMS1. Introduction to DBMS
1. Introduction to DBMS
koolkampus
 
SQL: Structured Query Language
SQL: Structured Query LanguageSQL: Structured Query Language
SQL: Structured Query Language
Rohit Bisht
 
Using SQL Queries to Insert, Update, Delete, and View Data.ppt
Using SQL Queries to Insert, Update, Delete, and View Data.pptUsing SQL Queries to Insert, Update, Delete, and View Data.ppt
Using SQL Queries to Insert, Update, Delete, and View Data.ppt
MohammedJifar1
 
Introduction to Data Mining
Introduction to Data MiningIntroduction to Data Mining
Introduction to Data Mining
Si Krishan
 
Advance Database Management Systems -Object Oriented Principles In Database
Advance Database Management Systems -Object Oriented Principles In DatabaseAdvance Database Management Systems -Object Oriented Principles In Database
Advance Database Management Systems -Object Oriented Principles In Database
Sonali Parab
 
Database system utilities by dinesh
Database system utilities by dineshDatabase system utilities by dinesh
Database system utilities by dinesh
Dinesh Kumar
 
Introduction to DBMS(For College Seminars)
Introduction to DBMS(For College Seminars)Introduction to DBMS(For College Seminars)
Introduction to DBMS(For College Seminars)
Naman Joshi
 

Similar to SQL(database) (20)

Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
Vibrant Technologies & Computers
 
SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data   SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data
Vibrant Technologies & Computers
 
Chap 7
Chap 7Chap 7
Chap 7
Karan Patil
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
arjun431527
 
INTRODUCTION TO SQL QUERIES REALTED BRIEF
INTRODUCTION TO SQL QUERIES REALTED BRIEFINTRODUCTION TO SQL QUERIES REALTED BRIEF
INTRODUCTION TO SQL QUERIES REALTED BRIEF
VADAPALLYPRAVEENKUMA1
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
Nitesh Singh
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
YitbarekMurche
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
BhupendraShahi6
 
Ch 9 S Q L
Ch 9  S Q LCh 9  S Q L
Ch 9 S Q L
guest8fdbdd
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SabrinaShanta2
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SaiMiryala1
 
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasdasdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
MuhamedAhmed35
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
Md. Mahedee Hasan
 
Lab
LabLab
Lab
neelam_rawat
 
Lec-w9-SQL.pptx Introduction to SQL in basics
Lec-w9-SQL.pptx Introduction to SQL in basicsLec-w9-SQL.pptx Introduction to SQL in basics
Lec-w9-SQL.pptx Introduction to SQL in basics
zylzuht983
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
Information Technology
 
SQL
SQLSQL
SQL
zekeLabs Technologies
 
Data Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdfData Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdf
howto4ucontact
 
The Database Environment Chapter 7
The Database Environment Chapter 7The Database Environment Chapter 7
The Database Environment Chapter 7
Jeanie Arnoco
 
DP080_Lecture_1 SQL lecture document .pdf
DP080_Lecture_1 SQL lecture document .pdfDP080_Lecture_1 SQL lecture document .pdf
DP080_Lecture_1 SQL lecture document .pdf
MinhTran394436
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
arjun431527
 
INTRODUCTION TO SQL QUERIES REALTED BRIEF
INTRODUCTION TO SQL QUERIES REALTED BRIEFINTRODUCTION TO SQL QUERIES REALTED BRIEF
INTRODUCTION TO SQL QUERIES REALTED BRIEF
VADAPALLYPRAVEENKUMA1
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
BhupendraShahi6
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SabrinaShanta2
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SaiMiryala1
 
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasdasdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
MuhamedAhmed35
 
Lec-w9-SQL.pptx Introduction to SQL in basics
Lec-w9-SQL.pptx Introduction to SQL in basicsLec-w9-SQL.pptx Introduction to SQL in basics
Lec-w9-SQL.pptx Introduction to SQL in basics
zylzuht983
 
Data Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdfData Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdf
howto4ucontact
 
The Database Environment Chapter 7
The Database Environment Chapter 7The Database Environment Chapter 7
The Database Environment Chapter 7
Jeanie Arnoco
 
DP080_Lecture_1 SQL lecture document .pdf
DP080_Lecture_1 SQL lecture document .pdfDP080_Lecture_1 SQL lecture document .pdf
DP080_Lecture_1 SQL lecture document .pdf
MinhTran394436
 
Ad

More from welcometofacebook (20)

Quantitative exercise-toasty oven
Quantitative exercise-toasty ovenQuantitative exercise-toasty oven
Quantitative exercise-toasty oven
welcometofacebook
 
EVC exercise-novel motor oil
EVC exercise-novel motor oilEVC exercise-novel motor oil
EVC exercise-novel motor oil
welcometofacebook
 
jones blair calculations
jones blair calculationsjones blair calculations
jones blair calculations
welcometofacebook
 
EVC exercise-odi case
EVC exercise-odi caseEVC exercise-odi case
EVC exercise-odi case
welcometofacebook
 
cltv calculation-calyx corolla
cltv calculation-calyx corolla cltv calculation-calyx corolla
cltv calculation-calyx corolla
welcometofacebook
 
consumer behavior(4210)
consumer behavior(4210)consumer behavior(4210)
consumer behavior(4210)
welcometofacebook
 
competing in a global market(4210)
competing in a global market(4210)competing in a global market(4210)
competing in a global market(4210)
welcometofacebook
 
promotion strategies(4210)
promotion strategies(4210)promotion strategies(4210)
promotion strategies(4210)
welcometofacebook
 
pricing strategies(4210)
pricing strategies(4210)pricing strategies(4210)
pricing strategies(4210)
welcometofacebook
 
Pharmasim
PharmasimPharmasim
Pharmasim
welcometofacebook
 
distribution strategies calyx and corolla(4210)
distribution strategies calyx and corolla(4210)distribution strategies calyx and corolla(4210)
distribution strategies calyx and corolla(4210)
welcometofacebook
 
distribution strategies(4210)
distribution strategies(4210)distribution strategies(4210)
distribution strategies(4210)
welcometofacebook
 
the birth of swatch(4210)
the birth of swatch(4210)the birth of swatch(4210)
the birth of swatch(4210)
welcometofacebook
 
product and brand strategies(4210)
product and brand strategies(4210)product and brand strategies(4210)
product and brand strategies(4210)
welcometofacebook
 
stp case jones blair(4210)
stp case jones blair(4210)stp case jones blair(4210)
stp case jones blair(4210)
welcometofacebook
 
stp(4210)
stp(4210)stp(4210)
stp(4210)
welcometofacebook
 
situational analysis(4210)
situational analysis(4210)situational analysis(4210)
situational analysis(4210)
welcometofacebook
 
quantitative analysis(4210)
quantitative analysis(4210)quantitative analysis(4210)
quantitative analysis(4210)
welcometofacebook
 
overview of marketing strategy(4210)
overview of marketing strategy(4210)overview of marketing strategy(4210)
overview of marketing strategy(4210)
welcometofacebook
 
Class+3+ +quantitative+analysis+exercise+answer+key
Class+3+ +quantitative+analysis+exercise+answer+keyClass+3+ +quantitative+analysis+exercise+answer+key
Class+3+ +quantitative+analysis+exercise+answer+key
welcometofacebook
 
Quantitative exercise-toasty oven
Quantitative exercise-toasty ovenQuantitative exercise-toasty oven
Quantitative exercise-toasty oven
welcometofacebook
 
EVC exercise-novel motor oil
EVC exercise-novel motor oilEVC exercise-novel motor oil
EVC exercise-novel motor oil
welcometofacebook
 
cltv calculation-calyx corolla
cltv calculation-calyx corolla cltv calculation-calyx corolla
cltv calculation-calyx corolla
welcometofacebook
 
competing in a global market(4210)
competing in a global market(4210)competing in a global market(4210)
competing in a global market(4210)
welcometofacebook
 
distribution strategies calyx and corolla(4210)
distribution strategies calyx and corolla(4210)distribution strategies calyx and corolla(4210)
distribution strategies calyx and corolla(4210)
welcometofacebook
 
distribution strategies(4210)
distribution strategies(4210)distribution strategies(4210)
distribution strategies(4210)
welcometofacebook
 
product and brand strategies(4210)
product and brand strategies(4210)product and brand strategies(4210)
product and brand strategies(4210)
welcometofacebook
 
overview of marketing strategy(4210)
overview of marketing strategy(4210)overview of marketing strategy(4210)
overview of marketing strategy(4210)
welcometofacebook
 
Class+3+ +quantitative+analysis+exercise+answer+key
Class+3+ +quantitative+analysis+exercise+answer+keyClass+3+ +quantitative+analysis+exercise+answer+key
Class+3+ +quantitative+analysis+exercise+answer+key
welcometofacebook
 
Ad

Recently uploaded (20)

MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 

SQL(database)

  • 1. Lecture 7: Structured Query Language (SQL) ISOM3260, Spring 2014
  • 2. 2 Where we are now • Database environment – Introduction to database • Database development process – steps to develop a database • Conceptual data modeling – entity-relationship (ER) diagram; enhanced ER • Logical database design – transforming ER diagram into relations; normalization • Physical database design – technical specifications of the database • Database implementation – Structured Query Language (SQL), Advanced SQL • Advanced topics – data and database administration
  • 4. 4 Structured Query Language (SQL) • What is SQL? • Creating Tables • Changing and Removing Tables • INSERT, DELETE, UPDATE • Creating Indexes • SELECT statement • Using and Defining Views
  • 5. 5 What is SQL? • Structured Query Language • The standard language for relational database management systems (RDBMS) – SQL-1999 standard (Core, and 8 other levels) – SQL-2008 standard • Most RDBMS are in partial compliance with SQL-1999 and SQL-2008 • Each vendor’s version also includes enhancements, features, and capabilities beyond the core SQL-1999 standard
  • 6. 6 Benefits of a Standardized Relational Language • Reduced training costs – programmers can concentrate on one language • Productivity – programmers can more quickly maintain existing programs • Application portability – applications can be moved from machine to machine • Application longevity – a standard language tend to exist for a long time • Reduced dependence on a single vendor – can use different vendors for the DBMS • Cross-system communication – different DBMSs and programs can communicate and cooperate
  • 7. 7 SQL Environment • Catalog – a set of schemas that constitute the description of a database • Schema – that structure which contains descriptions of objects created by a user (base tables, views, constraints) • SQL commands – Data Definition Language (DDL) • commands to define a database, including creating, altering, and dropping tables and establishing constraints – Data Manipulation Language (DML) • commands to maintain and query a database – Data Control Language (DCL) • commands to control a database, including administering privileges and committing (saving) data
  • 8. 8 Figure 6-1: A simplified schematic of a typical SQL environment Describes all user schemas
  • 9. 9 Figure 6-4: DDL, DML, DCL, and the database development process
  • 10. 10 Figure 6-3: Sample Pine Valley Furniture data Customer_T Order_T Order_Line_T Product_T
  • 11. 11 Creating Tables • Identify appropriate datatype • Identify columns that should not accept null value (NOT NULL) • Identify columns that need to be unique (UNIQUE and PRIMARY KEY) • Identify all primary key-foreign key mates (REFERENCES) • Identify columns for which a default value is desired (DEFAULT) • Identify columns for which domain specifications may be stated (CHECK) • Create table using CREATE TABLE command
  • 12. 12 Figure 6-6: SQL database definition commands for Pine Valley Furniture
  • 13. 13 Figure 6-6: SQL database definition commands for Pine Valley Furniture Defining attributes and their data types
  • 14. 14 Figure 6-6: SQL database definition commands for Pine Valley Furniture Non-nullable specifications Note: primary keys should not be null
  • 15. 15 Figure 6-6: SQL database definition commands for Pine Valley Furniture Identifying primary keys This is a composite primary key
  • 16. 16 Figure 6-6: SQL database definition commands for Pine Valley Furniture Identifying foreign keys and establishing relationships
  • 17. 17 Figure 6-6: SQL database definition commands for Pine Valley Furniture Default values and domain constraints
  • 18. 18 Figure 6-6: SQL database definition commands for Pine Valley Furniture Overall table definitions
  • 19. 19 Changing and Removing Tables • Changing table definitions – To add customer type column to the CUSTOMER table ALTER TABLE Customer_T ADD(Cust_Type VARCHAR2(2)); • Removing tables – To drop a table from a database schema DROP TABLE Customer_T;
  • 20. 20 INSERT Statement • Adds data to a table • Inserting a row of data where every attribute will have a value INSERT INTO Customer_T VALUES (1,‘Contemporary Casuals’,‘1355 S. Himes Blvd.’,‘Gainesville’,‘FL’, ‘32601’); • Inserting a row of data that has some null attributes requires identifying the fields that actually get data INSERT INTO Product_T (Product_ID, Product_Description, Product_Finish, Standard_Price) VALUES (1,‘End Table’,‘Cherry’,175); • Inserting data from another table INSERT INTO CA_Customer_T SELECT * FROM Customer_T WHERE State = ‘CA’;
  • 21. 21 DELETE Statement • Removes rows from a table • Delete rows that meet a certain criterion DELETE FROM Customer_T WHERE State = ‘HI’; • Delete all rows from a table DELETE FROM Customer_T;
  • 22. 22 UPDATE Statement • To modify standard price of product 7 in PRODUCT table to 775 UPDATE Product_T SET Standard_Price = 775 WHERE Product_ID=7;
  • 23. 23 Creating Indexes • Speed up access to base table data • To create an alphabetical index on customer name in the CUSTOMER table CREATE INDEX Name_IDX ON Customer_T(Customer_Name); • To remove the index DROP INDEX Name_IDX;
  • 24. 24 SELECT Statement • Used for queries on single or multiple tables • Clauses of the SELECT statement – SELECT • list the columns (and expressions) that should be returned from the query – FROM • indicate the table(s) or view(s) from which data will be obtained – WHERE • indicate the conditions under which a row will be included in the result – GROUP BY • indicate categorization of results – HAVING • indicate the conditions under which a category (group) will be included – ORDER BY • sorts the result according to specified criteria
  • 25. 25 SELECT Example • Find products with standard price less than $275 SELECT Product_Description, Standard_Price FROM Product_T WHERE Standard_Price < 275; • To display all columns SELECT * • To display without duplicate rows SELECT DISTINCT City Table 6-3:
  • 26. 26 SELECT: Comparison Operators • Which orders have been placed after 10/24/2011? SELECT Order_ID, Order_Date FROM Order_T WHERE Order_Date > ‘24-OCT-2011’; • What furniture does Pine Valley carry that isn’t made of Cherry? SELECT Product_Description, Product_Finish FROM Product_T WHERE Product_Finish != ‘Cherry’;
  • 27. 27 SELECT: Alias • Alias is an alternative column name or table name SELECT CUST.Customer_Name AS NAME, CUST.Customer_Address FROM Customer_T CUST WHERE NAME = ‘Home Furnishings’; NAME CUSTOMER_ADDRESS Home Furnishings 1900 Allard Ave. Results:
  • 28. 28 SELECT: Using Expressions • An expression has an operator acting on numeric columns • Operators include: *, / , +, – • Expressions in parentheses are executed first, followed by ‘*’ and ‘/’ and then ‘+’ and ‘-’, from left to right • What is the total value for each product in inventory? SELECT Product_Description, Standard_Price, Quantity_On_Hand, Standard_Price * Quantity_On_Hand AS VALUE FROM Product_T;
  • 29. 29 SELECT: Using Functions • Functions include – COUNT, COUNT (*), MIN, MAX, SUM, and AVG – COUNT adds up the number of rows selected by a query that do not contain NULL – COUNT (*) adds up all the rows selected by a query – SUM and AVG can only be used with numeric columns • Using functions will result in a one-row answer • How many different items were ordered on order number 1004? SELECT COUNT(*) FROM Order_Line_T WHERE Order_ID = 1004;
  • 30. 30 SELECT: Using Wildcards • Wildcard used in SELECT clause * (means all) SELECT * FROM Customer_T; • Wildcards used in WHERE clause % (means any collection of characters) WHERE Product_Description LIKE ‘%Desk’ will find ‘Computer Desk’, ‘8-Drawer Desk’, etc. _ (means exactly one character) WHERE Product_Description LIKE ‘_-drawer’ will find ‘3-drawer’, ‘5-drawer’, etc.
  • 31. 31 SELECT: Boolean Operators • Include AND, OR, and NOT operators for customizing conditions in WHERE clause • If multiple operators are used, NOT is evaluated first, then AND, then OR • List product description, finish, and price for all desks and all tables that cost more than $300. SELECT Product_Description, Product_Finish, Standard_Price FROM Product_T WHERE Product_Description LIKE ‘%Desk’ OR Product_Description LIKE ‘%Table’ AND Standard_Price > 300; Note: All desks will be listed; even those that cost 300 or less.
  • 32. 32 SELECT: Boolean Operators • List product description, finish, and price for all desks and tables that cost more than $300. SELECT Product_Description, Product_Finish, Standard_Price FROM Product_T WHERE (Product_Description LIKE ‘%Desk’ OR Product_Description LIKE ‘%Table’) AND Standard_Price > 300;
  • 33. 33 SELECT: Ranges • Which products have a price between $200 and $300? SELECT Product_Description, Standard_Price FROM Product_T WHERE Standard_Price > 199 AND Standard_Price < 301; SELECT Product_Description, Standard_Price FROM Product_T WHERE Standard_Price BETWEEN 200 AND 300;
  • 34. 34 SELECT: IN and NOT IN Lists • List all customers who live in warmer states. SELECT Customer_Name, City, State FROM Customer_T WHERE State IN (‘FL’,‘TX’,‘CA’,‘HI’); Note: The IN operator in this example allows you to include rows whose STATE value is either FL, TX, CA, or HI. It is more efficient than separate OR conditions.
  • 35. 35 Sorting Results: ORDER BY • Referring to the previous query, list the results alphabetically by state, and alphabetically by customer within each state. SELECT Customer_Name, City, State FROM Customer_T WHERE State IN (‘FL’,‘TX’,‘CA’,‘HI’) ORDER BY State, Customer_Name; Note: (1) If sorting from high to low, use DESC as a keyword placed after the column to sort. (2) Oracle sorts NULLs last.
  • 36. 36 Categorizing Results : GROUP BY • GROUP BY is useful when paired with aggregate functions – divides a table into subsets (by groups); then an aggregate function can be used to provide summary information for that group • Scalar aggregate – a single value returned from an SQL query with an aggregate function • Vector aggregate – multiple values returned from an SQL query with an aggregate function (via GROUP BY) • Count no. of customers with addresses in each state we ship. SELECT State, COUNT(State) FROM Customer_T GROUP BY State; Note: You can use single-value fields with aggregate functions if they are included in the GROUP BY clause.
  • 37. 37 Qualifying Results: HAVING • Acts like a WHERE clause • Identifies groups that meet a criterion rather than rows • Use together with GROUP BY • Find only states with more than one customer. SELECT State, COUNT(State) FROM Customer_T GROUP BY State HAVING COUNT(State) > 1; Note: Only groups with total number of customers greater than 1 are included in final result.
  • 39. 39 Using and Defining Views • Base table – a table containing the raw data • Dynamic view – a “virtual table” created dynamically upon request by a user – no data actually stored; instead data from base table made available to user – based on SELECT statement on base tables or other views • Advantages of views – simplify query commands – provide data security – enhance programming productivity • CREATE VIEW command
  • 40. 40 Example 1: CREATE VIEW • What are the data elements in a customer invoice? Save this query as a view named Invoice_V. CREATE VIEW Invoice_V AS SELECT Customer_T.Customer_ID,Customer_Name, Customer_Address,Order_T.Order_ID,Order_Date, Product_T.Product_ID,Product_Description, Product_Finish,Standard_Price,Ordered_Quantity FROM Customer_T,Order_T,Order_Line_T,Product_T WHERE Customer_T.Customer_ID=Order_T.Customer_ID AND Order_T.Order_ID=Order_Line_T.Order_ID AND Product_T.Product_ID=Order_Line_T.Product_ID;
  • 41. 41 Example 1: CREATE VIEW • What are the data elements necessary to create an invoice for order number 1004? SELECT Customer_ID,Customer_Name,Customer_Address, Product_ID,Ordered_Quantity FROM Invoice_V WHERE Order_ID=1004;
  • 42. 42 Using and Defining Views • Some people suggest creating a view for every base table, even if that view is identical to the base table • Greater programming productivity – If the programs use the CUSTOMER_T and CUSTOMER_T is renormalized into 2 tables, then all the programs have to be modified – If the programs use a view on the CUSTOMER_T and CUSTOMER_T is renormalized into 2 tables, then only the view has to be recreated
  • 43. 43 Review Questions • What are the benefits of SQL? • What is the SQL environment? • How to CREATE, ALTER, and DROP tables? • How to INSERT, UPDATE, and DELETE rows from tables? • How to CREATE and DROP indexes? • How to retrieve data using SELECT statement? • What are views?