Mysql-数据表的操作-表数据-增加-删除-修改-查询

具体笔记内容目录

1. 删除数据表: drop table 表名称;
2. 插入数据,数据表增
3. 删除数据, 删除表数据
4. 修改数据,更新数据
5. 数据表的查询

删除数据表

drop table 表名称;

插入数据

1.指定字段添加数据
insert into 表名称 (字段1, 字段n) values (1, 值n)

注意: 如果是自增长类型的字段,要写 0 进行占位

2.不指定字段添加数据
insert into 表名称 values (1,值n) # 值根据字段数来定
3.同时添加多条数据
insert into 表名 values (1,值n), (2,值2n);

insert into 表名 (字段1,字段n) values (1,值n), (2,值n);

删除表的数据

1.删除部分数据: 需要使用where字句来指定删除那条数据
delete from 表名 where 字段=;
2.删除全部数据
delete from 表名;

修改数据

1.根据字段修改数据
# 修改的字段=值 可以是多个 用()包起来表示
update 表名 set 修改的字段=值 where 字段=; # 字段=值 指的是: 那一个字段
2.修改全部字段
update 表名 set 修改的字段=;

数据表的查询

1.查看数据表全部数据
select * from 表名称;
2.根据字段查看表数据
select 字段名1,字段名n from 表名称;

where 查询

3.使用where来查询,多样式
使用'比较运算符'= < > <= >= 等
还有逻辑运算符 or and not
select * from 表名称 where 字段=值
select * from 表名称 where 字段>值
select * from 表名称 where 字段<

模糊查询

方式一: like % _
方式二: rlike 正则
1.like查询:
'_' 代表一个字符 , '%'代表多个字段占位
select * from student where name like "吴__";
select * from student where name like "吴%";
2.正则查询:正则表达式
select * from student where name rlike "^吴"
select * from student where name rlike "a$"

范围查询

in 元组
not in 元组
between ... and ...
not between ... and .....

例子:

1.查询年龄 18, 20的信息
select * from 表名称 where 年龄字段 in (18,20);

2.查询年龄不是18-20的信息
select * from 表名称 where 年龄字段 not in (18,20)
3.查询年龄在18-20之间的信息
select * from 表名称 where 年龄字段 between 18 and 20

4.查询年龄不在18-20之间的信息
select * from 表名称 where 年龄字段 not between 18 and 20

空判断

判断字段是否是空的
is null
not is null
例子:
查出身高记录为空的数据
select * from students where height is null;

你可能感兴趣的:(MySQL)