標籤:mysql dba curd
1. 背景
* CURD 操作通常是使用關係型資料庫系統中的結構化查詢語言 (SQL)(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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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)
6. 總結
以需求驅動技術,技術本身沒有優略之分,只有業務之分。
本文出自 “sea” 部落格,請務必保留此出處http://lisea.blog.51cto.com/5491873/1943701
MySQL DML操作--------CURD最佳實戰