Practical Guide to MySQL Database: Detailed Explanation and Examples of Basic Operations of Data Addition, Deletion, Modification and Query

foreword

This article mainly explains the related operations of adding, deleting, modifying and querying data in tables in the database.

You can follow my cloud native community : Cloud native community
can also follow my English community : learn English from scratch

1. Data insertion operation

语法:insert into 表名values (字段值1,字段值2, 字段值3);

1.1 Insert a piece of data

Create Zhang Sanfeng's table:

CREATE TABLE zhang_san_feng (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(50) NOT NULL,
  gender ENUM('男', '女') NOT NULL,
  birthdate DATE,
  style VARCHAR(50),
  description TEXT
);

Notes:

  • "style" field: This field is used to store Zhang Sanfeng's martial arts school or style, and adopts VARCHAR(50) data type.
  • "description" field: This field is used to store Zhang Sanfeng's description information, which adopts TEXT data type and can accommodate longer text.

Insert data test:

INSERT INTO zhang_san_feng (name, gender, birthdate, style, description)
VALUES ('张三丰', '男', '1217-04-09', '太极拳', '张三丰是明朝末年著名武术家,被誉为武林宗师。');
MySQL [school]> select *from zhang_san_feng;
+----+-----------+--------+------------+-----------+--------------------------------------------------------------------+
| id | name      | gender | birthdate  | style     | description                                                        |
+----+-----------+--------+------------+-----------+--------------------------------------------------------------------+
|  1 | 张三丰    || 1217-04-09 | 太极拳    | 张三丰是明朝末年著名武术家,被誉为武林宗师。                       |
+----+-----------+--------+------------+-----------+--------------------------------------------------------------------+
1 row in set (0.00 sec)

1.2 Insert multiple rows of data

Create a martial arts table and insert multiple rows of data:

CREATE TABLE martial_arts (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(50) NOT NULL,
  age INT,
  gender ENUM('男', '女'),
  skill VARCHAR(50),
  description TEXT
);

Insert data:

INSERT INTO martial_arts (name, age, gender, skill, description)
VALUES 
('小龙女', 18, '女', '玉女心经', '小龙女是郭靖妻子,身具绝世武功。'),
('杨过', 20, '男', '九阳真经', '杨过是小龙女的丈夫,精通九阳真经。'),
('金轮法王', 40, '男', '金轮寺武功', '金轮法王是佛教金轮寺的高僧,武功高强,擅使金轮。');
MySQL [school]> select * from martial_arts;
+----+--------------+------+--------+-----------------+--------------------------------------------------------------------------+
| id | name         | age  | gender | skill           | description                                                              |
+----+--------------+------+--------+-----------------+--------------------------------------------------------------------------+
|  1 | 小龙女       |   18 || 玉女心经        | 小龙女是郭靖妻子,身具绝世武功。                                         |
|  2 | 杨过         |   20 || 九阳真经        | 杨过是小龙女的丈夫,精通九阳真经。                                       |
|  3 | 金轮法王     |   40 || 金轮寺武功      | 金轮法王是佛教金轮寺的高僧,武功高强,擅使金轮。                         |
+----+--------------+------+--------+-----------------+--------------------------------------------------------------------------+
3 rows in set (0.01 sec)

2. Query the records in the table

2.1 Query all records in the table

语法:select * from 表名称;

MySQL [school]> select * from martial_arts;
+----+--------------+------+--------+-----------------+--------------------------------------------------------------------------+
| id | name         | age  | gender | skill           | description                                                              |
+----+--------------+------+--------+-----------------+--------------------------------------------------------------------------+
|  1 | 小龙女       |   18 || 玉女心经        | 小龙女是郭靖妻子,身具绝世武功。                                         |
|  2 | 杨过         |   20 || 九阳真经        | 杨过是小龙女的丈夫,精通九阳真经。                                       |
|  3 | 金轮法王     |   40 || 金轮寺武功      | 金轮法王是佛教金轮寺的高僧,武功高强,擅使金轮。                         |
+----+--------------+------+--------+-----------------+--------------------------------------------------------------------------+

2.2 Query the content of a field in the table

MySQL [school]> select name from martial_arts;
+--------------+
| name         |
+--------------+
| 小龙女       |
| 杨过         |
| 金轮法王     |
+--------------+
3 rows in set (0.00 sec)

MySQL [school]> select name,age from martial_arts;
+--------------+------+
| name         | age  |
+--------------+------+
| 小龙女       |   18 |
| 杨过         |   20 |
| 金轮法王     |   40 |
+--------------+------+
3 rows in set (0.00 sec)

2.3 View the tables of other databases or not view them on this database

语法:SELECT 字段 FROM 数据库名.表名;
View the table content specified under a database, database name. table name

MySQL [school]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [mysql]> select name,age from school.martial_arts;
+--------------+------+
| name         | age  |
+--------------+------+
| 小龙女       |   18 |
| 杨过         |   20 |
| 金轮法王     |   40 |
+--------------+------+
3 rows in set (0.00 sec)

MySQL [mysql]> 

3. Delete records in the table

For important data, the delete statement cannot be easily executed to delete. Once deleted, the data cannot be recovered, and logical deletion can be performed at this time.

MySQL's tombstone refers to marking data as deleted without actually deleting the data from the database. This deletion method usually adds a column to the data table, such as is_deleted or status, and sets its value to 1 to indicate that the data has been deleted. This way you can easily restore deleted data when needed.

3.1 Delete the row where di is 3

MySQL [school]> show tables;
+------------------+
| Tables_in_school |
+------------------+
| martial_arts     |
| student          |
| zhang_san_feng   |
+------------------+
3 rows in set (0.00 sec)

