SQL must know - update and delete data

"SQL must know must know" reading notes

1. Update data

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

Note: Do not omit the WHERE clause
Be careful when using UPDATE. Because if you are not careful, all the rows in the table will be updated.

Tip: UPDATE and Security
In a client/server DBMS, using the UPDATE statement may require special security privileges. Before you use UPDATE, you should ensure that you have sufficient security permissions.

The basic UPDATE statement consists of three parts:

  • the table to update;
  • column names and their new values;
  • A filter that determines which rows to update.
UPDATE Customers
SET cust_email = '[email protected]'
WHERE cust_id = '1000005';

Update multiple columns:

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

Tip: Using Subqueries in
UPDATE Statements Subqueries can be used in UPDATE statements to update column data with the data retrieved by the SELECT statement.

Hint: FROM keyword
Some SQL implementations support the use of the FROM clause in an UPDATE statement to update rows from one table with data from another table.

To delete the value of a column, you can set it to NULL (if the table definition allows NULL values) [is to use update to delete data].

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

This is very different from saving an empty string (an empty string is represented by ", which is a value), where NULL means no value.

2. Delete data

There are two ways to use DELETE:

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

Note: Do not omit the WHERE clause
Be careful when using UPDATE. Because if you are not careful, all the rows in the table will be updated.

Tip: DELETE and Security
In a client/server DBMS, special security privileges may be required to use the DELETE statement. Before you use DELETE, you should ensure that you have sufficient security permissions.

Delete specific lines:

DELETE FROM Customers
WHERE cust_id = '10000006';

Tip: Friendly
Foreign Keys When foreign keys exist, the DBMS uses them to enforce referential integrity. For example, to insert a new product into the Products table, the DBMS does not allow inserting it with an unknown vendor id because the vendor_id column is connected to the Vendors table as a foreign key.

One of the benefits of using foreign keys to ensure referential integrity is that the DBMS often prevents deletion of rows that are needed for a relationship. 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.

Hint: FROM Keyword
In some SQL implementations, the FROM keyword following DELETE is optional. But even if you don't need it, it's a good idea to provide this keyword. Doing so will ensure that the SQL code is portable between DBMSs.

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

UPDATE Customers
SET cust_email = NUll;

Description: Delete the contents of the table instead of the table
DELETE statement to delete rows from the table, or even delete all rows in the table. However, DELETE does not delete the table itself.

Tip: Faster deletes
If you want to delete all rows from a table, don't use DELETE. You can use the TRUNCATE TABLE statement, which does the same job, but faster. (Because data changes are not recorded, that is, all rows in the table are deleted, and the deletion of a single row is not recorded).

Notes:

Compare truncate and delete:

truncate table is functionally identical to the delete statement without the WHERE clause: both delete all rows in the table.

truncate is faster than delete and uses less system and transaction log resources.

The table after truncate operation is much faster than the table after delete operation.

When the table is emptied, the table and the index of the table are reset to the initial size, but delete cannot. (That is, DELETE does not delete the definition and does not release the space. For example, the id was 1 2 3 before. After using DELETE, the id is 4 to add data, and the truncate is 1. )

For more information, please refer to: https://jingyan.baidu.com/article/8275fc8693e11846a03cf696.html

3. Guidelines for Updates and Deletions

The following are important principles that SQL follows 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, and use it like a WHERE clause as much as possible (you can specify each primary key, multiple values, or a range of values).
  • Before using a WHERE clause in an UPDATE or DELETE statement, you should use SELECT to test to ensure that it filters the correct records, in case the WHERE clause is written incorrectly.
  • Use a database that enforces referential integrity so that the DBMS will not allow deletion of rows whose data is associated with other tables.
  • Some DBMSs allow database administrators to impose constraints that prevent the execution of UPDATE or DELETE statements without a WHERE clause. If the DBMS in use supports this feature, it should be used.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324688206&siteId=291194637