ims 4212: data manipulation 1 dr. lawrence west, mis dept., university of central florida...

22
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida [email protected] Additional Data Manipulation Statements INSERT INTO (add data) UPDATE (modify data) DELETE (remove data) The Cast( ) Function

Upload: debra-charles

Post on 13-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

1Dr. Lawrence West, MIS Dept., University of Central [email protected]

Additional Data Manipulation Statements

• INSERT INTO (add data)

• UPDATE (modify data)

• DELETE (remove data)

• The Cast( ) Function

Page 2: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

2Dr. Lawrence West, MIS Dept., University of Central [email protected]

INSERT INTO

• Adds data one record at a time to a table

INSERT INTO TableName (List of columns)VALUES (List of values)

INSERT INTO Customers (CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax)VALUES ('TESTA', 'TestCompany', 'Joe Test', 'Assistant Sales Rep', '123 Test Street', 'Testville', 'OH', '55555', 'USA', '(555) 555-5555', '(555) 555-5556')

Page 3: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

3Dr. Lawrence West, MIS Dept., University of Central [email protected]

INSERT INTO—List of Columns

• Only columns to which data is being added need be listed

– Omitted columns will be left NULL or the default value will be used

– If a column is not nullable and does not have a default value it must be listed and receive a value

• Columns may be listed in any order

– But it is good practice to keep them in natural order

• If adding a value to every column the list of columns may be omitted

Page 4: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

4Dr. Lawrence West, MIS Dept., University of Central [email protected]

INSERT INTO—List of Columns (cont.)

• You may not add a value to an identity attribute

– This column may not be listed

• When the INSERT INTO executes the value in the identity column will be automatically determined by the DBMS

Try it out—Add a record to the Shippers table

Page 5: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

5Dr. Lawrence West, MIS Dept., University of Central [email protected]

INSERT INTO—List of Values

• There must be one value for each listed column

• Values must be compatible with the column's data type

– Strings must have delimiters

– Datetime values must have delimiters and must be in valid datetime format

– Numeric values must not have delimiters

• Values will be put into the column based on their sequential order in the list of columns and list of values

– First value to first column listed

– Second value to second column, etc.

Page 6: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

6Dr. Lawrence West, MIS Dept., University of Central [email protected]

INSERT INTO—List of Values (cont.)

• Rules for data conversion

– The length of string or text data may not exceed the column size

– The size of numeric values must not exceed the capacity of the numeric data type for the field

– Decimal values will be truncated (not rounded) if they are added to an integer field

• If the list of columns is omitted the values in the list of values will go, in sequence, into the columns as they are listed in the table's design view

Page 7: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

7Dr. Lawrence West, MIS Dept., University of Central [email protected]

INSERT INTO—Foreign Keys

• If a column in the table is a foreign key…

• … and if referential integrity is being enforced

• The value provided for the foreign key column must appear in the primary key column for the related parent table

– We will spend considerable effort in our application logic and interface design to prevent this problem

Try it out—Add a record to the [Order Details] table using the value of 1 (one) for both theOrderID and ProductID columns

Page 8: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

8Dr. Lawrence West, MIS Dept., University of Central [email protected]

INSERT INTO—Closing Notes

• The "INTO" portion of INSERT INTO is now syntactically optional

– It is not optional in my class…

– … because I am old and cranky and set in my ways

• SQL provides bulk upload capabilities for transferring existing (often external) data into a table

– Beyond the scope of this class

• Also SELECT INTO can create a new table based on the results of a SELECT query

– Often used for temporary tables

Page 9: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

9Dr. Lawrence West, MIS Dept., University of Central [email protected]

INSERT INTO--Exercises

• When adding new records to the Orders table what restrictions exist on this table?

• Write the SQL to add a new record to the Orders table

• Write the SQL to add a new record to the [Order Details] table for the order you created in the preceding statement

Page 10: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

10Dr. Lawrence West, MIS Dept., University of Central [email protected]

UPDATE

• Updates one or more record(s) in a table

UPDATE TableNameSET ColumnName1 = Value1, ColumnName2 = Value2, etc.[WHERE criteria]

UPDATE CustomersSET ContactName = 'Joe Smith', ContactTitle = 'Sales Associate'WHERE CustomerID = 'AABBC'

Page 11: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

11Dr. Lawrence West, MIS Dept., University of Central [email protected]

UPDATE—Values

• Only the columns needing changes need to be listed

– In applications it is often practical to write statements to update every column

• Values passed to columns must satisfy all of the rules for inserted values

– Data types

– Cannot update identity primary keys

– Foreign key constraints (at parent and child levels)

