tutorial on the use of UPDATE and DELETE statements in Mysql _mysql

Source: Internet
Author: User

Update updates
the update SET syntax is used to modify data in the Update data table.
Grammar:

UPDATE tb_name SET column1 = New_value1,column2 = New_value2,... WHERE definition

This syntax updates the value of Column1 in a record in the data table that matches the WHERE condition to the value of New_value1,column2 to New_value2, and so on. If you omit the WHERE condition, the column values for all records in the table are updated.
Example:

<?php
$conn = @mysql_connect ("localhost", "root", "root123");
if (! $conn) {
 die ("Connection Database failed:". Mysql_error ());
}

mysql_select_db ("Test", $conn);
mysql_query ("Set names ' GBK '"); 

$sql = "UPDATE user SET email = ' xiaoming@163.com ' WHERE username = ' Xiaoming '";
if (mysql_query ($sql, $conn)) {
 echo updates the data successfully! ";
} else {
 echo failed to update data:. Mysql_error ();
>

Pre-Update data:


The example modifies the email of username in the user table for Xiaoming to xiaoming@163.com.
Post-Update data:


An UPDATE expression
The UPDATE syntax allows the SET to be followed by an expression.
Example 1:

UPDATE article SET PV = pv+1 WHERE id = 123

This example lets an article with ID 123 click on the reading and add 1 clicks.
Example 2:

UPDATE persondata SET age = age*2, age = age+1

The example SET is followed by two expressions: Age = age*2 (ages doubled), aged = age+1 (plus 1). The case of this multiple expression is performed in Left-to-right order.

Delete from deletes data
Delete Deletion
The delete from syntax is used to delete data records from a datasheet.
Grammar:

DELETE from Tb_name WHERE definition

This syntax deletes data records that match the WHERE condition in the datasheet. If you omit the where condition, all the records in the table are deleted.
Example:

<?php
$conn = @mysql_connect ("localhost", "root", "root123");
if (! $conn) {
 die ("Connection Database failed:". Mysql_error ());
}

mysql_select_db ("Test", $conn);
mysql_query ("Set names ' GBK '"); 

$sql = "DELETE from user WHERE username = ' Xiao Wang '";
if (mysql_query ($sql, $conn)) {
 echo "delete". Mysql_affected_rows (). "Data record." ";
} else {
 exit ("Delete data failed:". Mysql_error ());
}
? >

Delete data successfully, browser output:
Deletes 1 data records.
Before deleting data:

Post-delete data:

If no qualifying records are deleted, mysql_query () still returns TRUE (unless SQL syntax errors). Therefore, to determine exactly whether a data record is deleted, you need to call the Mysql_affected_rows () function, which returns the number of rows that were affected by the most recent insert,update or DELETE query.
Tips
If you just want to delete a field data for a record, use the UPDATE SET syntax to empty it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.