Presentation is loading. Please wait.

Presentation is loading. Please wait.

IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.

Similar presentations


Presentation on theme: "IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT."— Presentation transcript:

1 IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Additional Data Manipulation Statements INSERT INTO (add data) UPDATE (modify data) DELETE (remove data) The Cast( ) Function

2 IMS 4212: Data Manipulation 2 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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')

3 IMS 4212: Data Manipulation 3 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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

4 IMS 4212: Data Manipulation 4 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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

5 IMS 4212: Data Manipulation 5 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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.

6 IMS 4212: Data Manipulation 6 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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

7 IMS 4212: Data Manipulation 7 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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 the OrderID and ProductID columns

8 IMS 4212: Data Manipulation 8 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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

9 IMS 4212: Data Manipulation 9 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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

10 IMS 4212: Data Manipulation 10 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu UPDATE Updates one or more record(s) in a table UPDATE TableName SET ColumnName1 = Value1, ColumnName2 = Value2, etc. [WHERE criteria] UPDATE Customers SET ContactName = 'Joe Smith', ContactTitle = 'Sales Associate' WHERE CustomerID = 'AABBC'

11 IMS 4212: Data Manipulation 11 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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

12 IMS 4212: Data Manipulation 12 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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

13 IMS 4212: Data Manipulation 13 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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

14 IMS 4212: Data Manipulation 14 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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 Products SET UnitPrice = UnitPrice * 0.05 FROM Products INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID WHERE Suppliers.Country = 'Germany'

15 IMS 4212: Data Manipulation 15 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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 Customers WHERE CustomerID = 'AABBC' DELETE FROM Customers

16 IMS 4212: Data Manipulation 16 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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)

17 IMS 4212: Data Manipulation 17 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu DELETE FROM—Exercises Delete the records you added on Slide #9 (What order does this need to be done in?)

18 IMS 4212: Data Manipulation 18 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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

19 IMS 4212: Data Manipulation 19 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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 ProductIDName FROM Products Order BY ProductName Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value ': ' to data type int.

20 IMS 4212: Data Manipulation 20 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu Cast( ) (cont.) Now try this version The general format of Cast( ) is CAST(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 ProductIDName FROM Products Order BY ProductName

21 IMS 4212: Data Manipulation 21 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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 clause limits the number of rows to be returned—used here to avoid a large data dump not needed to illustrate the technique

22 IMS 4212: Data Manipulation 22 Dr. Lawrence West, MIS Dept., University of Central Florida lwest@bus.ucf.edu 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


Download ppt "IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT."

Similar presentations


Ads by Google