Update and delete-UPDATE, DELETE

Update data-UPDATE

In order to update the data in the database, you can use the UPDATE statement, using two methods to use UPDATE

  1.  Update specific rows in the table
  2. Update all rows in the table

Note: When using UPDATE, if there is no update condition WHERE, it will update all the rows in the table

Here is a simple example: customer 1005 now has an email address, and his record needs to be updated. WHERE will tell MySQL to update the condition

UPDATE customers
set cust_email = '[email protected]'
WHERE cust_id = 10005

 

If you want to delete the value of a column, you can set it to NULL (assuming that the table definition allows NULL values)

UPDATE customers
set cust_email = NULL
WHERE cust_id = 10005

 

 

Delete data-DELETE

   There are two ways to use DELETE:

  1. Delete a specific row from the table
  2. Delete all rows from the table

As with updates, if there is no WHERE filter, all rows in the table will be deleted.

DELETE can only delete all rows of the table, can not delete the table

Here is a simple example:

Delete a row from the customers table

DELETE FROM customers
WHERE cust_id = 10015;

 

 

Update and delete principles:

  • If you plan to delete or update a row, you should never use the clause without WHERE
  • Ensure that each table has a primary key
  • When a primary key or foreign key is referenced, MySQL is not allowed to delete rows with data associated with other tables 
Published 156 original articles · Liked 34 · Visits 150,000+

Guess you like

Origin blog.csdn.net/bbj12345678/article/details/105574592