MySQL DML language operation example

MySQL DML language operation example

Additional explanation, foreign keys: Do not use foreign keys, all foreign key concepts are solved at the application layer.

Additional note: Database columns, that is, field names, should be prefixed with floating symbols as much as possible.

The purpose of database existence: data storage and data management.

Database: rows (data), columns (fields)

Note: This page solves the data problem of the row. The previous page is about solving the column field problem.

DML language: Data Manipulation Language

1. Add (insert)

2. Update

3. Delete

1. Add insert (add data to the row)

-- Insert statement (add)
-- Format:

INSERT INTO `table name`(`field name 1`,`field name 2`,`field name 3`) VALUES ('value 1','value 2','value 3'),('value 1','value 2','value 3'),('value 1','value 2','value 3')
INSERT INTO `student`(`name`,`age`,`sex`) VALUES ('xiaoming','22','man'),('xiaohong','20','woman'),('xiaoli','25','man')

Notice:

1. The symbol above the field name is: float`

2. The symbol above the value is: single quote '

2. Modify update

2.1 Symbols of judgment statements

Operators meaning scope result
= equal 5=6 false
<> or != Not equal to 5<>6 true
> Greater than
< Less than
<= Less than or equal to
>= Greater than or equal to
BETWEEN ... ADN ... Within a certain range (closed loop) [2,5]
AND and 5<1 ADN 1>2 false
OR or 5>1 OR 1>2 true

2.2 Modify the value of a field, unconditionally

-- Change, unconditionally. Format: UPDATE `table name` SET `field name` = value

UPDATE `student` SET `name` = 'BeiHang' -- The effect is that the value of all name fields is BeiHang

2.3 Modify the value of a field and specify conditions

-- Change, conditional. Format: UPDATE `table name` SET `field name` = value WHERE id=value1 OR id=value2

UPDATE `student` SET `name` = 'DongDa' WHERE id=1 OR id=3 -- The effect is that the value of the name field of all students with id 1 and 3 is DongDa

2.4 Modify the values ​​of multiple fields and specify conditions

-- Change, conditional. Format: UPDATE `table name` SET `field name 1` = 'value 1', `field name 2` = 'value 2' WHERE field name BETWEEN value 1 AND value 2

UPDATE `student` SET `name` = 'BeiHang',`sex` = 'man' WHERE age BETWEEN 20 AND 25 -- The effect is all students whose age is between 20 and 25

The name in the text box changes to BeiHang, and sex changes to man.

3. Delete (delete command or truncate command)

3.1 Method 1: delete command deletes all data and specified data in the table

DELETE FROM `student`; -- The effect is to completely delete the student table


DELETE FROM `student` WHERE id=1; -- The effect is to delete the specified data in the student table

3.2 Method 2: truncate command deletes all data in the table

TRUNCATE `student`; -- The effect is to completely delete the student table

3.3 The difference between the two commands to delete all data in the table

After deleting, if you add data to the table again, the auto-incremented id will continue to be sorted downwards as before;

After truncate is deleted, the auto-incremented id will not be sorted downwards.

Therefore, if you want to delete all data in a table, it is recommended to use truncate.

The above is the details of the MySQL DML language operation example. For more information about MySQL DML language (addition, deletion and modification), please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of database language classification DDL, DCL, DML
  • Detailed explanation of the use of MySQL DML statements
  • MySQL learning database operation DML detailed explanation for beginners
  • MySQL data operation-use of DML statements
  • MySQL tutorial DML data manipulation language example detailed explanation

<<:  Ubuntu 20.04 sets a static IP address (including different versions)

>>:  VMware installation of Ubuntu 20.04 operating system tutorial diagram

Recommend

64-bit CentOs7 source code installation mysql-5.6.35 process sharing

First install the dependent packages to avoid pro...

JavaScript Html to implement the mobile red envelope rain function page

This article example shares the specific code of ...

MySQL scheduled backup solution (using Linux crontab)

Preface Although some love in this world has a pr...

How to quickly use mysqlreplicate to build MySQL master-slave

Introduction The mysql-utilities toolset is a col...

Search engine free collection of website entrances

1: Baidu website login entrance Website: http://ww...

Detailed graphic tutorial on installing centos7 virtual machine in Virtualbox

1. Download centos7 Download address: https://mir...

Tutorial on installing Ubuntu 20.04 and NVIDIA drivers

Install Ubuntu 20.04 Install NVIDIA drivers Confi...

Who is a User Experience Designer?

Scary, isn't it! Translation in the picture: ...

CSS Reset style reset implementation example

Introduction: All browsers come with default styl...

Implementation of built-in modules and custom modules in Node.js

1. Commonjs Commonjs is a custom module in nodejs...

MySQL 8.0.21 installation and configuration method graphic tutorial

Record the installation and configuration method ...

Causes and solutions to the garbled character set problem in MySQL database

Preface Sometimes when we view database data, we ...

Graphic tutorial on installing tomcat8 on centos7.X Linux system

1. Create the tomcat installation path mkdir /usr...

How to Develop a Progressive Web App (PWA)

Table of contents Overview Require URL of the app...