A Guide to SQL, Sixth Edition 1 Chapter 5 Updating Data.

Slides:



Advertisements
Similar presentations
Data Definition Language (DDL)
Advertisements

Virtual training week 4 structured query language (SQL)
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Chapter 8 Embedded SQL.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Introduction to Structured Query Language (SQL)
Concepts of Database Management, 4th Edition, Pratt & Adamski
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Introduction to Structured Query Language (SQL)
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Concepts of Database Management Sixth Edition
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
1 An Introduction to SQL. 2 Objectives  Understand the concepts and terminology associated with relational databases  Create and run SQL commands 
Chapter 3 Single-Table Queries
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
A Guide to SQL, Eighth Edition Chapter Two Database Design Fundamentals.
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
Concepts of Database Management Seventh Edition
Chapter 6 Database Administration
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Computer Literacy BASICS: A Comprehensive Guide to IC 3, 5 th Edition Lesson 23 Getting Started with Access Essentials 1 Morrison / Wells / Ruffolo.
1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Advanced SQL: Triggers & Assertions
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Chapter 8 Embedded SQL.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
A Guide to MySQL 6. 2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
Chapter 3 Table Creation and Management Oracle 10g: SQL.
A Guide to SQL, Sixth Edition 1 Chapter 7 Reports.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
3 A Guide to MySQL.
A Guide to SQL, Eighth Edition
A Guide to SQL, Seventh Edition
Insert, Update and the rest…
Introduction to Oracle9i: SQL
A Guide to SQL, Eighth Edition
Temporary versus Permanent Changes to Data
Lesson 23 Getting Started with Access Essentials
Presentation transcript:

A Guide to SQL, Sixth Edition 1 Chapter 5 Updating Data

2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command Use the COMMIT and ROLLBACK commands to make permanent data updates or to reverse updates

3 Objectives Understand transactions and the role of COMMIT and ROLLBACK in supporting transactions Delete data using the DELETE command Use nulls in UPDATE commands Change the structure of an existing table Drop a table

4 Creating a New Table from an Existing Table It is possible to create a new table from data in an existing table Problem: Create a new table named LEVEL1_CUSTOMER containing the following columns from the CUSTOMER table: CUSTOMER_NUM, CUSTOMER_NAME, BALANCE, CREDIT_LIMIT, and REP_NUM. The columns in the new LEVEL1_CUSTOMER table should have the same characteristics as the corresponding columns in the CUSTOMER table.

5 Creating a New Table from an Existing Table Solution: CREATE TABLE LEVEL1_CUSTOMER (CUSTOMER_NUM CHAR(3) PRIMARY KEY, CUSTOMER_NAME CHAR(35), BALANCE DECIMAL(8,2), CREDIT_LIMIT DECIMAL(8,2), REP_NUM CHAR(2) );

6 Creating a New Table from an Existing Table A new table can be described with the CREATE TABLE command A SELECT command selects data from the CUSTOMER table By placing this SELECT command in an INSERT command, the query results are added to the new table

7 Creating a New Table from an Existing Table Problem: Insert into the LEVEL1_CUSTOMER table the customer number, customer name, balance, credit limit, and rep number for customers with credit limits of $7,500.

8 Creating a New Table from an Existing Table Solution: INSERT INTO LEVEL1_CUSTOMER SELECT CUSTOMER_NUM, CUSTOMER_NAME, BALANCE, CREDIT_LIMIT, REP_NUM FROM CUSTOMER WHERE CREDIT_LIMIT = 7500;

9 Changing Existing Data in a Table The data stored in a database is subject to constant change The UPDATE command is used to change rows on which a specific condition is true Format for the UPDATE command: UPDATE SET = A WHERE clause can be included to indicate the row(s) on which the change is to take place

10 Adding New Rows to an Existing Table The INSERT command can be used to: Add data to the tables Update table data Problem: Add customer number 895 to the LEVEL1_CUSTOMER table. The name is Peter and Margaret’s, the balance is 0, the credit limit is $8,000, and the rep number is 20.

11 Adding New Rows to an Existing Table Solution: INSERT INTO LEVEL1_CUSTOMER VALUES ('895','Peter and Margaret''s', 0, 8000, '20');

