mysql基础-增INSERT、删DELETE、改UPDATE、查SELECT数据库操作(二)
程序员文章站
2024-03-20 10:37:30
...
紧接上一篇博客,上一篇介绍了有关数据库表的一些有关操作,这一篇介绍在数据库里面对于数据增、删、改、查的一些语句。
上一篇连接: https://blog.csdn.net/brucewong0516/article/details/79871413
现在有个数据库表user3:
mysql> desc user3;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| symbol | char(10) | NO | | NULL | |
| name | varchar(30) | NO | | NULL | |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
一、INSERT插入数据
1.1插入单行数据,不提取关键字
mysql> INSERT INTO user3 VALUES(2,5,'hh');
Query OK, 1 row affected (0.00 sec)
1.2插入单行数据,提取关键字
mysql> INSERT INTO user3(id,symbol,name) VALUES(5,2,'lh');
Query OK, 1 row affected (0.00 sec)
1.3同时插入多行数据
mysql> INSERT INTO user3 VALUES (10,10,'th'),(8,4,'jj');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
二、SELECT查询数据
- DISTINCT :查找唯一的不同的值
- *:表示所有
- WHERE:可以添加条件,没有条件,则显示所有行
a可以使用的操作符
b除非使用 LIKE 来比较字符串,否则MySQL的WHERE子句的字符串比较是不区分大小写的。
c可以使用 BINARY 关键字来设定WHERE子句的字符串比较是区分大小写的。
2.1选择所有的数据
mysql> select * from user3;
+----+--------+------+
| id | symbol | name |
+----+--------+------+
| 2 | 5 | hh |
| 5 | 2 | lh |
| 8 | 4 | jj |
| 9 | 5 | hh |
| 10 | 10 | th |
+----+--------+------+
5 rows in set (0.00 sec)
2.2选择特定某个值的数据
mysql> select id,name from user3 where id=2;
+----+------+
| id | name |
+----+------+
| 2 | hh |
+----+------+
1 row in set (0.00 sec)
2.3选择特定某个值的数据
mysql> select id,name from user3 where id<6;
+----+------+
| id | name |
+----+------+
| 2 | hh |
| 5 | lh |
+----+------+
2 rows in set (0.00 sec)
2.4选择名字相同的只显示一个
mysql> SELECT DISTINCT name FROM user3 ;
+------+
| name |
+------+
| hh |
| lh |
| jj |
| th |
+------+
4 rows in set (0.00 sec)
三、UPDATE更新数据
mysql> UPDATE user3 set symbol=99 where name='hh';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from user3
-> ;
+----+--------+------+
| id | symbol | name |
+----+--------+------+
| 2 | 99 | hh |
| 5 | 2 | lh |
| 8 | 4 | jj |
| 9 | 99 | hh |
| 10 | 10 | th |
+----+--------+------+
5 rows in set (0.00 sec)
四、DELETE删除数据
mysql> delete from user3 where name='jj';
Query OK, 1 row affected (0.00 sec)
mysql> select * from user3;
+----+--------+------+
| id | symbol | name |
+----+--------+------+
| 2 | 99 | hh |
| 5 | 2 | lh |
| 9 | 99 | hh |
| 10 | 10 | th |
+----+--------+------+
4 rows in set (0.00 sec)