The previous example would need to be broken down into two statements, such as: INSERT INTO Customers FirstName, LastName, State VALUES 'Virginia', 'Jones', 'OH'; INSERT INTO Customers F
Trang 1D A T A B A S E D I F F E R E N C E S : O r a c l e
Oracle doesn’t permit multiple rows to be specified after the VALUES keyword The previous example would need to be broken down into two statements, such as:
INSERT INTO Customers
(FirstName, LastName, State)
VALUES ('Virginia', 'Jones', 'OH');
INSERT INTO Customers
(FirstName, LastName, State)
VALUES ('Clark', 'Woodland', 'CA');
Also note that the order of values after theVALUESkeyword corresponds to the
order of columns listed in the columnlist after the INSERT TO The order in which the columns are listed does not have to be the same as it is in the database
In other words, the above insert just as easily could have been accomplished with this statement:
INSERT INTO Customers
(State, LastName, FirstName)
VALUES
('OH', 'Jones', 'Virginia'),
('CA', 'Woodland', 'Clark')
In the aboveINSERT, we listed the State column first instead of last Again, the order in which columns are listed doesn’t matter
To sum up, the general format for theINSERT INTOstatement is:
INSERT INTO table
(columnlist)
VALUES
(RowValues1),
(RowValues2)
(repeat any number of times)
The columns in columnlist need to correspond to the columns in RowValues Also, if all the columns in columnlist are listed in the same order as they
physi-cally exist in the database, and if there are no auto-increment columns in the Chapter 17 ■ Modifying Data
176
Trang 2table, then theINSERT INTOstatement can be executed without specifying the
columnlist However, this practice is strongly discouraged since it is prone to
error
What happens when not all columns are specified in anINSERT? Simple Those
columns that are not specified are given NULL values For example, let’s say we
want to insert one additional row into the Customers table, for a customer
named Tom Monroe However, we don’t know Tom’s state Here’s theINSERT:
INSERT INTO Customers
(FirstName, LastName)
VALUES
('Tom', 'Monroe')
Afterwards, his row in the table appears as:
CustomerID FirstName LastName State
Since we didn’t specify a value for the State column for this new row, it is given a
NULL value
There are two variations of the INSERT INTO statement The second format
applies to situations where you insert data that is obtained from a SELECT
statement, which means that instead of listing values after a VALUESkeyword,
you substitute aSELECTstatement that obtains similar values
Let’s say that we have another table named CustomerTransactions, which holds
data that we would like to insert into the Customers table The
Customer-Transactions table might look like:
CustomerID State Name1 Name2
Trang 3If we wanted to add all customers in the state of Rhode Island from the CustomerTransactions table to the Customers table, the following would accomplish that objective:
INSERT INTO Customers
(FirstName, LastName, State)
SELECT
Name1,
Name2,
State
FROM CustomerTransactions
WHERE State = 'RI'
After thisINSERT, the Customers table contains:
CustomerID FirstName LastName State
The aboveINSERT simply substituted a SELECT statement for the previously seenVALUES clause As would be expected, Michael Blake didn’t get added to the Customers table, since he is not in Rhode Island Also notice that the column names in the Customers and CustomerTransactions tables are not identical The column names don’t matter as long as the columns are listed in the correct cor-responding order
Deleting Data
Deleting data is much simpler than adding it TheDELETEstatement is used to handle deletes When aDELETEis executed, it deletes entire rows, not individual columns in a row The general format is:
DELETE
FROM table
WHERE condition
Chapter 17 ■ Modifying Data
178
Trang 4Here’s a simple example Let’s say we want to delete rows from the previously
mentioned Customers table if the customer is in Rhode Island The statement to
accomplish this is:
DELETE
FROM Customers
WHERE State = 'RI'
That’s all there is to it If you wanted to test the results of the above DELETE
before executing it, you would simply substitute a SELECT for DELETE, as
follows:
SELECT
COUNT (*)
FROM Customers
WHERE State = 'RI'
This would provide a count of the rows to be deleted, which supplies some level
of validation for the delete
There is one other option for deleting data that is worth mentioning If you want
to delete all the data in a single table, you can employ a TRUNCATE TABLE
statement to delete everything The advantage of theTRUNCATE TABLEover the
DELETEstatement is that it is much faster Unlike theDELETE, theTRUNCATE
TABLEdoesn’t log the results of the transaction We haven’t talked about
data-base log processes, but this is a function that most datadata-bases provide that allows
database administrators to recover databases in the event of system crashes and
other similar problems
If you want to delete all rows in the Customers table, you can issue this
statement:
TRUNCATE TABLE Customers
This has the same result as this statement:
DELETE
FROM Customers
One other slight difference between DELETE and TRUNCATE TABLE is that
TRUNCATE TABLE resets the current values used for auto-increment columns
TheDELETEdoesn’t affect those values
Trang 5Updating Data
The procedure for updating data involves specifying which columns are to be updated, as well as logic for selecting rows The general format for anUPDATE
statement is:
UPDATE table
SET Column1 = Expression1,
Column2 = Expression2
(repeat any number of times)
WHERE condition
This statement is similar to the basic SELECT, except that theSET keyword is used to assign new values to specified columns TheWHERE condition specifies which rows are to be updated, but theUPDATE statement can update multiple columns at the same time If more than one column is being updated, theSET
keyword is only listed once, but a comma must separate all update expressions Starting with a simple example, let’s say we want to change the name of customer William Smith to Bill Smythe His row in the Customers table currently looks like:
CustomerID FirstName LastName State
TheUPDATEstatement to accomplish the change is:
UPDATE Customers
SET FirstName = 'Bill',
LastName = 'Smythe'
WHERE CustomerID = 1
After executing this statement, this row in the Customers table appears as:
CustomerID FirstName LastName State
Notice that the value of the State column is unchanged since that column was not included in the UPDATE statement Also note that the WHERE clause is Chapter 17 ■ Modifying Data
180