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

Slides:



Advertisements
Similar presentations
MySQL. To start go to Login details: login: labuser password:macimd15 – There.
Advertisements

Keys, Referential Integrity and PHP One to Many on the Web.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Populating and Querying tables Insert and mostly View (DML)
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Oracle Data Definition Language (DDL)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
MIS 301 Information Systems in Organizations Dave Salisbury ( )
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Visual Programing SQL Overview Section 1.
Module 7: Modifying Data. Overview Using Transactions Inserting Data Deleting Data Updating Data Performance Considerations.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
IMS 4212: Data Modeling—Attributes and Domains 1 Dr. Lawrence West, Management Dept., University of Central Florida Attributes and Domains.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Populating and Querying tables Insert, Update, Delete and View (DML)
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
SQL server Section 2&3. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
IMS 4212: Data Modeling—Business Rules 1 Dr. Lawrence West, Management Dept., University of Central Florida Business Rules—Topics Intro.
IMS 4212: Intro to Multi-Table SELECT Statements 1 Dr. Lawrence West, MIS Dept., University of Central Florida Multi-Table SELECT Statements—Topics.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
1 DBS201: More on SQL Lecture 2. 2 Agenda Select command review How to create a table How to insert data into a table.
IMS 4212: Indexes (Indices) 1 Dr. Lawrence West, Management Dept., University of Central Florida Indexes—Topics Reasons for concern Data.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Fundamentals of DBMS Notes-1.
How to: SQL By: Sam Loch.
Web Systems & Technologies
Chapter 5 Introduction to SQL.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
MIS2502: Data Analytics SQL – Putting Information Into a Database
SQL Creating and Managing Tables
MIS2502: Data Analytics SQL – Putting Information Into a Database
SQL Creating and Managing Tables
SQL Creating and Managing Tables
DBS201: More on SQL Lecture 3
Insert, Update, Delete Manipulating Data.
MIS2502: Data Analytics SQL – Putting Information Into a Database
Oracle Data Definition Language (DDL)
MIS2502: Data Analytics SQL – Putting Information Into a Database
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
CS122 Using Relational Databases and SQL
DBS201: More on SQL Lecture 3
Presentation transcript:

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

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

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

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

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

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

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

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

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

IMS 4212: Data Manipulation 10 Dr. Lawrence West, MIS Dept., University of Central Florida 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'

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

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

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

IMS 4212: Data Manipulation 14 Dr. Lawrence West, MIS Dept., University of Central Florida 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'

IMS 4212: Data Manipulation 15 Dr. Lawrence West, MIS Dept., University of Central Florida 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

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

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

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

IMS 4212: Data Manipulation 19 Dr. Lawrence West, MIS Dept., University of Central Florida 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.

IMS 4212: Data Manipulation 20 Dr. Lawrence West, MIS Dept., University of Central Florida 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

IMS 4212: Data Manipulation 21 Dr. Lawrence West, MIS Dept., University of Central Florida 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

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