当前位置: 首页 > 工具软件 > wang crud > 使用案例 >

表的CRUD

宰父劲
2023-12-01
增加(Create)、读取查询(Retrieve)、更新(Update)、删除(Delete)
--------------- 增删查改 --------------
一.增加 insert into
insert into ta_name[不写,默认全部属性] values(数据),...


二.改 update
update 表名 set 要修改的列名=修改后的值,...
	[where 条件]
	[limit]

create table tt20(
	id int comment '学号',
	name varchar(10) not null
);

insert into tt20 values(1,'lisi'),(2,'wang'),(3,'mr.S');

update tt20 set id= 4; #修改一列

update tt20 set id=5 where name = 'lisi';# 修改name为lisi的id=5;

update tt20 set id=4 limit 1;# 只影响一个

三.删除 delete

delete from 表名  
	[where 条件] #不跟where条件 整表删除,一行一行的删

truncate table 表名 一次性删除 #将文件大小设置为0;

四.查询

select *是查询效率最低的。

1.select [distinct(去重)] * | (列名) from 表名;


create table student(
	id int(2) zerofill primary key auto_increment comment '学号不能重复,最多100个学生',
	name varchar(10),
	chinese decimal(4,1) default 0.0 not null,
	english decimal(4,1) default 0.0 not null,
	math decimal(4,1) default 0.0 not null
);

insert into student values
	(1,'lisi',89.0,78.0,90.0),
	(2,'tang',67.0,98.0,56.0),
	(3,'sun',87.0,78.0,77.0)
	;

insert into student(name,chinese,english,math) values
	('lao',88.0,98.0,90.0),
	('hong',82.0,84.0,67.0)
	;

insert into student values
	(6,'ru',55.0,85.0,45.0),
	(7,'菩萨',75.0,65.0,30.0)
	;

select id,name,chinese from student;

select distinct math from student; #去重查询

select chinese+english+math from student;#结果累加

2.as

select chinese+english+math as '总分' from student; #结果查询后 命名该列为as后的别名

3.where 子句 (and 优先级大于or)
将所有姓tang的学生的总成绩增加60% 

select name,(chinese+english+math)*1.6
	from student
	where name like 'tang%';#like 模糊查询
 
查询英语成绩大于90分的同学和他的英语成绩
select name,english from student
	where english > 90;

查询姓li并且数学成绩>85的同学姓名与他的总成绩

select name,(chinese+english+math) from student
	where name like 'li%' and math > 85;

查询总分大于200并且数学成绩小于语文成绩的姓唐的学生

select id,name from student
	where (chinese+math+english)>200 and
	math < chinese and
	name like 'tang';

查询英语分数在84-90的所有学生

select id,name from student
	where engish>=84 and english <=90;

4.between and

select id,name from student 
	where english between 84 and 90;# between..and.. 闭区间

查询总分大于200的学生姓名和他的总成绩

select name,(english+chinese+math) as total from student
	where total >200; #错误 where子句优先执行 故不能使用


5.order by 对结果进行排序 最后执行 放于末尾

select  [distinct(去重)] 列名 ...
	from 表名
	[where 子句]
	order by 列名 asc(默认) | desc;

查询总分大于200的学生姓名和他的总成绩,并且按总成绩升序排序

select name,(english+chinese+math) as total from student
	where (english+chinese+math) >200
	order by total;

6.分页查询
select...
	[where 条件]
	limit 起始位置,记录条数

查询所有学生信息,每页展示3条记录,输出第二页。

select * from student limit 3,3; #展示 4,5,6

7.聚合函数
count(列名|*) 不统计null
sum()
avg()
max()
min()
select count(math) from student; #7
select count(*) from student; #7

alter table student modify math decimal(4,1) default 0.0;

insert into student values(8,'opo',89,99,null);

select count(math) from student; #7
select count(*) from student; #8

8.分组查询 group by
条件过滤 having

select 列名..
	from 表名
	group by 列名

显示每个部门的平均和最高工资
select avg(列名),max(列名)
	from 表名
	group by 列名;

select avg(sal),max(sal),deptno
	from emp
	group by deptno;

显示平均工资的低于2000的部门编号和他的平均工资
select avg(sal) as avg_sal,deptno
	from emp
	group by deptno having avg_sal <2000;

--------------------多表查询----------------

数据来源于多个表的查询语句

查询所有员工姓名,工资和他的部门名称

默认笛卡尔积查询(1,2)(3,4) = [(1,3),(1,4),(2,3),(2,4)]
select ename,sal,dname
	from emp,dept
	where emp.deptno = dept.deptno;#条件


显示部门号为10的部门名称,员工名称和工资

select dname,ename,sal
	from emp,dept
	where emp.deptno = dept.deptno
	and emp.deptno = 10;#公有属性一定要注明是哪个表 不能只写deptno

-------------------子查询---------------
显示和SMITH同一部门的所有员工姓名,员工编号

select ename,empno
	from emp
	where empno=(select deptno
		from emp
		where ename = 'SMITH');#包含了SMITH

select deptno
	from emp
	where ename = 'SMITH';

-------------------索引----------------
原理:二叉树

create index empno_index on emp(empno); #给表emp的empno创建一个叫做empno_index的索引

创建索引的原则:
1.比较频繁作为查询条件的字段应该创建索引
2.唯一性太差的字段不太适合单独作为索引
3.更新非常频繁的字段不适合作为索引
4.不会出现在where子句的字段不适合创建索引

-------------------事务-----------------
 类似资料: