Download presentation
Presentation is loading. Please wait.
1
A Guide to SQL, Seventh Edition
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 A Guide to SQL, Seventh Edition
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 A Guide to SQL, Seventh Edition
4
Creating a New Table from an Existing Table Possible to create a new table from existing table Use CREATE TABLE command Create a SELECT command to select the desired data You can add query results to the table by placing this SELECT command in an INSERT command A Guide to SQL, Seventh Edition
7
Changing Existing Data in a Table The UPDATE command can change rows where specified conditions are true Command format UPDATE the name of the table to be updated SET the name of the column to be updated = and the new value A Guide to SQL, Seventh Edition
9
Adding New Rows to an Existing Table You can use the existing value in a column to calculate an update The INSERT command can be used to update table data A SELECT command shows the row was successfully added A Guide to SQL, Seventh Edition
12
Commit and Rollback Updates to a table are only temporary and can be cancelled at any time during the current work session COMMIT command saves changes immediately during current session ROLLBACK command reverses the changes made since last COMMIT command or in current work session A Guide to SQL, Seventh Edition
13
Transactions A transaction is a logical unit of work A sequence of steps that accomplish a single task Essential that the entire sequence is completed successfully COMMIT and ROLLBACK commands support transactions A Guide to SQL, Seventh Edition
14
Transaction Support Before starting updates for a transaction, COMMIT any previous updates Complete the updates for the transaction If it cannot be completed, use ROLLBACK If all updates complete, use COMMIT again A Guide to SQL, Seventh Edition
15
Delete Existing Rows from a Table Use the DELETE command to delete data from the database Command format DELETE followed by the table from which the row(s) is to be deleted Next, use a WHERE clause with a condition to select the row(s) to delete All rows satisfying the condition will be deleted A Guide to SQL, Seventh Edition
17
Executing a Rollback ROLLBACK command returns data to its original state All updates made prior to the previous commit are still reflected in the data A Guide to SQL, Seventh Edition
18
Changing a Value in a Column to Null Command for changing the value to null is the same as changing any other value Affected column must be able to accept nulls Use the value NULL as the replacement value A Guide to SQL, Seventh Edition
20
Changing a Table’s Structure Relational DBMS allow changes to table structure Add new tables Delete tables no longer required Add new columns to a table Change physical characteristics of existing columns A Guide to SQL, Seventh Edition
21
ALTER TABLE Command ALTER TABLE command allows for changing a table’s structure Use the ADD clause to add a new column ADD clause is followed by the name of the column to be added, followed by its characteristics A Guide to SQL, Seventh Edition
22
ADD Clause Value is needed in the new column Simplest approach for the DBMS is to assign NULL as the value Some systems allow for the specification of an initial value ADD Cust_Type CHAR(1) INIT = R Others require an UPDATE command A Guide to SQL, Seventh Edition
25
DESCRIBE Command Command describes the table, including the addition of new column Access uses Documentor MySQL uses SHOW COLUMNS A Guide to SQL, Seventh Edition
27
MODIFY Clause MODIFY clause of the ALTER TABLE command changes the characteristics of existing columns Can be used to change a column that currently rejects null values Use NULL in place of NOT NULL A Guide to SQL, Seventh Edition
30
Making Complex Changes Changes to table structure may be beyond the capabilities of DBMS Eliminate column Change column order Combine data from two tables to one Reduce the size of a column Change data type A Guide to SQL, Seventh Edition
31
Making Complex Changes Use the CREATE TABLE command to describe the new table Insert values into new table with the INSERT command and the appropriate SELECT command A Guide to SQL, Seventh Edition
32
Dropping a Table Use the DROP TABLE command to delete a table Permanently removes the table and all its data from the database A Guide to SQL, Seventh Edition
33
Summary CREATE TABLE command is used to make a new table from an existing table UPDATE command to change data INSERT command adds new rows COMMIT command saves changes ROLLBACK reverses changes DELETE command deletes rows SET clause used to make values NULL A Guide to SQL, Seventh Edition
34
Summary DELETE command deletes rows SET clause used to make values NULL Change a specific value to null with a condition to select the row Add a column to a table with ALTER TABLE command and the ADD clause Change a table with the ALTER TABLE command and the MODIFY clause A Guide to SQL, Seventh Edition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.