chap on derivative
Transcript of chap on derivative
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 1/42
1
IS 4420Database Fundamentals
Chapter 7:Introduction to SQL
Leon Chen
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 2/42
2
Systems DevelopmentLife Cycle
Project Identificationand Selection
Project Initiation
and Planning Analysis
Physical Design
Implementation
Maintenance
Logical Design
Enterprise modeling
Conceptual data modeling
Logical database design
Physical database designand definition
Database implementation
Database maintenance
DatabaseDevelopment Process
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 3/42
3
Part Four: Implementation
Chapter 7 – Introduction to SQLChapter 8 – Advanced SQLChapter 9 – Client/Server EnvironmentChapter 10 – Internet
Chapter 11 – Data Warehousing
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 4/42
4
Overview
Define a database using SQL datadefinition languageWork with ViewsWrite single table queriesEstablish referential integrity
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 5/42
5
SQL OverviewStructured Query LanguageThe standard for relational databasemanagement systems (RDBMS)SQL-92 and SQL-99 Standards – Purpose:
Specify syntax/semantics for data definition andmanipulationDefine data structuresEnable portabilitySpecify minimal (level 1) and complete (level 2)standards
Allow for later growth/enhancement to standard
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 6/42
6
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 7/42
7
SQL EnvironmentCatalog
A set of schemas that constitute the description of adatabase
SchemaThe structure that contains descriptions of objectscreated by a user (base tables, views, constraints)
Data Definition Language (DDL)Commands that define a database, including creating,altering, and dropping tables and establishingconstraints
Data Manipulation Language (DML)Commands that maintain and query a database
Data Control Language (DCL)Commands that control a database, includingadministering privileges and committing data
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 8/42
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 9/42
9
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 10/42
10
SQL Database DefinitionData Definition Language (DDL)Major CREATE statements:
CREATE SCHEMA – defines a portion of thedatabase owned by a particular userCREATE TABLE – defines a table and its columnsCREATE VIEW – defines a logical table from oneor more views
Other CREATE statements: CHARACTER SET,COLLATION, TRANSLATION, ASSERTION,DOMAIN
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 11/42
11
The following slides create tables forthis enterprise data model
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 12/42
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 13/42
13
Non-nullable specification
Identifying primary key
Primary keyscan never have
NULL values
Create PRODUCT table
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 14/42
14
Non-nullable specifications
Primary key
Some primary keys are composite – composed of multiple attributes
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 15/42
15
Default value
Domain constraint
Controlling the values in attributes
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 16/42
16
Primary key of parent table
Identifying foreign keys and establishing relationships
Foreign key ofdependent table
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 17/42
17
Data Integrity ControlsReferential integrity – constraint thatensures that foreign key values of atable must match primary key values of
a related table in 1:M relationshipsRestricting:Deletes of primary recordsUpdates of primary recordsInserts of dependent records
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 18/42
18
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 19/42
19
Using and Defining Views Views provide users controlled access to tables
Base Table – table containing the raw dataDynamic View
A “virtual table” created dynamically upon request by a userNo data actually stored; instead data from base table madeavailable to userBased on SQL SELECT statement on base tables or otherviews
Materialized ViewCopy or replication of dataData actually storedMust be refreshed periodically to match the correspondingbase tables
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 20/42
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 21/42
21
Advantages of ViewsSimplify query commands
Assist with data security (but don't rely on viewsfor security, there are more important securitymeasures)Enhance programming productivityContain most current base table dataUse little storage space
Provide customized view for userEstablish physical data independence
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 22/42
22
Disadvantages of ViewsUse processing time each time view isreferencedMay or may not be directly updateable
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 23/42
23
Create Four ViewsCREATE VIEW CUSTOMER_V AS SELECT * FROM CUSTOMER_T;
CREATE VIEW ORDER_V AS SELECT * FROM ORDER_T;
CREATE VIEW ORDER_LINE_V AS SELECT * FROM ORDER_LINE_T;
CREATE VIEW PRODUCT_V AS SELECT * FROM PRODUCT_T;
„* is the wildcard
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 24/42
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 25/42
25
Schema DefinitionControl processing/storage efficiency:
Choice of indexesFile organizations for base tablesFile organizations for indexesData clustering
Statistics maintenanceCreating indexes
Speed up random/sequential access to base tabledata
ExampleCREATE INDEX NAME_IDX ONCUSTOMER_T(CUSTOMER_NAME)This makes an index for the CUSTOMER_NAME field ofthe CUSTOMER_T table
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 26/42
26
Insert Statement
Adds data to a tableInserting a record with all fieldsINSERT INTO CUSTOMER_T VALUES (001, „ContemporaryCasuals , 1355 S. Himes Blvd. , „Gainesville , „FL , 32601);
Inserting a record with specified fieldsINSERT INTO PRODUCT_T (PRODUCT_ID, PRODUCT_DESCRIPTION,PRODUCT_FINISH, STANDARD_PRICE, PRODUCT_ON_HAND) VALUES(1, „End Table , „Cherry , 175, 8);
Inserting records from another tableINSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T WHERESTATE = „CA ;
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 27/42
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 28/42
28
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 29/42
29
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 30/42
30
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 31/42
31
Delete Statement
Removes rows from a tableDelete certain rows
DELETE FROM CUSTOMER_T WHERE STATE= „HI ;
Delete all rowsDELETE FROM CUSTOMER_T;
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 32/42
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 33/42
33
SELECT StatementUsed for queries on single or multiple tablesClauses of the SELECT statement:
SELECTList the columns (and expressions) that should be returned from the query
FROMIndicate the table (s) or view(s) from which data will be obtained
WHEREIndicate the conditions under which a row will be included in the result
GROUP BYIndicate columns to group the results
HAVINGIndicate the conditions under which a group will be included
ORDER BYSorts the result according to specified columns
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 34/42
34
Figure 7-8: SQL statementprocessing order
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 35/42
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 36/42
36
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 37/42
37
SELECT Example using Alias
Alias is an alternative column or table name
SELECTCUST.CUSTOMER ASNAME,CUST.CUSTOMER_ADDRESS
FROM CUSTOMER_VCUSTWHERE NAME = „Home Furnishings ;
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 38/42
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 39/42
SELECT E l
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 40/42
40
SELECT Example – Sorting Results with the ORDER BY Clause
Sort the results first by STATE, and within astate by CUSTOMER_NAME
SELECT CUSTOMER_NAME, CITY, STATEFROM CUSTOMER_VWHERE STATE IN („FL , „TX , „CA , „HI ) ORDER BY STATE, CUSTOMER_NAME;
Note: the IN operator in this example allows you to include rows whoseSTATE value is either FL, TX, CA, or HI. It is more efficient than separateOR conditions
SELECT E l
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 41/42
41
SELECT Example – Categorizing Results Using the GROUP BY Clause
SELECT STATE, COUNT(STATE)FROM CUSTOMER_VGROUP BY STATE;
Note: you can use single-value fields withaggregate functions if they are included in the
GROUP BY clause
Customer table
l
8/11/2019 chap on derivative
http://slidepdf.com/reader/full/chap-on-derivative 42/42
SELECT Example – Qualifying Results by Categories
Using the HAVING Clause
For use with GROUP BY
SELECT STATE, COUNT(STATE)FROM CUSTOMER_VGROUP BY STATEHAVING COUNT(STATE) > 1;
Like a WHERE clause, but it operates on groups (categories),not on individual rows. Here, only those groups with totalnumbers greater than 1 will be included in final result