Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Action Queries CS 320

2 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

3 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

4 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)

5 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')

6 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' )

7 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');

8 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) )

9 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);

10 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

11 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');

12 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;

13 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;

14 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

15 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

16 Fully-populated CANDY database tables CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT

17 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')

18 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')

19 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');

20 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');

21 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);

22 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);

23 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');

24 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');

25 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;

26 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;

27 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.

28 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', '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');

29 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.

30 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;

31 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.

32 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;


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

Similar presentations


Ads by Google