mysql数据库--表中数据的基本操作
约束:
1.主键(primary key) : 它的整个列中元素唯一且非空。
auto_increment 自动增加 每次加1,只能用于创建表时。报错、删除数据也会自增。 需要 truncate table XXX,才会从1开始。
例如:create table student1(id int primary key auto_increment,name varchar(10));insert into student1(name) values('aaa'),('bbbb'),('cccc');
2.外键(foreign key):会校验外键在主表中是否存在
3.非空: not null
4.默认值 : default 默认值
5.检查约束 mysql enum()
6. 唯一(unique):唯一但是可以为空 。
1.插入数据
1.1 为所有列插入数据
1.1.1 在insert语句里指定所有字段名
语法格式:
insert into 表名(字段名1,字段名2,...) values(值1,值2,.....);
1.1.2 在insert语句里不指定字段名
语法格式:
insert into 表名 values(值1,值2,....);
这种方式值必须和表中的字段对应,并且每个字段都要赋值,否则错误。
1.2 为指定列插入数据
语法格式:
insert into 表名(字段名1,字段名2,...) values(值1,值2,.....);
其他不插入数据的列使用默认值即可。
1.3 批量插入数据
1.3.1 为所有列批量插入数据
语法格式:
insert into 表名(字段名1,......) values(值1,...),(值1,....),......;
1.3.2 为指定列批量插入数据
语法格式:
insert into 表名(字段名1,…) values(值1,…),(值1,…),…;
2.查看数据
语法格式:
select * from 表名;
3.修改数据
3.1 更新全部数据
语法格式:
update 表名 set 字段名=‘新值’;
3.2 更新部分数据
update 表名 set 字段名=‘新值’ [where 条件];update 表名 set 字段名1=‘新值’,字段名2=‘新值’ [where 条件];
4.删除数据
删除表中所有数据,但保留表结构:
delete from 表名;
删除表中指定数据:
delete from 表名 where 条件;
5.单表查询数据
5.1普通查询
查询表中所有数据:
select * from 表名;
指定字段查询数据:
select 字段名1,字段名2,... from 表名;
带条件查询数据:
select 字段名 form 表名 where 条件;
5.2 带between的查询(范围查询)
select 字段名 form 表名 where 条件1 and 条件2;select 字段名 form 表名 where 字段名 between 条件1 and 条件2;
5.3模糊查询(like)
_表示任意一个字符,%任意多个字符
5.4查询并排序:
order by (升序)
select * from 表名 order by 字段名; +desc降序
order by 放在 where条件后,limit放在order后
5.5 limit : 限制
select * from 表名 limit n ; 表示取前n条数据select * from 表名 limit offset(偏移量n) ,m;表示跳过前n条数据,再取m条数据
5.6 distinct:去重
select distinct 字段名 from 表名 ;
5.7聚合函数:
max:
select max(字段名) from 表名;
min:
select min(字段名) from 表名;
sum:
select sum(字段名) from 表名;
average:
select avg(字段名) from 表名;
count:
select count(字段名) from 表名 where 条件;select count(*) from 表名 where 条件;
5.8 分组查询 :gruop by
select 字段1,... from 表名 where 条件 group by 字段 [having 字段][order by 字段][limit n]
5.9 子查询
两次查询,将第一次查询的结果作为第二次查询的参数传入。
6.查询练习
创建员工表employee包含如下信息:
列名 | 员工编号eno | 员工姓名ename | 员工年龄eage | 员工性别esex | 员工值为ejob | 员工入职时间ehiredate | 员工奖金 ebonus | 员工底薪ebsalary | 部门编号deptno |
---|---|---|---|---|---|---|---|---|---|
要求 | 主键 | 不能为空 | 男女中选择 |
create table employee(eno int primary key,ename varchar(5) not null,eage int,esex enum('男','女'),ejob varchar(6),ehiredate date,ebonus int,ebsalary int,deptno int);
2.增加如下员工信息
insert into employee values(1,'李鹏飞',32,'男','经理',20161112,5000,8000,10);insert into employee values(2,'王鹏飞',27,'男','销售员',20181020,2000,1000,10);insert into employee values(3,'肖美',24,'女','前台',20190321,0,3500,20);
insert into employee values(4,'王乐乐',30,'女','经理',20170302,0,9000,20);insert into employee values(5,'张丽丽',28,'女','行政人员',20191111,0,5000,20);insert into employee values(6,'徐华',33,'女','销售员',20191117,500,4000,10);insert into employee values(7,'赵辉',40,'男','经理',20161117,0,50000,30);
insert into employee values(8,'王伟',35,'男','开发工程师',20181118,0,30000,30);
insert into employee values(9,'钱慧辉',28,'女','开发工程师',20190417,0,25000,30);
insert into employee values(10,'孙雯彩',29,'女','开发工程师',20170915,0,20000,30);
3.修改肖美的奖金为500
update employee set ebonus=500 where ename='肖美';
4.删除名为孙雯彩的员工
delete from employee where ename='孙雯彩';
5.查询出部门编号为30的所有员工
select * from employee where deptno=30;
6.所有销售员的姓名、编号和部门编号
SELECT ename,eno,deptno from employee where ejob='销售员';
7.找出奖金高于底资的员工
SELECT * from employee where ebonus>ebsalary;
8.找出奖金高于工资60%的员工
SELECT * from employee where ebonus>ebsalary*0.6;
9.找出部门编号为20中所有经理,部门编号为10中所有销售员,还有即不是经理又不是销售员但其工资大或等于20000的所有员工详细资料。
SELECT * from employee where (deptno=20 and ejob='经理') or (deptno=10 and ejob='销售员') or (ejob not in(‘经理’,‘销售员’)and ebsalary >= 20000);
10.查询有奖金的员工的职位
SELECT ejob from employee where ebonus <>0;
11.查询2017年入职的员工信息
select * from employee where ehiredate=20170101;
12.查询 底薪+奖金的薪资在3000到7000之间的(范围查询)
SELECT * from employee where (ebonus+ebsalary) BETWEEN 3000 and 7000;
13.查询入职日期在2019-12-30之前入职的员工
select * from employee where ehiredate=20191230;
14.查询年龄是22,24,33,28岁的所有员工(in,not in)
select * from employee where eage in (22,24,33,28);
模糊查询:
15.查询名字由三个字组成的员工
select * from employee WHERE ename like'___';
16.查询姓王的员工
select * from employee where ename like '王%';
17.查询名字带飞的员工
select * from employee where ename like '%飞%';
排序查询:
18.查询所有员工并按年龄排序
select * from employee where ename like '%飞%';
19.查询所有部门号为10,并按底薪排序
select * from employee where deptno=10 order by edsalary;
20.查询 奖金小于底薪的所有员工,并按年龄排序
select * from employee where ebsalary<ebonus order by eage;
21.查询总工资大于8000的员工,并按总工资排序
select * from employee where ebsalary+ebonus>8000 and esex='男' order by ebsalary+ebonus;
22.查询总工资大于10000,并姓孙的员工
select * from employee where ebsalary+ebonus>10000 and ename like'孙%';
23.查询2019年前入职的经理,并按底薪排序
select * from employee where ehiredate<'2019-01-01' order by ebsalary;
24.查找10,20,30部门总工资小于10000的员工信息,并按照入职时间排序
select * from employee where ebsalary+ebonus<10000 and deptno in(10,20,30) order by ehiredate;
25.查询姓王和姓孙的女生信息
select * from employee where esex='女' and (ename like'孙%' or ename like'王%');
26.查询奖金为null或为0的员工,并按照部门号码排序
select * from employee where ebonus=0 or ebonus is null order by deptno;
27.查询员工姓名和总工资,并按总工资排序
select ename,ebsalary+ifnull(ebonus,0) as 总工资 from employee order by 总工资;
别名可以用于order by,不能用于where,对数据库无影响
数字和null相加结果为null,可以用ifnull(为null的字段,指定值)函数将null替换为指定值
聚合函数:
28.查询员工表最大年龄
select max(eage) from employee;
29.查询所有男生的人数
select count(*) from employee where esex='男';
30.查询2,4,5部门的总人数
select count(*) from employee where deptno in (10,20);
31.查询底薪超过1万的男生人数
select count(*) from employee where ebsalary>10000 and esex='男';
32查询女生的薪资总和
select sum(ebsalary+ebonus) from employee where esex='女';
33.查询姓王的员工人数
select count(*) from employee where ename like'王%';
34.查询职位是销售员,经理的工资总和
select sum(ebsalary+ebonus) from employee where ejob='销售员' or ejob='经理';
35.查询30号部门有哪些职位
select DISTINCT ejob from employee where deptno=30;
分组查询:
36.查询表中男女的个数
select esex,count(*) as number from employee group by esex;
37.查询各个部门的人数
select deptno,count(*) as number from employee group by deptno;
38.查询各个部门的最高工资
select deptno,max(ebsalary) from employee group by deptno;
39.查询各个职位的最大年龄
select ejob,max(eage) from employee group by ejob;
40.查询10号部门男女生人数
select esex,count(*) from employee where deptno=10 group by esex;
41.查询30号部门各个职位的最高工资
select ejob,max(ebsalary) from employee where deptno=30 group by ejob;
42.查询有最高底薪的前3个部门
select deptno,max(ebsalary) as 最高底薪 from employee group by deptno order by 最高底薪 desc limit 3;
43.查询各个部门最高底薪大于10000的部门
select deptno,max(ebsalary) as 最高底薪 from employee group by deptno having 最高底薪>10000;
having也表示条件筛选,它在gruop by 后面执行,不能独立存在
44.查询10号部门各个职位的最高底薪低于10000的部门
select ejob,max(ebsalary) as 最低工资 from employee where deptno=10 group by ejob having 最低工资<10000;
45.查询各个部门的男女生人数
select deptno,esex,count(*) from employee group by deptno,esex order by deptno;
先按gruop by 后面第一个字段分组,再按第二个字段分组
子查询:
46.查询比20号部门最高底薪还高的员工信息
select * from employee where ebsalary>(select MAX(ebsalary) from employee where deptno=20);
47.低于平均底薪的员工信息
select * from employee where ebsalary<(select avg(ebsalary) from employee);
48.查询与最高底薪同一个部门的员工信息
select * from employee where deptno=(select deptno from employee where ebsalary=(select max(ebsalary) from employee));
49.查询年龄大于25,比平均底薪高的员工信息
select * from employee where eage>25 and ebsalary>(select avg(ebsalary) from employee);
50.查询与王伟在一个部门的员工信息
select * from employee where deptno=(SELECT deptno from employee where ename='王伟');
51.查询比肖美的底薪还低的员工所在的部分编号
SELECT deptno from employee WHERE ebsalary<(select ebsalary from employee where ename='肖美');
mysql数据库--表中数据的基本操作相关推荐
- MySQL~数据库表中数据的增删查改(基础篇)
文章目录 增加 建表 多行数据 全列插入 多行数据 指定列插入 查询 全列查询 指定列查询 查询字段为表达式 查询字段 名字重定义 去重 distinct 排序 order by 条件查询 运算符 比 ...
- MySQL学习笔记03【数据库表的CRUD操作、数据库表中记录的基本操作、客户端图形化界面工具SQLyog】
MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...
- python亿级mysql数据库导出_Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法...
本文实例讲述了python实现将MySQL数据库表中的数据导出生成csv格式文件的方法.分享给大家供大家参考,具体如下: #!/usr/bin/env python # -*- coding:utf- ...
- cpp导入excel到mysql_将EXCEL表格中的数据导入mysql数据库表中
本文转载自http://blog.sina.com.cn/s/blog_5d972ae00100gjij.html 今天项目上遇到需要将excel中的数据导入到数据库的classify表中,于是乎拼命 ...
- MYSQL数据库表A数据同步到表B
目前有一个功能,需要数据库表A的数据同步到数据库表B里,目前来说除了表名不一样,字段全部都一样. 表A 表B 然后执行sql语句 INSERT QC_RSLT_EXT SELECT * FROM QC ...
- Eclipse搭建SSH环境实现Struts2分页显示mysql数据库表中内容
2019独角兽企业重金招聘Python工程师标准>>> 摘要 学习(Eclipse搭建SSH(Struts2+Spring3+Hibernate3)框架项目教程),尝试搭建ssh框架 ...
- mysql删除表中数据
方法1:delete from 表名; 方法2:truncate table 表名; 比 较: 1> truncate 是整体删除 (速度较快),delete是逐条删除 (速度较慢) 2> ...
- eclipse ssh mysql数据库_Eclipse搭建SSH环境实现Struts2分页显示mysql数据库表中内容...
摘要 因运行后404错误,遂选择集成好SSH框架的MyEclipse开发工具: 最终实现了Struts2框架的分页查看学员信息,Spring3和Hibernate3的尝试宣告失败. 1.本项目的环境 ...
- php查询mysql显示在html表格中_php – 在网页上的HTML表格中显示MySQL数据库表中的值...
我想从数据库表中检索值并在页面的html表中显示它们. 我已经搜索了这个,但我找不到答案,虽然这肯定是容易的(这应该是数据库的基础知识).我想我搜索过的词语具有误导性. 数据库表名称是票证,它现在有6 ...
最新文章
- ROS image_transport使用笔记
- 优化内核报错及解决方法
- java界面 ppt_Java GUI图形用户界面 课件.ppt
- mysql5.7.16安装版_mysql数据库5.7.16安装版怎么安装图解
- nuxt asyncData extendRoutes nuxtServerInit
- 使用c++制作微服务计算服务
- st7789s显示芯片驱动代码
- 网络空间信息安全-密码学-信息密码技术基础
- word插入公式为灰色解决办法
- linux中#和## 用法
- html页脚版权声明,版权声明与免责声明的区别
- JDK8中Lambda 表达式语法糖脱糖[非原创]
- 钉钉扫码登录二维码错乱
- excel怎么算复购率(EXCEL怎么算平均分)
- 绘画入门新手要学的绘画基础有哪些
- python爬取天眼查数据(未破解图片验证及ajax版)
- mousedown mouseup click 触发顺序
- ESP32-CAM+PIR传感器=动作抓拍监控
- Win7服务器没及时响应,Win7无法停止服务提示“服务没有及时响应启动或控制请求”怎么办?...
- 《炬丰科技-半导体工艺》 纳米掩膜蚀刻