Action Queries CS 320. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language.

Slides:



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

Multiple Table Queries 2: Outer Joins, Self Joins, Nested Queries, and Views CS 320.
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
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”
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Using SQL Queries to Insert, Update, Delete, and View Data © Abdou Illia MIS Spring 2015 Wednesday 1/28/2015 Chapter 3A.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
ASP.NET Programming with C# and SQL Server First Edition
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Working with Tables: Data Management and Retrieval Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Introduction to MySQL Lab no. 10 Advance Database Management System.
CSC 2720 Building Web Applications Database and SQL.
Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
SQL SELECT QUERIES CS 260 Database Systems. Overview  Introduction to SQL  Single-table select queries  Using the DBMS to manipulate data output 
Creating PHPs to Insert, Update, and Delete Data CS 320.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Information Building and Retrieval Using MySQL Track 3 : Basic Course in Database.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
Visual Programing SQL Overview Section 1.
Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
Sql DDL queries CS 260 Database Systems.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
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.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
3 A Guide to MySQL.
Fundamentals of DBMS Notes-1.
Introduction to Oracle9i: SQL
ORACLE SQL Developer & SQLPLUS Statements
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
Manipulating Data.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
MySQL Database System Installation Overview SQL summary
SQL (Structured Query Language)
Presentation transcript:

Action Queries CS 320

Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language (DML)  Used to insert, update, delete, and view the data in database objects  Control Language (CL)  Used to create database transactions

SQL INSERT command 2 approaches:  Insert a value for every table field  Insert values for selected table fields Basic rules for both approaches:  You must insert a unique value for primary key fields  You must provide a value for fields with NOT NULL constraints

Inserting a value for every field  You must include a value for every field  You must list the values in the correct order  To find the correct order, look at the table in the Object Browser  Why?  The DBMS is expecting certain data types for each field. If the data types are not correct an error occurs INSERT INTO tablename VALUES(value1, value2, …) INSERT INTO candy_product VALUES (6, 'Chocolate Covered Ants', 6.25, 7.50)

Inserting values for selected fields  Syntax:  You can insert the field names and corresponding values in any order, but …  The names specified before the VALUES clause and the corresponding values following the VALUES clause must be in the same order!  This is the preferred way to do an insert  Easier to maintain – makes it easy to see what fields are being inserted into  Your command won't "break" if someone adds a new field to the table INSERT INTO tablename (field1, field2, …) VALUES(value1, value2, …) INSERT INTO candy_customer (cust_id, cust_name) VALUES (100, 'Joe Jones')

How do you specify different data type values? Numeric values (SMALLINT, INT, BIGINT, FLOAT, DOUBLE, DECIMAL, …)  Type the number ( 3, 2.75 ) Character values (CHAR, VARCHAR)  Enclose characters in single quotation marks ( 'Joline', 'Caramel Crunch' )  For character fields with embedded quotation marks, type the escape backslash (\) followed by the quotation mark: ( 'Joline\'s White Chocolate Fudge' )

How do you specify date data values? Just as you did in search conditions:  As a quoted string in YYYY-MM-DD format Example INSERT INTO candy_purchase VALUES (1, 1, 5, ' ', ' ', 3.5, 'PAID');

Review: Surrogate Keys A surrogate key is a primary key field created solely for the purpose of being a unique identifier in a database You create surrogate key columns in MySQL using an AUTO_INCREMENT column: CREATE TABLE candy_customer (cust_id BIGINT AUTO_INCREMENT PRIMARY KEY, cust_name VARCHAR(30) )

Inserting Records into AUTO_INCREMENT Columns  Use the INSERT command format that specifies selected field names  Omit inserting the surrogate key value  The DBMS automatically inserts the next value in the sequence INSERT INTO candy_product (prod_desc, prod_cost, prod_price) VALUES ('Gummy Worms', 8.50, 12.50);

CANDY_PURCHASE CANDY_PRODUCT Inserting Foreign Key Values Insert them just like any other value, except …  You MUST have inserted the parent record first! Parent record Child record

Inserting NULL values If inserting a value for every field, use the keyword NULL If inserting values for selected fields, omit the field INSERT INTO candy_product VALUES (7, 'Everlasting Gopstoppers', NULL, NULL); INSERT INTO candy_purchase (prod_id, prod_desc) VALUES (7, 'Everlasting Gopstoppers');

Updating Records General syntax: Notes:  Records can be updated in only one table at a time  You can update multiple records in the same table if they all match the search condition UPDATE tablename SET field1 = new_value, field2 = new_value, … WHERE search_condition(s); UPDATE candy_product SET prod_desc = 'Chocolate Obsession', prod_price = 8.5 WHERE prod_id = 6; UPDATE candy_product SET prod_price = prod_price * 1.1 WHERE prod_price > 10;

Deleting Records  Syntax:  Notes:  Deletes multiple records if search condition specifies multiple records  If the search condition is omitted, all table records are deleted  You can’t delete a record in a parent table that is referenced as a foreign key DELETE FROM tablename WHERE search_condition; DELETE FROM candy_product WHERE prod_id = 6;

Safe Mode in MySQL By default, MySQL executes UPDATE and DELETE commands in “Safe Mode”  Forbids statements in which the WHERE clause uses a non-key field in the search condition  Forbids statements in which the WHERE clause is omitted

