The Language of SQL- P39 doc

5 117 0
The Language of SQL- P39 doc

Đang tải... (xem toàn văn)

Thông tin tài liệu

DATABASE DIFFERENCES: Oracle 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 the VALUES keyword corresponds to the order of columns liste d 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 above INSERT, 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 the INSERT INTO statement 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 Data176 table, then the INSERT INTO statement 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 an INSERT? Sim ple. 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 the INSERT: INSERT INTO Customers (FirstName, LastName) VALUES ('Tom', 'Monroe') Afterwards, his row in the table appears as: CustomerID FirstName LastName State 6 Tom Monroe NULL 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 VALUES keyword, you substitute a SELECT statement that obta ins 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 Nam e2 1 RI Susan Harris 2 DC Michael Blake 3 RI Alan Canter Inserting Data 177 If 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 this INSERT, the Customers table contains: CustomerID FirstName LastName State 1 William Smith IL 2 Natalie Lopez WI 3 Brenda Harper NV 4 Virginia Jones OH 5 Clark Woodland CA 6 Tom Monroe NULL 7 Susan Harris RI 8 Alan Canter RI The above INSERT simply substituted a SELECT statement for the previously seen VALUES 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. The DELETE statement is used to handle deletes. When a DELETE is executed, it deletes entire rows, not individual columns in a row. The general format is: DELETE FROM table WHERE condition Chapter 17 ■ Modifying Data178 Here’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 wa nt to delete all the data in a single table, you can employ a TRUNCATE TABLE statement to delete everything. The advantage of the TRUNCATE TABLE over the DELETE statement is that it is much faster. Unlike the DELETE, the TRUNCATE TABLE doesn’t log the results of the transaction. We haven’t talked about data- base log processes, but this is a function that most databases provi de 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. The DELETE doesn’t affect those values. Deleting Data 179 Updating 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 an UPDATE 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 the SET keyword is used to assign new values to specified columns. The WHERE condition specifies which rows are to be updated, but the UPDATE statement can update multiple columns at the same time. If more than one colu mn is being updated, the SET 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 1 William Smith IL The UPDATE statement 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 1 Bill Smythe IL 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 Data180 . the order of values after the VALUES keyword corresponds to the order of columns liste d in the columnlist after the INSERT TO. The order in which the columns are listed does not have to be the. delete everything. The advantage of the TRUNCATE TABLE over the DELETE statement is that it is much faster. Unlike the DELETE, the TRUNCATE TABLE doesn’t log the results of the transaction. We. 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 wa nt to delete all the data

Ngày đăng: 05/07/2014, 05:20

Mục lục

    Chapter 1 Relational Databases and SQL

    Microsoft SQL Server, Oracle, and MySQL

    Primary and Foreign Keys

    The Significance of SQL

    Chapter 2 Basic Data Retrieval

    Column Names with Embedded Spaces

    Chapter 3 Calculations and Aliases

    The Function of Functions

    Sorting in Ascending Order

    Sorting in Descending Order

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan