SQL再學習plus
阿新 • • 發佈:2022-03-18
DML
新增資料
程式碼實現
mysql> select * from stu; +------+------+------+ | name | id | sex | +------+------+------+ | xue | 1 | 1 | | he | 2 | 2 | +------+------+------+ 2 rows in set (0.01 sec) mysql> insert into stu(name,id,sex) values("小爽",1,"女"); Query OK, 1 row affected (0.01 sec) mysql> select * from stu; +------+------+------+ | name | id | sex | +------+------+------+ | xue | 1 | 1 | | he | 2 | 2 | | 小爽 | 1 | 女 | +------+------+------+ 3 rows in set (0.01 sec)
修改資料
程式碼實現
mysql> select * from stu; +------+------+------+ | name | id | sex | +------+------+------+ | xue | 1 | 男 | | he | 2 | 2 | | 小爽 | 1 | 女 | | 小程 | 2 | 男 | +------+------+------+ 4 rows in set (0.02 sec) mysql> update stu set sex = "男" where name = "he"; Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from stu; +------+------+------+ | name | id | sex | +------+------+------+ | xue | 1 | 男 | | he | 2 | 男 | | 小爽 | 1 | 女 | | 小程 | 2 | 男 | +------+------+------+ 4 rows in set (0.00 sec)
刪除資料
程式碼實現
mysql> select * from stu; +------+------+------+ | name | id | sex | +------+------+------+ | xue | 1 | 男 | | he | 2 | 男 | | 小爽 | 1 | 女 | | 小程 | 2 | 男 | +------+------+------+ 4 rows in set (0.00 sec) mysql> delete from stu where name = "he"; Query OK, 1 row affected (0.01 sec) mysql> select * from stu; +------+------+------+ | name | id | sex | +------+------+------+ | xue | 1 | 男 | | 小爽 | 1 | 女 | | 小程 | 2 | 男 | +------+------+------+ 3 rows in set (0.00 sec)
DQL
程式碼實現
mysql> select name , sex from stu;
+------+------+
| name | sex |
+------+------+
| xue | 男 |
| 小爽 | 女 |
| 小程 | 男 |
+------+------+
3 rows in set (0.00 sec)
程式碼實現
mysql> select distinct sex from stu;
+------+
| sex |
+------+
| 男 |
| 女 |
+------+
2 rows in set (0.03 sec)
起別名
程式碼實現
mysql> select name as "姓名",sex "性別" from stu;
+------+------+
| 姓名 | 性別 |
+------+------+
| xue | 男 |
| 小爽 | 女 |
| 小程 | 男 |
+------+------+
3 rows in set (0.00 sec)