SlideShare ist ein Scribd-Unternehmen logo
1 von 43
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?

Weitere ähnliche Inhalte

Was ist angesagt?

Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
sanket1996
 
ITT MS-WORDS PRESENTATION---21
ITT  MS-WORDS   PRESENTATION---21ITT  MS-WORDS   PRESENTATION---21
ITT MS-WORDS PRESENTATION---21
Binay Kumar Giri
 

Was ist angesagt? (20)

Html forms
Html formsHtml forms
Html forms
 
Visual Basic Programming
Visual Basic ProgrammingVisual Basic Programming
Visual Basic Programming
 
Sql loader good example
Sql loader good exampleSql loader good example
Sql loader good example
 
Intermediate Excel
Intermediate Excel Intermediate Excel
Intermediate Excel
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Table maintenance generator and its modifications
Table maintenance generator and its modificationsTable maintenance generator and its modifications
Table maintenance generator and its modifications
 
Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
 
ITT MS-WORDS PRESENTATION---21
ITT  MS-WORDS   PRESENTATION---21ITT  MS-WORDS   PRESENTATION---21
ITT MS-WORDS PRESENTATION---21
 
Basic Ms excel
Basic Ms excelBasic Ms excel
Basic Ms excel
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Databases: Normalisation
Databases: NormalisationDatabases: Normalisation
Databases: Normalisation
 
Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)
 
Basic Functions - Excel 2013 Tutorial
Basic Functions - Excel 2013 TutorialBasic Functions - Excel 2013 Tutorial
Basic Functions - Excel 2013 Tutorial
 
Ms Access ppt
Ms Access pptMs Access ppt
Ms Access ppt
 
Microsoft excel
Microsoft excelMicrosoft excel
Microsoft excel
 
Sentencias abap
Sentencias abapSentencias abap
Sentencias abap
 
Data Manipulation Language
Data Manipulation LanguageData Manipulation Language
Data Manipulation Language
 
Business Rules in Databases
Business Rules in DatabasesBusiness Rules in Databases
Business Rules in Databases
 
Sap Adobe Form
Sap Adobe FormSap Adobe Form
Sap Adobe Form
 

Ähnlich wie SQL(database)

SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SaiMiryala1
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
YashaswiniSrinivasan1
 

Ähnlich wie 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
 
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
 
Chap 7
Chap 7Chap 7
Chap 7
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
 
chapter 8 SQL.ppt
chapter 8 SQL.pptchapter 8 SQL.ppt
chapter 8 SQL.ppt
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
Ch 9 S Q L
Ch 9  S Q LCh 9  S Q L
Ch 9 S Q L
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
Lab
LabLab
Lab
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
SQL
SQLSQL
SQL
 
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
 
The Database Environment Chapter 7
The Database Environment Chapter 7The Database Environment Chapter 7
The Database Environment Chapter 7
 
Physical Design and Development
Physical Design and DevelopmentPhysical Design and Development
Physical Design and Development
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
 
IR SQLite Session #1
IR SQLite Session #1IR SQLite Session #1
IR SQLite Session #1
 

Mehr von 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
 

Mehr von welcometofacebook (20)

Quantitative exercise-toasty oven
Quantitative exercise-toasty ovenQuantitative exercise-toasty oven
Quantitative exercise-toasty oven
 
EVC exercise-novel motor oil
EVC exercise-novel motor oilEVC exercise-novel motor oil
EVC exercise-novel motor oil
 
jones blair calculations
jones blair calculationsjones blair calculations
jones blair calculations
 
EVC exercise-odi case
EVC exercise-odi caseEVC exercise-odi case
EVC exercise-odi case
 
cltv calculation-calyx corolla
cltv calculation-calyx corolla cltv calculation-calyx corolla
cltv calculation-calyx corolla
 
consumer behavior(4210)
consumer behavior(4210)consumer behavior(4210)
consumer behavior(4210)
 
competing in a global market(4210)
competing in a global market(4210)competing in a global market(4210)
competing in a global market(4210)
 
promotion strategies(4210)
promotion strategies(4210)promotion strategies(4210)
promotion strategies(4210)
 
pricing strategies(4210)
pricing strategies(4210)pricing strategies(4210)
pricing strategies(4210)
 
Pharmasim
PharmasimPharmasim
Pharmasim
 
distribution strategies calyx and corolla(4210)
distribution strategies calyx and corolla(4210)distribution strategies calyx and corolla(4210)
distribution strategies calyx and corolla(4210)
 
distribution strategies(4210)
distribution strategies(4210)distribution strategies(4210)
distribution strategies(4210)
 
the birth of swatch(4210)
the birth of swatch(4210)the birth of swatch(4210)
the birth of swatch(4210)
 
product and brand strategies(4210)
product and brand strategies(4210)product and brand strategies(4210)
product and brand strategies(4210)
 
stp case jones blair(4210)
stp case jones blair(4210)stp case jones blair(4210)
stp case jones blair(4210)
 
stp(4210)
stp(4210)stp(4210)
stp(4210)
 
situational analysis(4210)
situational analysis(4210)situational analysis(4210)
situational analysis(4210)
 
quantitative analysis(4210)
quantitative analysis(4210)quantitative analysis(4210)
quantitative analysis(4210)
 
overview of marketing strategy(4210)
overview of marketing strategy(4210)overview of marketing strategy(4210)
overview of marketing strategy(4210)
 
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
 

Kürzlich hochgeladen

Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 

Kürzlich hochgeladen (20)

Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 

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?