2018秋招面试问题(十四、MySQL基础(3))

第四部分:触发器的操作

什么是触发器?

触发器是监听某种情况,触发某种操作。

创建触发器有四元素:一个监听地点(table)、一个监听事件(insert、update、delete)、一个触发时间(before、after)、一个触发事件(insert、delete、update)。

触发器是数据库对象之一,与函数非常类似,需要声明、执行。但是触发器的执行是由事件来触发的。

当表发生更改时,需要自动进行一些处理,比如每增加一条学生的记录,学生的总数就需要同时改变,这时候就要执行一次计算学生总数的操作。

触发器的操作包括创建触发器查看触发器删除触发器

创建有一条执行语句的触发器:

create trigger trigger_dairytime before insert on product for each row insert into dairy values(NULL,'product',now());

这句话的意思是 对表product做任何操作都会触发触发器trigger_dairytime,使得在操作之前会先对表dairy插入一条当前时间的语句。for each row是指任何一个记录操作都会触发触发器。

创建完之后验证触发器是否起作用:

insert into product values(1,'apple',1.5);//向product插入数据

再查看dairy是否有插入一行时间信息:select * from dairy;

创建包含多条执行语句的触发器:

将$$设置为语句结束符:delimiter $$;

创建:create trigger trigger_dairytime2 after insert on product for each row

    -> begin

    -> insert into dairy values(NULL,'product',now());

    -> insert into dairy values(NULL,'product',now());

    -> end

-> $$   

然后再输入:delimiter;//将结束符号还原为默认结束符号“;”

然后再向product中插入数据来检验触发器是否设置成功。

查看触发器:show triggers \G;

在系统数据库information_schema中存在一个存储所有触发器信息的系统表triggers,可以通过这个查询,

select * from triggers where trigger_name='trigger_dairytime' \G;

删除触发器:drop trigger trigger_dairytime;

完了再用show triggers \G;来查看

第五部分:数据的操作

插入完整数据记录:

insert into product(id,name,price) values(9,'pear2',5.0);

//字符串加单引号

或者是:

insert into product values(7,'apple2',9.0);

插入部分数据记录:

insert into product(name,price) values('peach2',4.5);

插入多条数据记录:

insert into product(id,name,price) values(...),(...),(...);

还可以插入另一个表的查询结果,来实现表数据值的复制功能,这两个表的个数和参数类型需一样:

insert into fruits(id,name,price) select product_id,pro_name,pro_price from product;

//向fruits表中插入product表中的信息

更新特定数据记录:

update product set price=6.5 where name='pear2';

//将product表中name为pear2的price更新为6.5

update product set price=5.5 where id<5;

删除特定数据记录:

delete from product where name='pea2';

delete from product;//把这里面的数据全删了

第六部分:单表数据记录查询

查询字段数据:

select id,name,price from product;//字段之间也可以交换顺序

查询所有字段数据:

select * from product;

distinct 避免查询到重复的数据:

select distinct price from product;//加关键字distinct,去掉重复数据

实现四则运算数据查询:

select id,price*100 from product;

给price*100换一个可读性字段名:

select id,price*100 salary from product;

设置显示格式数据查询:

select CONCAT(id,'水果的总价:',price*100) salary from product;??

条件数据查询:

select name from product where price=5.5;

多条件的话可以加and或&&,或者是or等逻辑运算符。

带between和and的查询:

select name from product where price between 5 and 9;

查询不符合范围的数据记录:

select name from product where price not between 5 and 9;

空或非空查询:

select price from product where id is NULL;

select price from product where id is not NULL;

select price from product where not id is NULL;

带关键字in的集合查询:

select name from product where price in (5.5,4.5);

select name from product where price not in (5.5,4.5);

select name from product where not price in (5.5,4.5);

注意:对于关键字not in,如果集合中有NULL,则不会有任何查询结果。

Lile查询,like用来判断字段的值是否与指定的值相匹配。Like后面可以跟完整的字符,也可以支持“_”和“%”通配符,“_”匹配单个字符,“%”可以匹配任意长度的字符。

比如:

select name from product where name like 'p%';//查询name字段中以p开头的name

select name from product where not name like 'p%';//查询不以p开头的name

查询第二个字母为e的name:

select name from product where name like '_e%';

也可以用not like来查询不匹配的数据。

select name from product where name not like '%h%';

//查询不含字母h的name

select price from product where price like '%5%';

//查询price中包含数字5的

升序排序查询:

select * from product order by price asc;//price字段升序查询

不加asc也是默认的升序。

降序排序查询:

select * from product order by price desc;//price字段降序查询

多字段排序查询:

将price降序排列,如果price相等再按id降序排列:

select * from product order by price desc,id desc;

Limit来限制查询结果的数量:

  • 不指定初始位置:

select * from product where price=5.5 limit 3;//限制三条记录

  • 指定初始位置:

限制从满足条件的第二个记录开始,显示三条记录:

select * from product where price=5.5 order by price limit 1,3;

limit n 等价于 limit 0,n。

统计函数查询:

支持count()、avg()、sum()、max()、min()。统计函数都会忽略值为NULL的数据记录,但是不会忽略值为0的数据记录。

使用count():

查询表中的数据个数:

select count(*) number from product;

查询某个字段的个数:

select count(name) number from product;

还可以再加条件:

select count(price) number from product where price=5.5;

使用avg():

select avg(price) average from product;

还可以再加条件:

select avg(price) average from product where not price=0;//忽略掉0的情况

使用sum():

select sum(price) sumvalue from product;

使用max() min():

select max(price) maxval,min(price) minval from product;

注意:使用统计函数时,如果表中无数据,count会返回0,别的函数会返回NULL。

分组查询:

当字段中有重复值时,可将该字段分组查询:

select * from product group by type;

select group_concat(type) from product group by type;

//group_concat,是获取每组中指定参数的记录元素

显示出每个分组中的个数和名字:

select type,group_concat(name) names,count(name) number from product group by type;

实现多个字段分组查询:

select type,name from product group by type,name;

//首先按type分为6组,再对每组type针对name再分组

select type,name group_concat(name) names,count(name) from product group by type,name;

//获取每组中的记录元素,并统计记录数目

由type来分组,统计每组的平均price:

select type,avg(price) average from product group by type;

Having来实现条件分组查询,跟在group by的后面。having后面跟上条件:

由type分组显示出平均price大于5的名称和个数:

select type,avg(price) average,group_concat(name) names,count(name) number from product group by type having avg(price)>5;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值