zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

MySQL筑基篇之增删改查操作详解

mysql 详解 操作 增删 改查
2023-06-13 09:20:08 时间

1.指定字段添加数据

给表中的部分列添加数据:值的顺序必须跟指定列的顺序保持一致

语法:insert into 表名(列1,列2, ) values(值1,值2, )

2.默认添加数据

向表中的所有列添加数据:值的顺序必须跟字段顺序保持一致

语法:insert into 表名 values(值1,值2, )

2、有自增列时

1.对于指定字段的添加

不用指定自增列,语法同无自增列一致

2.对于默认情况的添加

必须手动为自增列赋值或者填上null

例如:insert into t_person VALUES(null, wangwu ,32, 男 ,150.9 )

自增列设置方法:

create table if not exists t_person(
p_id int primary key auto_increment, 主键 自增

关键字:auto_increment

二、删除表中数据

1、使用delete

语法:delete from 表名 [where条件]

删除所有数据,例如:delete from t_person

删除指定数据,例如:delete from t_person where p_id=8

2、使用truncate

语法:truncate table 表名

通过表截断(truncate)的方式删除数据要优于使用delete,原因:

delete是一条一条删除,效率低,而truncate是直接在物理空间中将存放该表数据的空间截断舍弃,效率更快

delete主键会继续删除之前的自增,而truncate会重新开始自增

三、修改表中数据

语法:update 表名 set 列名1=新值,列名2=新值, [where 条件]

操作整张表,例如:update t_person set age=18

操作部分数据,例如:update t_person set age=28 where p_id=1

第一个例子的含义是把t_person表中所有的age属性改为18,第二个含义是只把p_id为1对应的age改为28

四、*查询操作

查询是数据库基础的重点,拿小本本记上

1、简单查询

1.查询所有列

select * from 表名

2.查询部分列

select 列名1,列名2,... from 表名

可以通过列出所有字段名的方式查询所有列

弊端:书写繁琐

优势:可维护性更高、更灵活、执行效率更快

3.别名

select 列名1 as 别名1,列名2 as 别名2,... from 表名

as关键字可省

select 列名1 别名1,列名2 别名2,... from 表名

表名也可以起别名

别名使用示例:

SELECT employee_id as 员工编号,salary as 工资 from employees
SELECT employee_id 员工编号,salary 工资,first_name,last_name from employees e

4.数学运算

select 列名+数字,列名-数字,列名*数字,列名/数字,列名%数字 from 表名

5.去重

select distinct 列名 from 表名

去重规则可以为多个列,只有当规则中的所有列的信息完全一致时才会去重 :

select distinct 列名1,列名2,... from 表名

6.case when

select 列名1,列名2,
case
when 条件1 then 结果2
when 条件2 then 结果2

else 其他结果
end
from 表名

使用示例:

查询员工id及其工资,并对工资进行评级:工资 10000 高薪,工资 8000 中级,工资 5000 低级,其他 底层

select employee_id,salary,
case
when salary 10000 then 高薪
when salary 8000 then 中级
when salary 5000 then 低级
else 底层
end as 薪资等级
from employees

7.查询表详情

describe 表名

describe可以简写成desc:

desc 表名

2、条件查询

语法:

select 列名 from 表名 where 条件

1.单条件查询

查询工资 10000的员工信息:

SELECT * from employees where salary 10000

比较的类型为字符串时,对比数据需要加上单引号

mysql默认不区分大小写,如有需求,则在对应位置添加binary关键字

查询first_name为Steven的所有员工信息:

select * from employees where first_name= STEVEN

区分大小写:

select * from employees where binary first_name= STEVEN

2.多条件查询

多个条件之间须通过and或者or进行拼接:

and:代表并且,多个条件同时满足,相当于java中的

or:代表或者,满足任意一个即可,相当于java中的||

3.区间查询

在区间内

between 最小值 and 最大值

不在范围内

not between 最小值 and 最大值

4.枚举查询

在列举范围内

列名 in(值1,值2,值3, )

查询员工id为100、105、110的员工信息

select * from employees where employee_id=100 or employee_id=105 or employee_id=110

等价于:

select * from employees where employee_id in(100,105,110)

不在列举范围内

列名 not in(值1,值2,值3, )

查询员工id不是100、105、110的员工信息

select * from employees where employee_id not in(100,105,110)

5.空值查询

为空时

列名 is null

不为空时

列名 is not null

查询commission_pct为null的员工信息

select * from employees where commission_pct is null

查询commission_pct不为null的员工信息

select * from employees where commission_pct is not null

6.模糊查询

语法:

where 列名 like 值

%:代表不固定长度,可以为0-n个字符

_:代表一个长度

示例:

查询first_name中包含s的员工信息

select * from employees where first_name like %s%

查询firstname中以s开头的员工信息

select * from employees where first_name like s%

查询firstname中以s结尾的员工信息

select * from employees where first_name like %s

查询firstname中第二个字母为s的员工信息

select * from employees where first_name like _s%

对查询结果进行指定规则的排序显示

1、单列排序

select 列名 from 表名 order by 列名 asc(升序)|desc(降序)

示例:

根据工资从高到低显示员工信息

select * from employees order by salary desc

根据工资从低到高

select * from employees order by salary asc
select * from employees order by salary

tips: 默认为升序排列

2、多列排序

order by 列名1 asc|desc , 列名2 asc|desc,...

示例:

根据工资从低到高显示员工信息,如果工资相同,根据员工id从高到低显示

select * from employees order by salary asc,employee_id desc

3、where+order by

 select 列名 from 表名
 where 筛选条件
 order by 排序条件

示例:

查询工资 10000的员工信息,从高到低显示

select * from employees where salary 10000 order by salary desc

到此这篇关于MySQL筑基篇之增删改查操作详解的文章就介绍到这了,更多相关MySQL增删改查内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!


我想要获取技术服务或软件
服务范围:MySQL、ORACLE、SQLSERVER、MongoDB、PostgreSQL 、程序问题
服务方式:远程服务、电话支持、现场服务,沟通指定方式服务
技术标签:数据恢复、安装配置、数据迁移、集群容灾、异常处理、其它问题

本站部分文章参考或来源于网络,如有侵权请联系站长。
数据库远程运维 MySQL筑基篇之增删改查操作详解