– Nullability

• Values may be calculated and include functions as part of the calculation

Page 12: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

12Dr. Lawrence West, MIS Dept., University of Central [email protected]

UPDATE—Where Clauses

• The WHERE clause in an UPDATE statement is optional

– If omitted all records in the table will have the update applied (rarely desired)

• The WHERE clause will often test the PK value to update a single record

• All rules for WHERE clauses that apply to SELECT statement WHERE clauses apply to UPDATE statements

Page 13: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

13Dr. Lawrence West, MIS Dept., University of Central [email protected]

UPDATE—Exercises

• Change the Contact Name for Supplier #5 to be your name

• Change the quantity of Product #51 sold as part of Order #10250 to 30 in the Order Details table

• Raise the current selling price of every product costing more than $20.00 by 5% as long as the product is not discontinued

Page 14: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

14Dr. Lawrence West, MIS Dept., University of Central [email protected]

UPDATE—Joining Tables

• Even though UPDATE must act on only one table it is possible to join tables in an UPDATE statement in order to test criteria in a WHERE clause that can't be tested in the table being updated

UPDATE ProductsSET UnitPrice = UnitPrice * 0.05FROM Products INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierIDWHERE Suppliers.Country = 'Germany'

Page 15: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

15Dr. Lawrence West, MIS Dept., University of Central [email protected]

DELETE FROM

• DELETE deletes rows from a table (No kidding?)

• Leaving out the WHERE clause deletes all rows (careful!!)

DELETE FROM TableName[WHERE criteria]

DELETE FROM CustomersWHERE CustomerID = 'AABBC'

DELETE FROM Customers

Page 16: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

16Dr. Lawrence West, MIS Dept., University of Central [email protected]

DELETE FROM—Notes

• The "FROM" portion is syntactically optional in some DBMS

– It is not optional in my class

– See slide 8

• Table joining for DELETE FROM statements can be performed as it is in UPDATE statements

• There are other ways to remove data from tables

– TRUNCATE removes all data without logging

– DROP TABLE removes the entire table (which can then be rebuilt using DDL SQL)

Page 17: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

17Dr. Lawrence West, MIS Dept., University of Central [email protected]

DELETE FROM—Exercises

• Delete the records you added on Slide #9(What order does this need to be done in?)

Page 18: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

18Dr. Lawrence West, MIS Dept., University of Central [email protected]

Function of the Day—Cast( )

• There are many times when we need to change the data type of data in the result set without changing the original stored data type

• SQL Server gives us two ways to do this

– Cast( ) is an ANSI standard function with no formatting capabilities

– Convert( ) is not ANSI standard but provides some formatting capabilities

• You can always take care of formatting in your application

Page 19: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

19Dr. Lawrence West, MIS Dept., University of Central [email protected]

Cast( ) (cont.)

• Loading Combo Boxes: We will often want two column queries that we will use to load combo boxes

– One column is the PK and is not shown to the user

– One is a column that will help the user make a choice

• Try this query that wants to show the user the PK value along with the product name

SELECT ProductID, ProductID + ': ' + ProductName AS ProductIDNameFROM ProductsOrder BY ProductNameMsg 245, Level 16, State 1, Line 1Conversion failed when converting the varchar value ': ' to data type int.

Page 20: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

20Dr. Lawrence West, MIS Dept., University of Central [email protected]

Cast( ) (cont.)

• Now try this version

• The general format of Cast( ) isCAST(value AS datatype)

– Value can be a field name or any other value

– Datatype is a data type available for table design

SELECT ProductID, CAST(ProductID AS char(3)) + ': ' + ProductName AS ProductIDNameFROM ProductsOrder BY ProductName

Page 21: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

21Dr. Lawrence West, MIS Dept., University of Central [email protected]

Cast( ) & Dates

• Cast( ) actually applies some formatting when dates are converted to strings

SELECT Top 10 OrderDate, CAST(OrderDate AS varchar) AS 'Order Date'FROM ORDERS

"Top 10" in the SELECT clauselimits the number of rows tobe returned—used here toavoid a large data dump notneeded to illustrate the technique

Page 22: IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT

IMS 4212: Data Manipulation

22Dr. Lawrence West, MIS Dept., University of Central [email protected]

Cast( ) and UNION Queries

• Cast( ) can be very useful in UNION queries

– Joins the results of two different queries into one result set

– Data columns must be of compatible types

– We will cover UNIONs later this semester

• Try these:

– Display the product name and the units in stock as one column for all products that are not discontinued

– Display the OrderID and ProductID from the Order Details table combined as one column