MySQL [school]> delete from martial_arts where id=3;
Query OK, 1 row affected (0.00 sec)

MySQL [school]> select id,name from martial_arts;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | 小龙女    |
|  2 | 杨过      |
+----+-----------+
2 rows in set (0.00 sec)

MySQL [school]> 

3.2 Delete rows with empty skills

mysql> delete from martial_arts where skill is null;

3.3 Update records

When no line is specified, each line will be modified

MySQL [school]> show tables;
+------------------+
| Tables_in_school |
+------------------+
| martial_arts     |
| student          |
| zhang_san_feng   |
+------------------+
3 rows in set (0.00 sec)

MySQL [school]> update martial_arts set description="小龙女是杨过的妻子,古墓派武功";
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

MySQL [school]> select * from martial_arts;
+----+-----------+------+--------+--------------+-----------------------------------------------+
| id | name      | age  | gender | skill        | description                                   |
+----+-----------+------+--------+--------------+-----------------------------------------------+
|  1 | 小龙女    |   18 || 玉女心经     | 小龙女是杨过的妻子,古墓派武功                |
|  2 | 杨过      |   20 || 九阳真经     | 小龙女是杨过的妻子,古墓派武功                |
+----+-----------+------+--------+--------------+-----------------------------------------------+
2 rows in set (0.00 sec)


When specifying which line, only modify the specified line

MySQL [school]> update martial_arts set description="杨过是小龙女的丈夫,自创武功:黯然销魂掌" where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MySQL [school]> select * from martial_arts;
+----+-----------+------+--------+--------------+--------------------------------------------------------------+
| id | name      | age  | gender | skill        | description                                                  |
+----+-----------+------+--------+--------------+--------------------------------------------------------------+
|  1 | 小龙女    |   18 || 玉女心经     | 小龙女是杨过的妻子,古墓派武功                               |
|  2 | 杨过      |   20 || 九阳真经     | 杨过是小龙女的丈夫,自创武功:黯然销魂掌                     |
+----+-----------+------+--------+--------------+--------------------------------------------------------------+
2 rows in set (0.00 sec)

MySQL [school]> 

  • If you want to update multiple fields at the same time, you need to separate them:
update students set stname='zhangsan',age=21 where uid=1;

3.4 Tombstone example

MySQL does not have a built-in logical deletion function, but logical deletion can be implemented by adding a field indicating the deletion status. In general, you can use a Boolean field named "deleted", whose default value is 0 for not deleted and 1 for deleted.

Let's demonstrate the process of logical deletion:

  1. Add a boolean field called "deleted" to the table:
MySQL [school]> alter table martial_arts add   deleted boolean not null default 0;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

MySQL [school]> select * from martial_arts;
+----+-----------+------+--------+--------------+--------------------------------------------------------------+---------+
| id | name      | age  | gender | skill        | description                                                  | deleted |
+----+-----------+------+--------+--------------+--------------------------------------------------------------+---------+
|  1 | 小龙女    |   18 || 玉女心经     | 小龙女是杨过的妻子,古墓派武功                               |       0 |
|  2 | 杨过      |   20 || 九阳真经     | 杨过是小龙女的丈夫,自创武功:黯然销魂掌                     |       0 |
+----+-----------+------+--------+--------------+--------------------------------------------------------------+---------+
2 rows in set (0.00 sec)


Note: Added a boolean field called "deleted" to the "martial_arts" table. This field indicates the deletion status of the record, and the default value is set to 0, that is, not deleted.

Then, mark the tombstone by updating the field. For example, to logically delete the record "Little Dragon Girl":

MySQL [school]> update martial_arts set deleted=1 where name="小龙女";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MySQL [school]> select * from martial_arts;
+----+-----------+------+--------+--------------+--------------------------------------------------------------+---------+
| id | name      | age  | gender | skill        | description                                                  | deleted |
+----+-----------+------+--------+--------------+--------------------------------------------------------------+---------+
|  1 | 小龙女    |   18 || 玉女心经     | 小龙女是杨过的妻子,古墓派武功                               |       1 |
|  2 | 杨过      |   20 || 九阳真经     | 杨过是小龙女的丈夫,自创武功:黯然销魂掌                     |       0 |
+----+-----------+------+--------+--------------+--------------------------------------------------------------+---------+
2 rows in set (0.00 sec)


The above example uses the UPDATE statement to set the "deleted" field to 1 to mark the record "Little Dragon Girl" as logically deleted.

When querying data, it is necessary to exclude records that have been logically deleted in the WHERE clause. For example, to query for records that have not been tombstoned:

MySQL [school]> select * from martial_arts where deleted = 0;
+----+--------+------+--------+--------------+--------------------------------------------------------------+---------+
| id | name   | age  | gender | skill        | description                                                  | deleted |
+----+--------+------+--------+--------------+--------------------------------------------------------------+---------+
|  2 | 杨过   |   20 || 九阳真经     | 杨过是小龙女的丈夫,自创武功:黯然销魂掌                     |       0 |
+----+--------+------+--------+--------------+--------------------------------------------------------------+---------+
1 row in set (0.00 sec)

The above example uses the SELECT statement to query the records in the "martial_arts" table that have not been logically deleted, that is, the records with the value of the "deleted" field being 0.
Through the above steps, you can implement logical deletion in the MySQL database. When you need to delete a record, you only need to set the "deleted" field to 1, without physically deleting the data. This allows data integrity to be preserved and restored if required.

Summarize

This article mainly explains the related operations of adding, deleting, modifying and checking the data in the table in the database, have you learned it?

おすすめ

転載: blog.csdn.net/wisdom_futrue/article/details/131263237