More Database Practice ====================== In the previous pages, we created a database, added a table to it, and saved several entries in that table. Now we will learn how to modify the stored data. Update Table Data ----------------- By using an :ref:`UPDATE ` query, we can change one or more entries in a table. Code along with the following video to practice this technique. .. admonition:: Note The video shows a new column in the ``students`` table. Here's how to add that column and fill it with ``'Yes'`` strings: .. sourcecode:: Python :lineno-start: 6 # Add the recent_grad column to the students table. sql_query = "ALTER TABLE students ADD COLUMN recent_grad TEXT" cursor.execute(sql_query) # Fill each cell in the column with a 'Yes' string. sql_query = "UPDATE students SET recent_grad = 'Yes'" cursor.execute(sql_query) database.commit() After running this code, your ``students`` table should match the one shown at the beginning of the video. .. raw:: html
**Key points**: #. The ``UPDATE`` query uses the ``SET`` keyword to identify one or more columns to change. #. To identify which rows to change, the ``UPDATE`` query should also include a ``WHERE`` condition. If we leave out the condition, then *every* row in the table will be modified. #. Combining ``SET`` and ``WHERE`` identifies which cell(s) in the table to change. .. figure:: figures/table-update.png :alt: Showing how SET and WHERE cross in a table to identify the cell(s) to change. :width: 80% Each cell in the table has a (column, row) position. #. After running the query, use the ``.commit()`` method to confirm the changes. #. After we commit an ``UPDATE`` query, there is no quick *Undo* option! **Sample code**: .. sourcecode:: Python :linenos: import sqlite3 database = sqlite3.connect('practice.db') cursor = database.cursor() # Update selected cells from the 'students' table: sql_query = "UPDATE students SET recent_grad = 'No' WHERE grad_year < 2017" cursor.execute(sql_query) database.commit() Delete Data ----------- Code along with the following video to practice removing rows from a table. Also learn how to delete an entire table from the database. .. raw:: html
**Key points**: #. The :ref:`DELETE FROM ` query removes selected rows from a table. #. Add a ``WHERE`` clause to identify which rows should be deleted. #. The ``DROP TABLE`` query removes an entire table from the database. ALL the row and column information will be deleted. #. *There is NO undo option* for a ``DELETE`` or ``DROP`` query. **Sample code**: .. sourcecode:: Python :linenos: import sqlite3 database = sqlite3.connect('practice.db') cursor = database.cursor() # Delete selected rows from the 'students' table: sql_query = "DELETE FROM students WHERE recent_grad == 'No'" cursor.execute(sql_query) database.commit() # Drop the 'remove_me' table from the database: sql_query = "DROP TABLE remove_me" cursor.execute(sql_query) Check Your Understanding ------------------------ .. admonition:: Question Assume we have a table called ``books`` stored in our database. The table includes columns for ``title``, ``publish_date``, and ``category``. Which SQL query returns the titles for all non-fiction books published after 2019? .. raw:: html
  1. SELECT title, category FROM books WHERE publish_date > 2019
  2. SELECT publish_date > 2019 FROM books WHERE category = 'non-fiction'
  3. SELECT books, title WHERE publish_date > 2019 AND category = 'non-fiction'
  4. SELECT title FROM books WHERE publish_date > 2019 AND category = 'non-fiction'

.. Answer = d .. admonition:: Question What should you do if you accidentally ``DROP`` a table from your database or ``DELETE`` the wrong row from a table? .. raw:: html
  1. Accept that the data is gone and learn from your mistake.
  2. Cry.
  3. Blame someone else.
  4. Use Control-z to undo the mistake.

.. Answer = a .. admonition:: Question To ``UPDATE`` an entry in a table, the SQL query should specify a row and a column. Which keyword identifies the rows to change, and which keyword identifies the columns? .. raw:: html
  1. SET identifies the rows. WHERE identifies the columns.
  2. SET identifies the columns. WHERE identifies the rows.
  3. FROM identifies the rows. SET identifies the columns.
  4. FROM identifies the columns. WHERE identifies the rows.

.. Answer = b