Disabling Safe Mode Click Edit – Preferences Select the SQL Editor tab, then clear the “Safe Updates” check box Click OK Click Query – Reconnect to Server to make change take affect

Fully-populated CANDY database tables CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT

Test Yourself: Assume you have a fully- populated CANDY database. What will happen when you execute the following command? a. The command will succeed b. The command will fail INSERT INTO candy_cust_type VALUES ('W', 'Wholesale')

Test Yourself: Assume you have a fully- populated CANDY database. What will happen when you execute the following command? a. The command will succeed b. The command will fail INSERT INTO candy_cust_type VALUES ('W', 'Wholesale')

Test Yourself: Assume you have a fully- populated CANDY database. What will happen when you execute the following command? a. The command will succeed b. The command will fail INSERT INTO candy_purchase VALUES (10, 5, 2, ' ', 75, 'NOT PAID');

Test Yourself: Assume you have a fully- populated CANDY database. What will happen when you execute the following command? a. The command will succeed b. The command will fail INSERT INTO candy_purchase VALUES (10, 5, 2, ' ', 75, 'NOT PAID');

Test Yourself: Assume you have a fully- populated CANDY database. What will happen when you execute the following command? a. The command will succeed b. The command will fail INSERT INTO candy_purchase VALUES (10, 5, 2, ' ', ' ', 'NOT PAID', 12);

Test Yourself: Assume you have a fully- populated CANDY database. What will happen when you execute the following command? a. The command will succeed b. The command will fail INSERT INTO candy_purchase VALUES (10, 5, 2, ' ', ' ', 'NOT PAID', 12);

Test Yourself: Assume you have a fully- populated CANDY database. What will happen when you execute the following command? a. The command will succeed b. The command will fail INSERT INTO candy_purchase (purch_id, cust_id, prod_id, purch_pounds, purch_date, purch_delivery_date, purch_status) VALUES (10, 5, 2, 12, ' ', ' ', 'NOT PAID');

Test Yourself: Assume you have a fully- populated CANDY database. What will happen when you execute the following command? a. The command will succeed b. The command will fail INSERT INTO candy_purchase (purch_id, cust_id, prod_id, purch_pounds, purch_date, purch_delivery_date, purch_status) VALUES (10, 5, 2, 12, ' ', ' ', 'NOT PAID');

Test Yourself: Assume you have a fully- populated CANDY database. How many records will the following command delete? a. 3 b. 4 c. 10 d. None of the above DELETE FROM candy_customer;

Test Yourself: Assume you have a fully- populated CANDY database. How many records will the following command delete? a. 3 b. 4 c. 10 d. None of the above DELETE FROM candy_customer;

Your Turn #1: INSERT If necessary, run the CANDY script to create a fully-populated CANDY database. Then, create a new MySQL script that contains the commands for the following action queries: 1. Write a command to add a new record for yourself in the CANDY_CUSTOMER table. Use the auto increment field to generate the surrogate key value, and use values of your choice for other table fields.. 2. Write a command to add a new product to the CANDY_PRODUCT table. Use the auto increment field to generate the surrogate key value, and use values of your choice for other table fields. 3. Write a command to add a new purchase record to the CANDY_PURCHASE table that references the new customer and product records that you just inserted. (You will need to run a SELECT command to determine the CUST_ID and PROD_ID foreign key values). Use values of your choice for other table fields, except insert values of NULL for the PURCH_DATE and PURCH_DELIVERY_DATE fields.

Your Turn #1: Solution USE candy; INSERT INTO candy_customer (cust_name, cust_type, cust_addr, cust_zip, cust_phone, cust_username, cust_password) VALUES ('Joline Morrison', 'P', '34 Mountain Shadow Road', '81122', ' ', 'morrisjp', '1111'); INSERT INTO candy_product (prod_desc, prod_cost, prod_price) VALUES ('Fudge Mountain', 7.50, 8.50); INSERT INTO candy_purchase (purch_id, prod_id, cust_id, purch_pounds, purch_status) VALUES (10, 6, 11, 200, 'PAID');

Your Turn #2: UPDATE Add the following commands to the MySQL script you just created: 1. Write a command to update the CUST_ZIP field of your CANDY_CUSTOMER record to an alternate value. 2. Write a command to update the PURCH_DATE field of your CANDY_PURCHASE record so it displays today's date. (Use the CURRENT_DATE() function to retrieve the current system date.) 3. Write a command to increase the PROD_COST and PROD_PRICE values of your CANDY_PRODUCT record by $1. Use an addition arithmetic operation to do this.

Your Turn #2: Solution UPDATE candy_customer SET cust_zip = '81113' WHERE cust_id = 11; UPDATE candy_purchase SET purch_date = CURRENT_DATE() WHERE purch_id = 10 AND prod_id = 6; UPDATE candy_product SET prod_cost = prod_cost + 1, prod_price = prod_price + 1 WHERE prod_id = 6;

Your Turn #3: DELETE Add the following commands to the MySQL script you just created: 1. Write commands to delete all of the records you inserted. Be sure to delete the records in the correct order so you delete child records before you delete parent records.

Your Turn #3: Solution DELETE FROM candy_purchase WHERE purch_id = 10 AND prod_id = 6; DELETE FROM candy_product WHERE prod_id = 6; DELETE FROM candy_customer WHERE cust_id = 11;