© 2009 pearson education, inc. publishing as prentice hall 1 unit 6: chapter 7: introduction to sql...
TRANSCRIPT
© 2009 Pearson Education, Inc. Publishing as © 2009 Pearson Education, Inc. Publishing as Prentice HallPrentice Hall 11
UNIT 6: UNIT 6: Chapter 7:Chapter 7:
Introduction to SQLIntroduction to SQLModern Database Modern Database
ManagementManagement
99thth Edition EditionJeffrey A. Hoffer, Mary B. Prescott, Jeffrey A. Hoffer, Mary B. Prescott,
Heikki Topi Heikki Topi
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 22
SQL OverviewSQL Overview Structured Query LanguageStructured Query Language
The standard for relational database The standard for relational database management systems (RDBMS) management systems (RDBMS)
RDBMS: A database management RDBMS: A database management system that manages data as a system that manages data as a collection of tables in which all collection of tables in which all relationships are represented by relationships are represented by common values in related tablescommon values in related tables
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 33
History of SQLHistory of SQL 1970–E. Codd develops relational database 1970–E. Codd develops relational database
conceptconcept 1974-1979–System R with Sequel (later SQL) 1974-1979–System R with Sequel (later SQL)
created at IBM Research Labcreated at IBM Research Lab 1979–Oracle markets first relational DB with SQL1979–Oracle markets first relational DB with SQL 1986–ANSI SQL standard released1986–ANSI SQL standard released 1989, 1992, 1999, 2003–Major ANSI standard 1989, 1992, 1999, 2003–Major ANSI standard
updatesupdates Current–SQL is supported by most major Current–SQL is supported by most major
database vendorsdatabase vendors
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 44
Purpose of SQL StandardPurpose of SQL Standard Specify syntax/semantics for data Specify syntax/semantics for data
definition and manipulationdefinition and manipulation Define data structuresDefine data structures Enable portabilityEnable portability Specify minimal (level 1) and complete Specify minimal (level 1) and complete
(level 2) standards(level 2) standards Allow for later growth/enhancement to Allow for later growth/enhancement to
standardstandard
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 55
Benefits of a Standardized Benefits of a Standardized Relational LanguageRelational Language
Reduced training costsReduced training costs ProductivityProductivity Application portabilityApplication portability Application longevityApplication longevity Reduced dependence on a single Reduced dependence on a single
vendorvendor Cross-system communicationCross-system communication
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 66
SQL EnvironmentSQL Environment CatalogCatalog
A set of schemas that constitute the description of a databaseA set of schemas that constitute the description of a database SchemaSchema
The structure that contains descriptions of objects created by a The structure that contains descriptions of objects created by a user (base tables, views, constraints)user (base tables, views, constraints)
Data Definition Language (DDL)Data Definition Language (DDL) Commands that define a database, including creating, altering, Commands that define a database, including creating, altering,
and dropping tables and establishing constraintsand dropping tables and establishing constraints Data Manipulation Language (DML)Data Manipulation Language (DML)
Commands that maintain and query a databaseCommands that maintain and query a database Data Control Language (DCL)Data Control Language (DCL)
Commands that control a database, including administering Commands that control a database, including administering privileges and committing dataprivileges and committing data
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 77
Figure 7-1A simplified schematic of a typical SQL environment, as described by the SQL: 200n standard
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 88
Some SQL Data typesSome SQL Data types(Table 7-2) (Table 7-2)
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 99
Figure 7-4 DDL, DML, DCL, and the database development process
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1010
SQL Database DefinitionSQL Database Definition Data Definition Language (DDL)Data Definition Language (DDL) Major CREATE statements:Major CREATE statements:
CREATE SCHEMA–defines a portion of the CREATE SCHEMA–defines a portion of the database owned by a particular userdatabase owned by a particular user
CREATE TABLE–defines a table and its CREATE TABLE–defines a table and its columnscolumns
CREATE VIEW–defines a logical table from CREATE VIEW–defines a logical table from one or more viewsone or more views
Other CREATE statements: CHARACTER Other CREATE statements: CHARACTER SET, COLLATION, TRANSLATION, SET, COLLATION, TRANSLATION, ASSERTION, DOMAINASSERTION, DOMAIN
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1111
Table CreationTable CreationFigure 7-5 General syntax for CREATE TABLE
Steps in table creation:
1. Identify data types for attributes
2. Identify columns that can and cannot be null
3. Identify columns that must be unique (candidate keys)
4. Identify primary key–foreign key mates
5. Determine default values
6. Identify constraints on columns (domain specifications)
7. Create the table and associated indexes
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1212
The following slides create tables The following slides create tables for this enterprise data modelfor this enterprise data model
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1313
Figure 7-6 SQL database definition commands for Pine Valley Furniture
Overall table definitions
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1414
Defining attributes and their data types
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1515
Non-nullable specification
Identifying primary key
Primary keys can never have NULL values
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1616
Non-nullable specifications
Primary key
Some primary keys are composite– composed of multiple attributes
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1717
Default value
Domain constraint
Controlling the values in attributes
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1818
Primary key of parent table
Identifying foreign keys and establishing relationships
Foreign key of dependent table
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1919
Data Integrity ControlsData Integrity Controls Referential integrity–constraint Referential integrity–constraint
that ensures that foreign key that ensures that foreign key values of a table must match values of a table must match primary key values of a related primary key values of a related table in 1:M relationshipstable in 1:M relationships
Restricting:Restricting: Deletes of primary recordsDeletes of primary records Updates of primary recordsUpdates of primary records Inserts of dependent recordsInserts of dependent records
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 2020
Relational integrity is enforced via the primary-key to foreign-key match
Figure 7-7 Ensuring data integrity through updates
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 2121
Changing and Removing Changing and Removing TablesTables
ALTER TABLE statement allows you ALTER TABLE statement allows you to change column specifications:to change column specifications: ALTER TABLE CUSTOMER_T ADD (TYPE ALTER TABLE CUSTOMER_T ADD (TYPE
VARCHAR(2))VARCHAR(2)) DROP TABLE statement allows you to DROP TABLE statement allows you to
remove tables from your schema:remove tables from your schema: DROP TABLE CUSTOMER_TDROP TABLE CUSTOMER_T
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 2222
Schema DefinitionSchema Definition Control processing/storage efficiency:Control processing/storage efficiency:
Choice of indexesChoice of indexes File organizations for base tablesFile organizations for base tables File organizations for indexesFile organizations for indexes Data clusteringData clustering Statistics maintenanceStatistics maintenance
Creating indexesCreating indexes Speed up random/sequential access to base table dataSpeed up random/sequential access to base table data ExampleExample
CREATE INDEX NAME_IDX ON CUSTOMER_T(CUSTOMER_NAME)CREATE INDEX NAME_IDX ON CUSTOMER_T(CUSTOMER_NAME) This makes an index for the CUSTOMER_NAME field of the This makes an index for the CUSTOMER_NAME field of the
CUSTOMER_T tableCUSTOMER_T table
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 2323
Insert StatementInsert Statement Adds data to a tableAdds data to a table Inserting into a tableInserting into a table
INSERT INTO CUSTOMER_T VALUES (001, ‘Contemporary INSERT INTO CUSTOMER_T VALUES (001, ‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);32601);
Inserting a record that has some null attributes Inserting a record that has some null attributes requires identifying the fields that actually get requires identifying the fields that actually get datadata INSERT INTO PRODUCT_T (PRODUCT_ID, INSERT INTO PRODUCT_T (PRODUCT_ID,
PRODUCT_DESCRIPTION,PRODUCT_FINISH, STANDARD_PRICE, PRODUCT_DESCRIPTION,PRODUCT_FINISH, STANDARD_PRICE, PRODUCT_ON_HAND) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8);PRODUCT_ON_HAND) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8);
Inserting from another tableInserting from another table INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T
WHERE STATE = ‘CA’;WHERE STATE = ‘CA’;
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 2424
Creating Tables with Identity Creating Tables with Identity ColumnsColumns
Inserting into a table does not require explicit customer ID Inserting into a table does not require explicit customer ID entry or field listentry or field list
INSERT INTO CUSTOMER_T VALUES ( ‘Contemporary INSERT INTO CUSTOMER_T VALUES ( ‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);
New with SQL:2003
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 2525
Delete StatementDelete Statement
Removes rows from a tableRemoves rows from a table Delete certain rowsDelete certain rows
DELETE FROM CUSTOMER_T WHERE DELETE FROM CUSTOMER_T WHERE STATE = ‘HI’;STATE = ‘HI’;
Delete all rowsDelete all rows DELETE FROM CUSTOMER_T;DELETE FROM CUSTOMER_T;
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 2626
Update StatementUpdate Statement
Modifies data in existing rowsModifies data in existing rows
UPDATE PRODUCT_T SET UNIT_PRICE = UPDATE PRODUCT_T SET UNIT_PRICE = 775 WHERE PRODUCT_ID = 7;775 WHERE PRODUCT_ID = 7;
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall© 2009 Pearson Education, Inc. Publishing as Prentice Hall 2727
Merge StatementMerge Statement
Makes it easier to update a table…allows combination of Insert and Update in one statement
Useful for updating master tables with new data