Chapter 08 How to Insert, Update, and Delete Data MIT 22033, Database Management Systems By. S....
-
Upload
clement-perry -
Category
Documents
-
view
214 -
download
0
Transcript of Chapter 08 How to Insert, Update, and Delete Data MIT 22033, Database Management Systems By. S....
Chapter 08 How to Insert,
Update, and Delete Data
MIT 22033, Database Management Systems
By. S. Sabraz Nawaz
MIT 22033 By. S. Sabraz NawazSlide 2
How to use the SELECT INTO statement to createtest tables The INTO clause is a SQL Server extension that lets you create a
new table based on the result set of the SELECT statement. The definitions of the columns in the new table are based on the
columns in the result set. If you use a calculated value in the select list, you must name the
column since that name is used in the definition of the new table. Only column definitions and data are copied to the new table. The
definitions of keys, indexes, default values, and so on, aren’tincluded.
Note The table you name in the INTO clause must not exist. If it does,
you must delete it using the DROP TABLE statement before youexecute the SELECT INTO statement.
MIT 22033 By. S. Sabraz NawazSlide 3
The syntax of the SELECT INTO statementSELECT select_listINTO table_nameFROM table_source[WHERE search_condition][GROUP BY group_by_list][HAVING search_condition][ORDER BY order_by_list]
MIT 22033 By. S. Sabraz NawazSlide 4
A statement that creates a complete copy of the Invoices table
SELECT * INTO InvoiceCopy FROM Invoices (114 row(s) affected)
A statement that creates a partial copy of the Invoices table
SELECT * INTO OldInvoices FROM Invoices WHERE InvoiceTotal – PaymentTotal – CreditTotal = 0 (103 row(s) affected)
MIT 22033 By. S. Sabraz NawazSlide 5
A statement that creates a table with summary rows from the Invoices table
SELECT VendorID, SUM(InvoiceTotal) AS SumOfInvoices INTO VendorBalances FROM Invoices WHERE InvoiceTotal - PaymentTotal - CreditTotal <> 0 GROUP BY VendorID (7 row(s) affected)
A statement that deletes a table DROP TABLE InvoiceCopy
MIT 22033 By. S. Sabraz NawazSlide 6
The syntax of the INSERT statement INSERT [INTO] table_name [(column_list)] [DEFAULT] VALUES (expression_1 [, expression_2]...) [, (expression_1 [, expression_2]...) ...]
How to insert a single row into a table You use the INSERT statement to add a new row to a table.
In the INSERT clause, you specify the name of the table that you want to add a row to, along with an optional column list.
You specify the values to be inserted in the VALUES clause. The values you specify depend on whether you include a column list.
If you don’t include a column list: You must code a value for every column, in the same order as they appear in the table. The only exception is an identity column, which must be omitted.
MIT 22033 By. S. Sabraz NawazSlide 7
How to insert a single row into a table (continued) If you include a column list: You must specify the column values in
the same order as they appear in the column list. You can omitcolumns with default values and columns that accept null values,and you must omit identity columns.
To insert a null value into a column, you can use the NULLkeyword.
To insert a default value into a column, you can use the DEFAULTkeyword.
If all of the table columns are identity columns, columns withdefault values, or columns that allow null values, you can code theDEFAULT keyword at the beginning of the VALUES clause andthen omit the list of values.
MIT 22033 By. S. Sabraz NawazSlide 8
The values for a new row in the Invoices table
Column Value Column Value
InvoiceID (Next available unique ID)
PaymentTotal 0
VendorID 97 CreditTotal 0
InvoiceNumber 456789 TermsID 1
InvoiceDate 8/01/2008 InvoiceDueDate 8/31/2008
InvoiceTotal 8,344.50 PaymentDate null
MIT 22033 By. S. Sabraz NawazSlide 9
An INSERT statement that adds the new row without using a column list
INSERT INTO InvoiceCopy VALUES (97, '456789', '2008-08-01', 8344.50, 0, 0, 1, '2008-08-31', NULL)
An INSERT statement that adds the new row using a column list
INSERT INTO InvoiceCopy (VendorID, InvoiceNumber, InvoiceTotal, PaymentTotal, CreditTotal, TermsID, InvoiceDate, InvoiceDueDate) VALUES (97, '456789', 8344.50, 0, 0, 1, '2008-08-01', '2008-08-31')
The response from the system (1 row(s) affected)
MIT 22033 By. S. Sabraz Nawaz Slide 10
An INSERT statement that adds three new rows INSERT INTO InvoiceCopy VALUES (95, '111-10098', '2008-08-01', 219.50, 0, 0, 1, '2008-08-31', NULL), (102, '109596', '2008-08-01', 22.97, 0, 0, 1, '2008-08-31', NULL), (72, '40319', '2008-08-01', 173.38, 0, 0, 1, '2008-08-31', NULL)
The response from the system (3 row(s) affected)
MIT 22033 By. S. Sabraz Nawaz Slide 11
The definition of the ColorSample table Column name
Data type
Length
Identity
Allow nulls
Default value
ID Int Yes No No
ColorNumber Int No No 0
ColorName VarChar 10 No Yes No
MIT 22033 By. S. Sabraz Nawaz
Murach’s SQL Server 2005, C7
Slide 12
Six INSERT statements for the ColorSampletable
INSERT INTO ColorSample (ColorNumber)VALUES (606)
INSERT INTO ColorSample (ColorName)VALUES ('Yellow')
INSERT INTO ColorSampleVALUES (DEFAULT, 'Orange')
INSERT INTO ColorSampleVALUES (808, NULL)
INSERT INTO ColorSampleVALUES (DEFAULT, NULL)
INSERT INTO ColorSampleDEFAULT VALUES
MIT 22033 By. S. Sabraz Nawaz Slide 13
The ColorSample table after the rows are inserted
MIT 22033 By. S. Sabraz Nawaz Slide 14
The syntax of the UPDATE statement UPDATE table_name SET column_name_1 = expression_1 [, column_name_2 = expression_2]... [FROM table_source [[AS] table_alias] [WHERE search_condition]
How to perform a basic update operation You use the UPDATE statement to modify one or more rows in
the table named in the UPDATE clause.
You name the columns to be modified and the value to be assigned to each column in the SET clause.
You can specify the value for a column by using a literal, an expression, or the DEFAULT or NULL keyword when appropriate.
Warning: If you omit the WHERE clause, all the rows in the table will be updated.
MIT 22033 By. S. Sabraz Nawaz Slide 15
An UPDATE statement that assigns new values to two columns of one row in the InvoiceCopy table
UPDATE InvoiceCopy SET PaymentDate = '2008-09-21', PaymentTotal = 19351.18 WHERE InvoiceNumber = '97/522' (1 row(s) affected)
An UPDATE statement that assigns a new value to one column of all the invoices for a vendor
UPDATE InvoiceCopy SET TermsID = 1 WHERE VendorID = 95 (6 row(s) affected)
An UPDATE statement that uses an arithmetic expression to assign a value to a column
UPDATE InvoiceCopy SET CreditTotal = CreditTotal + 100 WHERE InvoiceNumber = '97/522' (1 row(s) affected)
MIT 22033 By. S. Sabraz Nawaz Slide 16
How to use subqueries in an UPDATE statement You can use a subquery in the SET clause to return the value that’s
assigned to a column.
You can use a subquery in the FROM clause to identify the rows that are available for update. Then, you can refer to the derived table in the SET and WHERE clauses.
You can use a subquery in the WHERE clause to provide one or more values used in the search condition.
MIT 22033 By. S. Sabraz Nawaz Slide 17
An UPDATE statement that updates all the invoices for a vendor based on the vendor’s name
UPDATE InvoiceCopy SET TermsID = 1 WHERE VendorID = (SELECT VendorID FROM VendorCopy WHERE VendorName = 'Pacific Bell') (6 row(s) affected)
MIT 22033 By. S. Sabraz Nawaz Slide 18
The syntax of the DELETE statement DELETE [FROM] table_name [FROM table_source] [WHERE search_condition]
How to perform a basic delete operation You can use the DELETE statement to delete one or more rows
from the table you name in the DELETE clause.
You specify the conditions that must be met for a row to be deleted in the WHERE clause.
You can specify additional criteria for the delete operation in the FROM clause.
Warning: If you omit the WHERE clause, all the rows in the table will be deleted.
MIT 22033 By. S. Sabraz Nawaz Slide 19
A DELETE statement that removes a single rowfrom the InvoiceCopy table
DELETE InvoiceCopyWHERE InvoiceID = 115(1 row(s) affected)
A DELETE statement that removes all the invoicesfor a vendor
DELETE InvoiceCopyWHERE VendorID = 37(3 row(s) affected)
MIT 22033 By. S. Sabraz Nawaz Slide 20
A DELETE statement that removes all paid invoices
DELETE InvoiceCopy WHERE InvoiceTotal - PaymentTotal - CreditTotal = 0 (103 row(s) affected)
A DELETE statement that removes all the rows from the InvoiceCopy table
DELETE InvoiceCopy (114 row(s) affected)