Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of dml statement in SQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the use of dml sentences in SQL, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Before describing how to add, delete, modify and query the data table, let's create a data table called students to facilitate all subsequent operations, as shown in the following figure:

Insert record

Insert into table name (field 1, field 2, field n) values (value 1, value 2, value n)

Where:

Table name: the data table we operate on, for example, table students

Fields: fields that need to be manipulated

Value: is the data to be added.

Through insert into, you can add data to our fields without specifying a field name, but the order followed by values should be the same as the sort of fields. You can insert data into the data table into both of the following. The sql statement and its display results are as follows:

Insert into can insert not only one piece of data at a time, but also multiple records at a time.

Insert into table name (field 1, field 2, field n) values (value 1, value 2, value n), (value 1, value 2, value 2, value n)

The sql statement and its output can be seen in the following figure:

Query data

In this article we will only talk about the simplest queries.

Select * from table name

Through this sentence, we can query the records that meet the conditions. * query all the fields in the table on your behalf. If we query a certain field, we only need to change * to that field.

We use the select * from table name when inserting the data, and we can see the result by looking at the code that inserts the data above.

If we want to query the data in the id field, we just need to change * to id.

So we show the data in the id field.

Update record

Update a table

Update table name set field 1 = value 1, field 2 = value 2, field n = value n [where condition]

Where:

Condition: it is the requirement that we need to update the table.

If we do not write the conditions required for the update, then all values will be updated. For example:

We can see that all six statements have changed. If we do not want all statements to be updated, we need to write conditions, such as:

Because we wrote the condition, only the third statement was updated.

Update data in multiple tables

Update Table 1, Table 2. Table n set Table 1. Field 1 = expression 1 no., table n. Field n = expression n [where condition]

Multi-table updates are more often used to dynamically update the fields of another table according to the target field. We can create a new data table called stu and put data in it.

With this data table, we can update multiple tables. The sql statement and its result are as follows:

We can see that the contents of both data tables have been updated.

Delete record

Delete from table name [where condition]

Deleting records has the same condition as updating records, and all records in the table will be deleted without where conditions, so be careful.

There is no way to understand it. Let's take a look at the example:

Since all data will be deleted without conditions, I will not demonstrate it here.

Thank you for reading this article carefully. I hope the article "what is the use of dml sentences in SQL" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report