Insertions, Deletions, and Updates

Similar documents
Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Relational Databases

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

PHP Queries and HTML Forms Lecture 23

Functional Dependencies and Normal Forms

Functional Dependencies and Normal Forms

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

XQuery FLOWR Expressions Lecture 35

Minimal Spanning Trees

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

The Critical-Path Algorithm

MySQL Creating a Database Lecture 3

Introduction to Databases

Relational Algebra Part I. CS 377: Database Systems

Relational Databases Lecture 2

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Density Curves Sections

Introduction to Databases

Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

The Decreasing-Time Algorithm

Recursive Descent Parsers

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

2.2.2.Relational Database concept

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Database design process

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Sampling Distribution Examples Sections 15.4, 15.5

Rotations and Translations

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Scheduling and Digraphs

The Pairwise-Comparison Method

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Slides by: Ms. Shree Jaswal

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

CS 377 Database Systems

Part 1 on Table Function

SQL Structured Query Language Introduction

Solving Recursive Sequences by Iteration

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

Database Management Systems,

CMPT 354: Database System I. Lecture 2. Relational Model

The Plurality-with-Elimination Method

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Binary Tree Applications

Implementing Linked Lists

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Basic SQL II. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

The Traveling Salesman Problem Brute Force Method

Function Definition Syntax Tree

Street-Routing Problems

Building the Abstract Syntax Trees

Database Technology. Topic 3: SQL. Olaf Hartig.

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

Relational Calculus: 1

CIS611 Lab Assignment 1 SS Chung

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

Chapter 6 - Part II The Relational Algebra and Calculus

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

LR Parsing - Conflicts

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

KINGDOM OF SAUDI ARABIA-JAZAN UNIVERSITY COLLEGE OF COMPUTER SCIENCE & INFORMATION SYSTEMS 221 INFS 3 DATABASE SYSTEMS-1 REVIEW QUESTIONS

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

DTDs and XML Attributes

Announcements. Using Electronics in Class. Review. Staff Instructor: Alvin Cheung Office hour on Wednesdays, 1-2pm. Class Overview

SQL. Char (30) can store ram, ramji007 or 80- b

Scope and Parameter Passing

The Graphics Pipeline

Data Manipulation Language (DML)

XML and AJAX Lecture 28

Recognition of Tokens

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

The Projection Matrix

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

The Class Construct Part 1

The Traveling Salesman Problem Nearest-Neighbor Algorithm

SQL STRUCTURED QUERY LANGUAGE

Introduction to Databases CSE 414. Lecture 2: Data Models

CSC Web Programming. Introduction to SQL

List Iterator Implementation

Transcription:

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