Download presentation
Presentation is loading. Please wait.
Published byBridget Griffith Modified over 8 years ago
1
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Heikki Topi
2
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 2 SQL Environment Data Definition Language (DDL) Data Definition Language (DDL) Commands that define a database, including creating, altering, and dropping tables and establishing constraints Commands that define a database, including creating, altering, and dropping tables and establishing constraints Data Manipulation Language (DML) Data Manipulation Language (DML) Commands that maintain and query a database Commands that maintain and query a database Data Control Language (DCL) Data Control Language (DCL) Commands that control a database, including administering privileges and committing data Commands that control a database, including administering privileges and committing data
3
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 3 Some SQL Data types (Table 7-2)
4
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 4 Figure 7-4 DDL, DML, DCL, and the database development process
5
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 5 SQL Database Definition Data Definition Language (DDL) Data Definition Language (DDL) Major CREATE statements: Major CREATE statements: CREATE SCHEMA–defines a portion of the database owned by a particular user CREATE SCHEMA–defines a portion of the database owned by a particular user CREATE TABLE–defines a table and its columns CREATE TABLE–defines a table and its columns CREATE VIEW–defines a logical table from one or more views CREATE VIEW–defines a logical table from one or more views
6
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 6 Table Creation Figure 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
7
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 7 The following slides create tables for this enterprise data model
8
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 8 Figure 7-6 SQL database definition commands for Pine Valley Furniture Overall table definitions
9
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 9 Defining attributes and their data types
10
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 10 Non-nullable specification Identifying primary key Primary keys can never have NULL values
11
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 11 Non-nullable specifications Primary key Some primary keys are composite– composed of multiple attributes
12
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 12 Default value Domain constraint Controlling the values in attributes
13
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 13 Primary key of parent table Identifying foreign keys and establishing relationships Foreign key of dependent table
14
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 14 Data Integrity Controls Referential integrity–constraint that ensures that foreign key values of a table must match primary key values of a related table in 1:M relationships Referential integrity–constraint that ensures that foreign key values of a table must match primary key values of a related table in 1:M relationships Restricting: Restricting: Deletes of primary records Deletes of primary records Updates of primary records Updates of primary records Inserts of dependent records Inserts of dependent records
15
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 15 Relational integrity is enforced via the primary- key to foreign- key match Figure 7-7 Ensuring data integrity through updates
16
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 16 Changing and Removing Tables ALTER TABLE statement allows you to change column specifications: ALTER TABLE statement allows you to change column specifications: ALTER TABLE CUSTOMER_T ADD (TYPE VARCHAR(2)) ALTER TABLE CUSTOMER_T ADD (TYPE VARCHAR(2)) DROP TABLE statement allows you to remove tables from your schema: DROP TABLE statement allows you to remove tables from your schema: DROP TABLE CUSTOMER_T DROP TABLE CUSTOMER_T
17
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 17 Insert Statement Adds data to a table Adds data to a table Inserting into a table Inserting into a table INSERT INTO CUSTOMER_T VALUES (001, ‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601); INSERT INTO CUSTOMER_T VALUES (001, ‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601); Inserting a record that has some null attributes requires identifying the fields that actually get data Inserting a record 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, PRODUCT_ON_HAND) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8); INSERT INTO PRODUCT_T (PRODUCT_ID, PRODUCT_DESCRIPTION,PRODUCT_FINISH, STANDARD_PRICE, PRODUCT_ON_HAND) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8); Inserting from another table Inserting from another table INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T WHERE STATE = ‘CA’; INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T WHERE STATE = ‘CA’;
18
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 18 Creating Tables with Identity Columns Inserting into a table does not require explicit customer ID entry or field list INSERT INTO CUSTOMER_T VALUES ( ‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601); New with SQL:2003
19
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 19 Delete Statement Removes rows from a table Removes rows from a table Delete certain rows Delete certain rows DELETE FROM CUSTOMER_T WHERE STATE = ‘HI’; DELETE FROM CUSTOMER_T WHERE STATE = ‘HI’; Delete all rows Delete all rows DELETE FROM CUSTOMER_T; DELETE FROM CUSTOMER_T;
20
Chapter 7 © 2009 Pearson Education, Inc. Publishing as Prentice Hall 20 Update Statement Modifies data in existing rows Modifies data in existing rows UPDATE PRODUCT_T SET UNIT_PRICE = 775 WHERE PRODUCT_ID = 7; UPDATE PRODUCT_T SET UNIT_PRICE = 775 WHERE PRODUCT_ID = 7;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.