32
Action Queries CS 320

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

Embed Size (px)

Citation preview

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 fieldsYou 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, '2011-09-06', '2011-09-06', 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);

PURCH_ID PROD_ID CUST_IDPURCH_ DATE

PURCH_ DELIVERY_DATE

PURCH_ POUNDS

PURCH_ STATUS

1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID

CANDY_PURCHASE

CANDY_PRODUCTPROD_ID PROD_DESC PROD_COSTPROD_PRICE

1 Celestial Cashew Crunch 7.45$ 10.00$

2 Unbrittle Peanut Paradise 5.75$ 9.00$

3 Mystery Melange 7.75$ 10.50$

4 Millionaire’s Macadamia Mix 12.50$ 16.00$

5 Nuts Not Nachos 6.25$ 9.50$

Inserting Foreign Key Values Insert them just like any other value, except …

You MUST have inserted the parent record first!

Parentrecord

Childrecord

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_productSET prod_desc = 'Chocolate Obsession',prod_price = 8.5WHERE prod_id = 6;

UPDATE candy_productSET prod_price = prod_price * 1.1WHERE 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 tablenameWHERE search_condition;

DELETE FROM candy_productWHERE 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

CUST_ID CUST_NAME CUST_TYPE CUST_ADDR CUST_ZIP CUST_PHONE CUST_USERNAME CUST_PASSWORD

1 Jones, Joe P 1234 Main St. 91212 434-1231 jonesj 12342 Armstrong,Inc. R 231 Globe Blvd. 91212 434-7664 armstrong 33333 Sw edish Burgers R 1889 20th N.E. 91213 434-9090 sw edburg 23534 Pickled Pickles R 194 CityView 91289 324-8909 pickpick 53335 The Candy Kid W 2121 Main St. 91212 563-4545 kidcandy 23516 Waterman, Al P 23 Yankee Blvd. 91234 w ateral 89007 Bobby Bon Bons R 12 Nichi Cres. 91212 434-9045 bobbybon 30118 Crow sh, Elias P 7 77th Ave. 91211 434-0007 crow el 10339 Montag, Susie P 981 Montview 91213 456-2091 montags 9633

10 Columberg Sw eets W 239 East Falls 91209 874-9092 columsw e 8399

PURCH_ID PROD_ID CUST_IDPURCH_ DATE

PURCH_ DELIVERY_DATE

PURCH_ POUNDS

PURCH_ STATUS

1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID

PROD_ID PROD_DESC PROD_COSTPROD_PRICE

1 Celestial Cashew Crunch 7.45$ 10.00$

2 Unbrittle Peanut Paradise 5.75$ 9.00$

3 Mystery Melange 7.75$ 10.50$

4 Millionaire’s Macadamia Mix 12.50$ 16.00$

5 Nuts Not Nachos 6.25$ 9.50$

CUST_TYPE_IDCUST_TYPE_DESC

P Private

R Retail

W Wholesale

CANDY_CUSTOMER

CANDY_PURCHASECANDY_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, '2011-07-14', 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, '2011-07-14', 75, 'NOT PAID');

PURCH_ID PROD_ID CUST_IDPURCH_ DATE

PURCH_ DELIVERY_DATE

PURCH_ POUNDS

PURCH_ STATUS

1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 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, '2011-07-14', '2011-07-15', '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, '2011-07-14', '2011-07-15', 'NOT PAID', 12);

PURCH_ID PROD_ID CUST_IDPURCH_ DATE

PURCH_ DELIVERY_DATE

PURCH_ POUNDS

PURCH_ STATUS

1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 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, '2011-07-14', '2011-07-15', '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, '2011-07-14', '2011-07-15', '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: SolutionUSE 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', '7155555555', '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_customerSET cust_zip = '81113'WHERE cust_id = 11;

UPDATE candy_purchaseSET purch_date = CURRENT_DATE()WHERE purch_id = 10 AND prod_id = 6;

UPDATE candy_productSET prod_cost = prod_cost + 1, prod_price = prod_price + 1WHERE 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: SolutionDELETE FROM candy_purchaseWHERE purch_id = 10 AND prod_id = 6;

DELETE FROM candy_productWHERE prod_id = 6;

DELETE FROM candy_customerWHERE cust_id = 11;