Insertions, Deletions, and Updates Lecture 5 Robb T. Koether Hampden-Sydney College Wed, Jan 24, 2018 Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 1 / 17
1 Insertions 2 Deletions 3 Updates Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 2 / 17
Insertions The INSERT Statement INSERT INTO table_name(attrib_list) VALUES(list_of_values); The INSERT statement will insert a new tuple into a table. The list of values must contain one value for each attribute in the attribute list. If the attribute list is omitted, then one value for each attribute in the relation is required. Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 3 / 17
Insertions Inserting Tuples INSERT INTO departments VALUES ( Maintenance, 4, 222334444 ); Insert a new department into the departments table. If we were using foreign keys, then it would be required that there be an employee (already) in the employees table with Social Security number 222-33-4444. Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 4 / 17
Insertions Inserting Multiple Tuples INSERT INTO employees VALUES ( Joe, Plumber, 222334444, 1967-07-04, M, 60000.00, 4), ( Jim, Wilson, 333445555, 1992-02-01, M, 25000.00, 4), ( John, Anderson, 444556666, 1991-10-31, M, 20000.00, 4); Multiple tuples may be added in a single INSERT statement. Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 5 / 17
Insertions Default Values INSERT INTO employees(fname, lname) VALUES ( Bob, Sponge ); Query OK, 1 row affected, 2 warnings (0.00 sec) SELECT * FROM employees WHERE fname = Bob AND lname = Sponge ; +-------+--------+-----+-------+------+--------+------+ fname lname ssn bdate sex salary dept +-------+--------+-----+-------+------+--------+------+ Bob Sponge NULL NULL NULL 0 +-------+--------+-----+-------+------+--------+------+ If attributes are omitted, then their default values are used. Note the warnings. Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 6 / 17
Not-null Attributes Warnings SHOW WARNINGS; +---------+------+-------------------------------------------+ Level Code Message +---------+------+-------------------------------------------+ Warning 1364 Field ssn doesn t have a default value Warning 1364 Field dept doesn t have a default value +---------+------+-------------------------------------------+ Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 7 / 17
Insertions Inserting Tuples INSERT INTO employees VALUES ( Bob, Sponge ); ERROR 1136 (21S01): Column count doesn t match value count at row 1 The above command produces an error message. Why? Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 8 / 17
Inserting from a SELECT Statement Inserting from a SELECT Statement CREATE TABLE dept_2_emps ( fname CHAR(20), lname CHAR(20), ssn CHAR(11) PRIMARY KEY ); It is possible to insert into one table the tuples returned by a SELECT statement. First, we create the table into which we insert. The schemas must match. Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 9 / 17
Inserting from a SELECT Statement Inserting from a SELECT Statement INSERT INTO dept_2_emps SELECT fname, lname, ssn FROM employees WHERE dept = 2; SELECT * FROM dept_2_emps; +----------+---------+-----------+ fname lname ssn +----------+---------+-----------+ Alice Smith 123456789 Jennifer Wallace 321549876 Ernest Roth 642189753 John Kohler 789012345 Raymond Jones 963418527 +----------+---------+-----------+ Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 10 / 17
Deletions The DELETE Statement DELETE FROM table_name WHERE predicate; The DELETE statement allows us to delete certain tuples from a table. All tuples that satisfy the predicate will be deleted. If the WHERE clause is used, then the deletion applies only to those tuples that satisfy the predicate.. Otherwise, all tuples are deleted and this is not reversible. Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 11 / 17
Deletions Conditional Deletion DELETE FROM employees WHERE dept = 4; Query OK, 3 rows affected (0.00 sec) DELETE FROM departments WHERE dept = 4; Query OK, 1 row affected (0.00 sec) The first statement will delete all employees in Department 4 from the employees table. The second statement will delete Department 4 from the departments table. Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 12 / 17
Updates The UPDATE Statement UPDATE table_name SET list_of_assignments WHERE predicate; The UPDATE statement allows us to modify an existing tuple. Each assignment is of the form attribute = value If the WHERE clause is used, then the update applies only to those tuples that satisfy the predicate. Otherwise, the update applies to all tuples. Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 13 / 17
Updates Update UPDATE departments SET dept = 4 WHERE dept = 3; UPDATE employees SET dept = 4 WHERE dept = 3; UPDATE projects SET dept = 4 WHERE dept = 3; Suppose we want to change Department 3 to Department 4. We need to make the change in the departments table, the employees table, and the projects table. Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 14 / 17
Updates Increase All Department Numbers By 1 UPDATE departments SET dept = dept + 1; ERROR 1062 (23000): Duplicate entry 2 for key PRIMARY This statement is intended to increase all department numbers by 1. But it produces an error message. Why? Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 15 / 17
Updates The UPDATE Statement with ORDER BY UPDATE table_name SET list_of_assignments WHERE predicate ORDER BY attribute_list; The ORDER BY clause will order the records before the updating occurs. ORDER BY allows the ASC or DESC modifiers on each attribute to specify ascending or descending order. The default is ASC. Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 16 / 17
Updates The UPDATE Statement with ORDER BY UPDATE departments SET dept = dept + 1 ORDER BY dept DESC; Query OK, 3 rows affected (0.03 sec) If we order the department numbers in descending order, then the query works. How would we reduce each department number by 1? Robb T. Koether (Hampden-Sydney College) Insertions, Deletions, and Updates Wed, Jan 24, 2018 17 / 17