12 Commit and Rollback Updates to data are only temporary Updates can be reversed (cancelled) at any time during the current work session Updates become permanent automatically when the DBMS is exited Updates can be saved immediately by executing the COMMIT command

13 Commit and Rollback Updates can be cancelled by executing the ROLLBACK command Updates since the last COMMIT command will be reversed The ROLLBACK command reverses only changes made to the data, not the table’s structure

14 Transactions A transaction is a logical unit of work A transaction can be viewed as a sequence of steps that accomplishes a single task It is essential that the entire sequence is completed successfully

15 Transactions The COMMIT and ROLLBACK commands are used with transactions as follows: Before beginning the updates for a transaction, execute the COMMIT command Complete the updates for the transaction. If any update cannot be completed, execute the ROLLBACK command and discontinue the updates for the current transaction Execute the COMMIT command after completing the final update

16 Deleting Existing Rows from a Table The DELETE command is used to delete data from the database The format for the DELETE command is: DELETE WHERE =

17 Deleting Existing Rows from a Table Problem: In the LEVEL1_CUSTOMER table, change the name of customer 356 to Smith Sport, and then delete customer 895.

18 Deleting Existing Rows from a Table Solution: UPDATE LEVEL1_CUSTOMER SET CUSTOMER_NAME = 'Smith Sport‘ WHERE CUSTOMER_NUM = '356'; DELETE FROM LEVEL1_CUSTOMER WHERE CUSTOMER_NUM = '895'; SELECT * FROM LEVEL1_CUSTOMER;

19 Executing a Rollback Problem: Execute a rollback and then display the data in the LEVEL1_CUSTOMER table Solution: ROLLBACK; SELECT * FROM LEVEL1_CUSTOMER;

20 Changing a Value in a Column to Null The value in a column in an existing row can be changed to null To make this type of change, the affected column must accept nulls If NOT NULL was specified for the column when it was created, then changing a value to null is prohibited

21 Changing a Value in a Column to Null The command for changing the value to null is the same as it would be for changing any other value The value NULL is used as the replacement value

22 Changing Table Structures A table’s structure can be changed by using the ALTER TABLE command To add a new column, the ADD clause of the ALTER TABLE command is used The format for adding a new column is: ALTER TABLE ADD

23 Changing Table Structures Problem: Premiere Products decides to maintain a customer type for each customer in the database. These types are R for regular customers, D for distributors, and S for special customers. Add this information in a new column in the LEVEL1_CUSTOMER table.

24 Changing Table Structures Solution: ALTER TABLE LEVEL1_CUSTOMER ADD CUSTOMER_TYPE CHAR(1);

25 Changing Table Structures The characteristics of existing columns can be changed by using the MODIFY clause of the ALTER TABLE command Problem: The length of the CUSTOMER_NAME column in the LEVEL1_CUSTOMER table is too short. Increase its length to 50 characters. In addition, change the CREDIT_LIMIT column so that it cannot accept nulls.

26 Changing Table Structures Solution: ALTER TABLE LEVEL1_CUSTOMER MODIFY CUSTOMER_NAME CHAR(50); ALTER TABLE LEVEL1_CUSTOMER MODIFY CREDIT_LIMIT NOT NULL;

27 Making Complex Changes Some changes to a table’s structure are beyond the capabilities of some DBMSs Examples include: Eliminate a column Change the column order Combine data from two tables into one

28 Making Complex Changes To make complex changes: Use the CREATE TABLE command to describe the new table Insert values into the new table using the INSERT command combined with an appropriate SELECT command

29 Dropping a Table A table that is no longer needed can be deleted by using the DROP TABLE command Problem: Delete the LEVEL1_CUSTOMER table because it is no longer needed in the Premiere Products database. Solution: DROP TABLE LEVEL1_CUSTOMER;

30 Summary To create a new table from an existing table: Create the new table by using the CREATE TABLE command Use an INSERT command containing a SELECT command to select the desired data from the existing table UPDATE command: changes existing data in a table INSERT command: adds new rows to a table

31 Summary COMMIT command: saves updates ROLLBACK command: reverses updates DELETE command: deletes existing rows To add a column to a table, use the ALTER TABLE command with an ADD clause To change the characteristics of a column, use the ALTER TABLE command with a MODIFY clause

32 SQL Project Five Completed Good Luck H. Zamanzadeh