[SQL must know and know] - Lesson 16 Update and delete data

Table of contents

update data

        Do not omit the WHERE clause

        Using subqueries in UPDATE statements

delete data

        Do not omit the WHERE clause

        friendly foreign key

        delete the contents of the table but not the table

        faster deletion

Guidelines for Updates and Deletes


        This lesson describes how to further manipulate table data using UPDATE and DELETE statements.

update data

        To update (modify) the data in the table, you can use the UPDATE statement. There are two ways of using UPDATE:

  • update a specific row in the table;
  • Update all rows in the table.

        Do not omit the WHERE clause

        Be careful when using UPDATE. Because if you don't pay attention, all the rows in the table will be updated.

        Using the UPDATE statement is very easy, maybe even too easy. The basic UPDATE statement consists of three parts, namely:

  • the table to update;
  • column names and their new values;
  • Filter criteria that determine which rows to update.
UPDATE Customers
SET cust_email = '[email protected]'
WHERE cust_id = '1000000005';

        The UPDATE statement ends with a WHERE clause, which tells the DBMS which row to update. Without the WHERE clause, the DBMS will update all rows in the table, which is not what we want.

        The syntax for updating multiple columns is slightly different:

UPDATE Customers
SET cust_contact = 'Sam Roberts',
cust_email = '[email protected]'
WHERE cust_id = '1000000006';

        When updating multiple columns, you only need to use a SET command, separating each "column=value" pair with a comma (no comma after the last column).


        Using subqueries in UPDATE statements

        A subquery can be used in an UPDATE statement to update column data with data retrieved by a SELECT statement. See Lesson 11 for more on subqueries and their use.

        To delete a column value, set it to NULL (if the table definition allows NULL values) . Proceed as follows:

UPDATE Customers
SET cust_email = NULL
WHERE cust_id = '1000000005';

        Among them, NULL is used to remove the value in the cust_email column. This is very different from saving an empty string (an empty string is represented by '', which is a value), whereas NULL means no value .


delete data

        To delete (remove) data from a table, use the DELETE statement. There are two ways of using DELETE:

  • Delete specific rows from a table;
  • Delete all rows from the table.

        Do not omit the WHERE clause

        Be careful when using DELETE. Because if you don't pay attention, you will delete all the rows in the table by mistake. Please read this section in its entirety before using this statement.

        As mentioned earlier, UPDATE is very easy to use, and DELETE is even easier to use.
        The following statement deletes a row from the Customers table:

DELETE FROM Customers
WHERE cust_id = '1000000006';

        This statement is easy to understand. DELETE FROM requires specifying the name of the table from which to delete data, and the WHERE clause filters the rows to be deleted. In this example, only customer 1000000006 is deleted. If the WHERE clause is omitted, it will delete every customer in the table.


        friendly foreign key

        Lesson 12 introduced joins, a simple join of two tables that requires only common fields from both tables. It is also possible to have the DBMS strictly enforce relationships through the use of foreign keys. When foreign keys exist, they are used by the DBMS to enforce referential integrity . For example, to delete a product from the Products table that is used in an existing order in OrderItems, the DELETE statement will throw an error and abort. This is another reason to always define foreign keys.

        DELETE does not require column names or wildcards. DELETE deletes entire rows rather than columns. To drop specified columns, use the UPDATE statement.


        delete the contents of the table but not the table

        The DELETE statement deletes rows from a table, or even deletes all rows in the table. However, DELETE does not delete the table itself .

        faster deletion

        Do not use DELETE if you want to delete all rows from the table. You can use the TRUNCATE TABLE statement, which does the same job, but faster (because no data changes are recorded).


Guidelines for Updates and Deletes

        The following are important principles that many SQL programmers follow when using UPDATE or DELETE.

  • Never use an UPDATE or DELETE statement without a WHERE clause unless you really intend to update and delete every row.
  • Make sure each table has a primary key (see Lesson 12 if you forget this), and use it like a WHERE clause whenever possible (you can specify individual primary keys, multiple values, or ranges of values).
  • Before using the WHERE clause in the UPDATE or DELETE statement, you should use SELECT to test to ensure that it filters the correct records, in case the written WHERE clause is incorrect.
  • Use a database that enforces referential integrity (see Lesson 12 on this), so that the DBMS will not allow deletion of rows whose data is associated with other tables.
  • Some DBMSs allow the database administrator to impose constraints that prevent the execution of UPDATE or DELETE statements without a WHERE clause. If the DBMS used supports this feature, it should be used.

        If SQL doesn't have an undo button, you should use UPDATE and DELETE very carefully, or you may find yourself updating or deleting the wrong data.

Guess you like

Origin blog.csdn.net/qq_57163366/article/details/130151251