Showing posts with label SQL UPDATE statement. Show all posts
Showing posts with label SQL UPDATE statement. Show all posts
SQL DELETE statement

SQL DELETE statement

The DELETE statement is used to delete records in a table.

SQL DELETE statement

The DELETE statement is used to delete rows in a table.

SQL DELETE syntax

DELETE FROM table_name
WHERE some_column=some_value;

LampNote the WHERE clause in the SQL DELETE statement! 
The WHERE clause specifies which records or which records need to be deleted. If you omit the WHERE clause, all the records will be deleted!


Demo database

In this tutorial, we will use the RUNOOB sample database.
The following is the data selected from the "Websites" table:
+ ---- + -------------- + --------------------------- + - ------ + --------- + | id | name          | url                        | alexa | country | + ---- + -------------- + - -------------------------- + ------- + --------- + | 1 | Google | Https : //www.google.cm/ | 1 | USA | | 2 | Taobao | https : //www.taobao.com/ | 13 | CN | | 3 | Rookie Tutorial | http : //www.runoob.COM / | 4689 | CN | | 4 | Twitter | HTTP : //weibo.com/ | 20 | CN | | 5 | Facebook | HTTPS : //www.facebook.com/ | 3 | USA | | 6 | Baidu | Https : //www.baidu.com/ | 4 | CN | | 7 | stackoverflow | http : //stackoverflow.com/ | 0 | IND | + ---- + ---------- ----- + --------------------------- + ------- + -------- - +COM / | 20 is | the CN | | . 5 | Facebook | HTTPS : //www.facebook.com/ |. 3 | USA | | . 6 | Baidu | HTTPS : //www.baidu.com/ |. 4 | the CN | | . 7 | Stackoverflow | http : //stackoverflow.com/ | 0 | IND | + ---- + --------------- + ------------- -------------- + ------- + --------- +COM / | 20 is | the CN | | . 5 | Facebook | HTTPS : //www.facebook.com/ |. 3 | USA | | . 6 | Baidu | HTTPS : //www.baidu.com/ |. 4 | the CN | | . 7 | Stackoverflow | http : //stackoverflow.com/ | 0 | IND | + ---- + --------------- + ------------- -------------- + ------- + --------- +Com | | 4 | CN | | 7 | stackoverflow | http : //stackoverflow.com/ | 0 | IND | + ---- + --------------- + --- ------------------------ + ------- + --------- +Com | | 4 | CN | | 7 | stackoverflow | http : //stackoverflow.com/ | 0 | IND | + ---- + --------------- + --- ------------------------ + ------- + --------- +


           
              
          
              
         
             
   


SQL DELETE instance

Suppose we want to remove the site from the "Websites" table named "Baidu" and the country for the CN site.
We use the following SQL statement:

Examples

DELETE FROM Websites WHERE name = ' Baidu ' AND country = ' CN ' ;
Execute the above SQL, and then read the "Websites" table, the data is as follows:

Delete all data

You can delete all the rows in the table without deleting the table. This means that the table structure, attributes, and indexes will remain the same:
DELETE FROM table_name ;

or

DELETE * FROM table_name ;
Note: Be careful when deleting records! Because you can not come back!
SQL UPDATE statement

SQL UPDATE statement

The UPDATE statement is used to update the records in the table.

SQL UPDATE statement

The UPDATE statement is used to update an existing record in the table.

SQL UPDATE syntax

UPDATE table_name
SET column1 = value1 , column2 = value2 ,...
WHERE some_column = some_value ;

LampNote the WHERE clause in the SQL UPDATE statement!
The WHERE clause specifies which records or which records need to be updated. If you omit the WHERE clause, all the records will be updated!


Demo database

In this tutorial, we will use the RUNOOB sample database.
The following is the data selected from the "Websites" table:
 + ---- + -------------- + --------------------------- + - ------ + --------- +
 | Id | name | url | alexa | country |
 + ---- + -------------- + --------------------------- + - ------ + --------- +
 | 1 | Google | https://www.google.cm/ | 1 | USA |
 | 2 | Taobao | https://www.taobao.com/ | 13 | CN |
 | 3 | rookie tutorial | http://www.runoob.com/ | 4689 | CN |
 | 4 | microblogging | http://weibo.com/ | 20 | CN |
 5 | Facebook | https://www.facebook.com/ | 3 | USA |
 + ---- + -------------- + --------------------------- + - ------ + --------- +


SQL UPDATE instance

Suppose we want to update the "rookie tutorial" alexa ranking to 5000, country changed to USA.
We use the following SQL statement:

Examples

UPDATE Websites SET Alexa = ' 5000 ' , country = ' USA ' WHERE Name = ' rookie tutorial ' ;
Execute the above SQL, and then read the "Websites" table, the data is as follows:


Update Warning!

Be extra careful when updating your records! In the above example, if we omit the WHERE clause, as follows:
UPDATE Websites
SET alexa = '5000', country = 'USA' 
The above code will change all the data in the Websites table alexa to 5000, country to USA.
Execute UPDATE without WHERE clause to be careful and cautious.

Popular Posts