MySQL DML操作步驟流程

1. 背景

   * CURD 操作通常是使用關係型數據庫系統中的結構化查詢語言(Structured Query Language,SQL)完成的

   * CURD 定義了用於處理數據的基本原子操作

   * CURD 代表創建(Create)、更新(Update)、讀取(Retrieve)和刪除(Delete)操作。


2. 創建表操作

   * 創建數據庫(DB)  mytest

     CHARACTER SET: 設置字符集

mysql> CREATE DATABASE mytest CHARACTER SET utf8mb4;
Query OK, 1 row affected (0.00 sec)


   * 在數據庫中創建表(table)

   ENGINE=INNODB 指定Innodb 存儲引擎

     CHARSET=utf8mb4 設置表字符集

mysql> CREATE TABLE users(
    -> id BIGINT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    -> name VARCHAR(32) NOT NULL,
    -> sex ENUM('M', 'F') NOT NULL,
    -> age INT NOT NULL
    -> )ENGINE=INNODB CHARSET=utf8mb4;
Query OK, 0 rows affected (0.03 sec)


3. 插入數據操作

  * select 插入單條數據

mysql> INSERT INTO users SELECT NULL, 'tom', 'M', 29;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from users;
+----+------+-----+-----+
| id | name | sex | age |
+----+------+-----+-----+
|  1 | tom  | M   |  29 |
+----+------+-----+-----+
1 row in set (0.00 sec)


   * values 插入單條數據

mysql> INSERT INTO users VALUES (NULL, 'jak', 'F', 33);
Query OK, 1 row affected (0.01 sec)

mysql> select * from users;
+----+------+-----+-----+
| id | name | sex | age |
+----+------+-----+-----+
|  1 | tom  | M   |  29 |
|  2 | jak  | F   |  33 |
+----+------+-----+-----+
2 rows in set (0.00 sec)


   * select 指定列插入 [ id列會自增 ]

mysql> INSERT INTO users(name, sex, age) SELECT 'sea', 'M', '26';
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from users;
+----+------+-----+-----+
| id | name | sex | age |
+----+------+-----+-----+
|  1 | tom  | M   |  29 |
|  2 | jak  | F   |  33 |
|  3 | sea  | M   |  26 |
+----+------+-----+-----+
3 rows in set (0.01 sec)


   * values 指定列插入

mysql> INSERT INTO users(name, sex, age) VALUES ('hai', 'F', '18');
Query OK, 1 row affected (0.02 sec)

mysql> select * from users;
+----+------+-----+-----+
| id | name | sex | age |
+----+------+-----+-----+
|  1 | tom  | M   |  29 |
|  2 | jak  | F   |  33 |
|  3 | sea  | M   |  26 |
|  4 | hai  | F   |  18 |
+----+------+-----+-----+
4 rows in set (0.00 sec)


   * values 插入多條數據

mysql> INSERT INTO users VALUES (null, 'test1', 'F', 23), (null, 'test2', 'M', 34);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from users;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | tom   | M   |  29 |
|  2 | jak   | F   |  33 |
|  3 | sea   | M   |  26 |
|  4 | hai   | F   |  18 |
|  5 | test1 | F   |  23 |
|  6 | test2 | M   |  34 |
+----+-------+-----+-----+
6 rows in set (0.00 sec)


   * values 指定列插入多條數據

mysql> INSERT INTO users(name, sex, age) VALUES ('user1', 'F', 23), ('user2', 'M', 34);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from users;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | tom   | M   |  29 |
|  2 | jak   | F   |  33 |
|  3 | sea   | M   |  26 |
|  4 | hai   | F   |  18 |
|  5 | test1 | F   |  23 |
|  6 | test2 | M   |  34 |
|  7 | user1 | F   |  23 |
|  8 | user2 | M   |  34 |
+----+-------+-----+-----+
8 rows in set (0.00 sec)


4. 修改數據操作   

        update <table_name> 

     set column = val, ....

     where 條件語句 (沒有寫條件語句會修改表中所有數據)

   * 一次修改單列數據操作

mysql> select * from users where id = 1;
+----+------+-----+-----+
| id | name | sex | age |
+----+------+-----+-----+
|  1 | tom  | M   |  29 |
+----+------+-----+-----+
1 row in set (0.00 sec)

mysql> UPDATE users set name='lisea' where id = 1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from users where id = 1;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | lisea | M   |  29 |
+----+-------+-----+-----+
1 row in set (0.00 sec)


   * 一次修改多列數據操作

mysql> select * from users where id = 1;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | lisea | M   |  29 |
+----+-------+-----+-----+
1 row in set (0.01 sec)

mysql> UPDATE users set sex='F',age=33 where id = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from users where id = 1;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | lisea | F   |  33 |
+----+-------+-----+-----+
1 row in set (0.00 sec)


5. 刪除數據操作

   delete from <table_name> 

  where 條件語句 [ 沒有寫條件語句會修改表中所有數據 ]

mysql> select * from users;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  1 | lisea | F   |  33 |
|  2 | jak   | F   |  33 |
|  3 | sea   | M   |  26 |
|  4 | hai   | F   |  18 |
|  5 | test1 | F   |  23 |
|  6 | test2 | M   |  34 |
|  7 | user1 | F   |  23 |
|  8 | user2 | M   |  34 |
+----+-------+-----+-----+
8 rows in set (0.00 sec)

mysql> DELETE FROM users WHERE id = 1;
Query OK, 1 row affected (0.02 sec)

mysql> select * from users;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  2 | jak   | F   |  33 |
|  3 | sea   | M   |  26 |
|  4 | hai   | F   |  18 |
|  5 | test1 | F   |  23 |
|  6 | test2 | M   |  34 |
|  7 | user1 | F   |  23 |
|  8 | user2 | M   |  34 |
+----+-------+-----+-----+
7 rows in set (0.00 sec)

mysql> DELETE FROM users WHERE id in (2, 4);
Query OK, 2 rows affected (0.01 sec)

mysql> select * from users;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  3 | sea   | M   |  26 |
|  5 | test1 | F   |  23 |
|  6 | test2 | M   |  34 |
|  7 | user1 | F   |  23 |
|  8 | user2 | M   |  34 |
+----+-------+-----+-----+
5 rows in set (0.00 sec)

mysql> DELETE FROM users WHERE id >= 7;
Query OK, 2 rows affected (0.01 sec)

mysql> select * from users;
+----+-------+-----+-----+
| id | name  | sex | age |
+----+-------+-----+-----+
|  3 | sea   | M   |  26 |
|  5 | test1 | F   |  23 |
|  6 | test2 | M   |  34 |
+----+-------+-----+-----+
3 rows in set (0.00 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章