SQL zero-based introductory learning (4)

SQL zero-based introductory learning (3)

SQL INSERT INTO statement

The INSERT INTO statement is used to insert new records into a table.

SQL INSERT INTO syntax

The INSERT INTO statement can be written in two forms.

The first form does not need to specify the column name to insert data, just provide the value to be inserted:

INSERT INTO table_name
VALUES (value1,value2,value3,...);

The second form requires specifying the column names and the values ​​to be inserted:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

Parameter description:
table_name: The name of the table where new records need to be inserted.
column1, column2, ...: the names of the fields to be inserted.
value1, value2, …: Field values ​​to be inserted.

demo database

In this tutorial, we will use the RUNOOB sample database.

Here is the data from the "Websites" table:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 菜鸟教程      | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
+----+--------------+---------------------------+-------+---------+

INSERT INTO instance

Suppose we want to insert a new row into the "Websites" table.

We can use the following SQL statement:

INSERT INTO Websites (name, url, alexa, country)
VALUES ('百度','https://www.baidu.com/','4','CN');

Execute the above SQL, and then read the "Websites" table, the data is as follows:
insert image description here
Did you notice that we didn't insert any numbers into the id field?
The id column is automatically updated and each record in the table has a unique number.

Insert data in the specified column

We can also insert data in the specified column.

The following SQL statement will insert a new row, but only insert data in the "name", "url" and "country" columns (the id field will be updated automatically):

INSERT INTO Websites (name, url, country)
VALUES ('stackoverflow', 'http://stackoverflow.com/', 'IND');

Execute the above SQL, and then read the "Websites" table, the data is as follows:
insert image description here

SQL UPDATE statement

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

SQL UPDATE syntax

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

Parameter description:
table_name: the name of the table to be modified.
column1, column2, …: The name of the field to be modified, which can be multiple fields.
value1, value2, …: The value to be modified, which can be multiple values.
condition: Modification condition, which is used to specify which data should be modified.

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

demo database

In this tutorial, we will use the RUNOOB sample database.

Here is the data from the "Websites" table:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 菜鸟教程      | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
+----+--------------+---------------------------+-------+---------+

SQL UPDATE instance

Suppose we want to update the alexa rank of "Rookie Tutorial" to 5000, and change the country to USA.

We use the following SQL statement:

UPDATE Websites 
SET alexa='5000', country='USA' 
WHERE name='菜鸟教程';

Execute the above SQL, and then read the "Websites" table, the data is as follows:
insert image description here

Update Warning!

Be very careful when updating records! In the above example, if we omit the WHERE clause, as follows:

UPDATE Websites
SET alexa='5000', country='USA'

Executing the above code will change the alexa of all the data in the Websites table to 5000, and the country to USA.

Be cautious when performing UPDATEs without a WHERE clause.

SQL DELETE statement

DELETE statement is used to delete records in a table.

SQL DELETE syntax

DELETE FROM table_name
WHERE condition;

Parameter description:
table_name: the name of the table to be deleted.
condition: Deletion condition, used to specify which data to delete.

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

demo database

In this tutorial, we will use the RUNOOB sample database.

Here is the data from the "Websites" table:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝       | https://www.taobao.com/   | 13    | CN      |
| 3  | 菜鸟教程 | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博       | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
+----+--------------+---------------------------+-------+---------+

SQL DELETE instance

Suppose we want to delete from the "Websites" table the website with the website name "Facebook" and the country USA.

We use the following SQL statement:

DELETE FROM Websites
WHERE name='Facebook' AND country='USA';

Execute the above SQL, and then read the "Websites" table, the data is as follows:
insert image description here

delete all data

You can delete all rows in a table without deleting the table. This means the table structure, attributes, indexes will remain unchanged:

DELETE FROM table_name;

NOTE: Be extremely careful when deleting records! Because you can't do it all over again!

Guess you like

Origin blog.csdn.net/weixin_44006731/article/details/129138892