mysql数据库增删改查学习笔记
对数据的增删改查学习记录
准备一个空表(student),有三个字段,id,name,age
mysql> select * from student;
Empty set (0.00 sec)
mysql> desc student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(128) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> show create table student;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`name` varchar(128) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
增加insert
插入数据
INSERT INTO table_name ( 字段1, 字段2,…字段N ) VALUES ( 值1, 值2,…值N );
如果数据是字符型,必须使用单引号或者双引号,如:“value”
例:插入id为1,姓名zhangsan,年龄18
插入id为2,姓名lisi,年龄19
插入id为空,姓名tom,年龄20
三条数据
mysql> insert into student (id,name,age) values (1,'zhangsan',18);
Query OK, 1 row affected (0.00 sec)
也可以不加字段,但是后面的数据要和字段对应上
mysql> insert into student values (2,'lisi',19);
Query OK, 1 row affected (0.00 sec)
如果少了会报错
mysql> insert into student values (2,'lisi');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
可以有值不填,只要不填的值在设定中不是非空的就可以
mysql> insert into student (name,age) values ('tom',20);
Query OK, 1 row affected (0.00 sec)
查询select
SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[LIMIT N][ OFFSET M]
- 查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
- SELECT 命令可以读取一条或者多条记录。
- 可以使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
- 可以使用 WHERE 语句来包含任何条件。
- 可以使用 LIMIT 属性来设定返回的记录数。
- 可以通过OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0。
最简单的查询就是直接查全部
mysql> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 18 |
| 2 | lisi | 19 |
| NULL | tom | 20 |
+------+----------+------+
3 rows in set (0.00 sec)
查询指定字段
mysql> select name,age from student;
+----------+------+
| name | age |
+----------+------+
| zhangsan | 18 |
| lisi | 19 |
| tom | 20 |
+----------+------+
3 rows in set (0.00 sec)
指定条件查询(where)
mysql> select * from student where age=18;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 18 |
+------+----------+------+
1 row in set (0.00 sec)
只返回查询到的1行(limit)
mysql> select * from student limit 1;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 18 |
+------+----------+------+
1 row in set (0.01 sec)
偏移量的使用
取两个数据,偏移量为1,就是说不要第一个数据,从第二个开始顺序取2个数据
mysql> select * from student limit 2 offset 1;
+------+------+------+
| id | name | age |
+------+------+------+
| 2 | lisi | 19 |
| NULL | tom | 20 |
+------+------+------+
2 rows in set (0.00 sec)
offset和limit连用
取一条数据,排查掉前2条数据,顺序取
mysql> select * from student limit 2,1;
+------+------+------+
| id | name | age |
+------+------+------+
| NULL | tom | 20 |
+------+------+------+
1 row in set (0.00 sec)
修改update
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]
- 可以同时更新一个或多个字段。
- 可以在 WHERE 子句中指定任何条件。
- 可以在一个单独表中同时更新数据
还是用刚刚的那个表,在插入几个数据
mysql> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 18 |
| 2 | lisi | 19 |
| NULL | tom | 20 |
| 2 | aaa | 19 |
| 3 | bbb | 20 |
+------+----------+------+
5 rows in set (0.00 sec)
将id为null的数据变成5
修改的话要给一个条件,不然会修改所有的数据
mysql> update student set id=5 where name='tom';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 2 | aaa | 19 |
| 5 | tom | 20 |
| 2 | lisi | 19 |
| 3 | bbb | 20 |
| 1 | zhangsan | 18 |
+------+----------+------+
5 rows in set (0.00 sec)
补充小知识:
修改密码
使用update修改数据库用户密码
update user set authentication_string=password("新密码") where user='root';
删除delete
DELETE FROM table_name [WHERE Clause]
- 如果没有指定 WHERE 子句,MySQL 表中的所有记录将被删除。
- 可以在 WHERE 子句中指定任何条件
- 可以在单个表中一次性删除记录。
mysql> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 2 | aaa | 19 |
| 5 | tom | 20 |
| 2 | lisi | 19 |
| 3 | bbb | 20 |
| 1 | zhangsan | 18 |
+------+----------+------+
5 rows in set (0.00 sec)
删除年龄为20的数据
mysql> delete from student where age=20;
Query OK, 2 rows affected (0.00 sec)
mysql> select * from student;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 2 | aaa | 19 |
| 2 | lisi | 19 |
| 1 | zhangsan | 18 |
+------+----------+------+
3 rows in set (0.00 sec)
删除全部数据
mysql> delete from student ;
Query OK, 3 rows affected (0.00 sec)
mysql> select * from student;
Empty set (0.00 sec)
补充:
删除全表数据还可以使用truncate table table_name;