Sometimes, you want to drop some records or change their contents. The DELETE and UPDATE statements enable us to do this.
Modify records with update
UPDATE Tbl_name Set the column to change
The record WHERE you want to update
The WHERE clause here is optional, so if you do not specify it, each record in the table is updated.
For example, in the pet table, we found that the sex of the pet Whistler was not specified, so we can modify this record as follows:
Mysql> Update pet Set sex= ' F ' where name= ' Whistler ';
Delete Records with delete
The DELETE statement has the following format:
Delete from Tbl_name WHERE to delete records
The WHERE clause specifies which records should be deleted. It is optional, but if not selected, all records will be deleted. This means that the simplest DELETE statement is also the most dangerous.
This query clears all the contents of the table. You must be careful!
To delete a specific record, you can select the record you want to delete by using the WHERE clause. This is similar to the WHERE clause in the SELECT statement.
Mysql> Delete from pet where name= "Whistler";
You can empty the entire table with the following statement:
Mysql>delete from Pet;
Summarize
This section describes the use of two SQL statements. Use the update and DELETE statements with great care because it can be dangerous for your data. In particular, DELETE statements make it easy to delete large amounts of data. Be careful when you use it.
Study Questions
1, please personally follow the steps described in this chapter, so that the MySQL server in the Linux system startup, automatically start. And try other ways to start, restart, and shut down the server.
2, now has a host database.domain.net MySQL server, with the root user's identity, the password is newpass, connect to the database test. How do I give you the right command line? If you use an option file, how do I add an option?
3. In the test database, establish a table pet described in the example in this chapter, and its structure is described as follows:
name:30 a fixed-length string of width
owner:30 a fixed-length string of width
Species:10 a fixed-length string of width
Sex: Non-null enumeration type composed of M and F
Birth:date type
Death:date type