SQL高阶学习

简介: SQL高阶学习
create database demo_01 default charset=utf8mb4;
use demo_01;
CREATE TABLE `city` (
`city_id` int(11) NOT NULL AUTO_INCREMENT,
`city_name` varchar(50) NOT NULL,
`country_id` int(11) NOT NULL, PRIMARY KEY (`city_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `country` (
`country_id` int(11) NOT NULL AUTO_INCREMENT,
`country_name` varchar(100) NOT NULL,
PRIMARY KEY (`country_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `city` (`city_id`, `city_name`, `country_id`) values(1,'西安',1);
insert into `city` (`city_id`, `city_name`, `country_id`) values(2,'NewYork',2);
insert into `city` (`city_id`, `city_name`, `country_id`) values(3,'北京',1);
insert into `city` (`city_id`, `city_name`, `country_id`) values(4,'上海',1);
insert into `country` (`country_id`, `country_name`) values(1,'China');
insert into `country` (`country_id`, `country_name`) values(2,'America');
insert into `country` (`country_id`, `country_name`) values(3,'Japan');
insert into `country` (`country_id`, `country_name`) values(4,'UK');
show index from city;
alter table tb_name add primary key(column_list);
create view g2 as select goods_id,goods_name,shop_price from goods where
shop_price > 3000;
create or replace view city_country_view
as
select t.*,c.country_name from country c , city t where c.country_id =
t.country_id;
select * from city_country_view;
# 创建存储过程
delimiter $
create procedure pro_test1()
begin
select 'Hello Mysql' ;
end$
delimiter ;
# 调用存储过程
call procedure_name() ;
-- 查询db_name数据库中的所有的存储过程
select name from mysql.proc where db='db_name';
-- 查询存储过程的状态信息
show procedure status;
-- 查询某个存储过程的定义
show create procedure test.pro_test1;
#删除存储过程
delimiter $
create procedure pro_test2()
begin
declare num int default 5;
select num+ 10;
end$
delimiter ;
# SET
DELIMITER $
CREATE PROCEDURE pro_test3()
BEGIN
DECLARE NAME VARCHAR(20);
SET NAME = 'MYSQL';
SELECT NAME ;
END$
DELIMITER ;
#也可以通过select ... into 方式进行赋值操作 :
DELIMITER $
CREATE PROCEDURE pro_test5()
BEGIN
declare countnum int;
select count(*) into countnum from city;
select countnum;
END$
DELIMITER ;
#if条件判断
if search_condition then statement_list
[elseif search_condition then statement_list] ...
[else statement_list]
end if;
-- 根据定义的身高变量,判定当前身高的所属的身材类型
-- 180 及以上 ----------> 身材高挑
-- 170 - 180 ---------> 标准身材
-- 170 以下 ----------> 一般身材
delimiter $
create procedure pro_test6()
begin
declare height int default 175;
declare description varchar(50);
if height >= 180 then
set description = '身材高挑';
elseif height >= 170 and height < 180 then
set description = '标准身材';
else
set description = '一般身材';
end if;
select description ;
end$
delimiter ;
# 根据定义的身高变量,判定当前身高的所属的身材类型
delimiter $
create procedure pro_test5(in height int)
begin
declare description varchar(50) default '';
if height >= 180 then
set description='身材高挑';
elseif height >= 170 and height < 180 then
set description='标准身材';
else
set description='一般身材';
end if;
select concat('身高 ', height , '对应的身材类型为:',description);
end$
delimiter ;
# 根据传入的身高变量,获取当前身高的所属的身材类型
create procedure pro_test5(in height int , out description varchar(100))
begin
if height >= 180 then
set description='身材高挑';
elseif height >= 170 and height < 180 then
set description='标准身材';
else
set description='一般身材';
end if;
end$
# 计算从1加到n的值
delimiter $
create procedure pro_test8(n int)
begin
declare total int default 0;
declare num int default 1;
while num<=n do
set total = total + num;
set num = num + 1;
end while;
select total;
end$
delimiter ;
create database emp;
use emp;
create table emp(
id int(11) not null auto_increment , name varchar(50) not null comment '姓名', age int(11) comment '年龄',
salary int(11) comment '薪水',
primary key(`id`)
)engine=innodb default charset=utf8 ;
insert into emp(id,name,age,salary) values(null,'金毛狮王',55,3800),(null,'白眉鹰王',60,4000),(null,'青翼蝠王',38,2800),(null,'紫衫龙王',42,1800);
create table emp_logs(
id int(11) not null auto_increment,
operation varchar(20) not null comment '操作类型, insert/update/delete', operate_time datetime not null comment '操作时间',
operate_id int(11) not null comment '操作表的ID', operate_params varchar(500) comment '操作参数', primary key(`id`)
)engine=innodb default charset=utf8;
#创建 insert 型触发器,完成插入数据时的日志记录 :
DELIMITER $
create trigger emp_logs_insert_trigger after insert
on emp
for each row begin
insert into emp_logs (id,operation,operate_time,operate_id,operate_params) values(null,'insert',now(),new.id,concat('插入后(id:',new.id,',
name:',new.name,', age:',new.age,', salary:',new.salary,')')); end $
DELIMITER ;
#创建 update 型触发器,完成更新数据时的日志记录 :
DELIMITER $
create trigger emp_logs_update_trigger after update
on emp
for each row begin
insert into emp_logs (id,operation,operate_time,operate_id,operate_params) values(null,'update',now(),new.id,concat('修改前(id:',old.id,',
name:',old.name,', age:',old.age,', salary:',old.salary,') , 修改后(id',new.id,
'name:',new.name,', age:',new.age,', salary:',new.salary,')'));
end $
DELIMITER ;
#创建delete 行的触发器 , 完成删除数据时的日志记录 :
DELIMITER $
create trigger emp_logs_delete_trigger after delete
on emp
for each row begin
insert into emp_logs (id,operation,operate_time,operate_id,operate_params) values(null,'delete',now(),old.id,concat('删除前(id:',old.id,',
name:',old.name,', age:',old.age,', salary:',old.salary,')'));
end $
DELIMITER ;
insert into emp(id,name,age,salary) values(null, '光明左使',30,3500);
insert into emp(id,name,age,salary) values(null, '光明右使',33,3200);
update emp set age = 39 where id = 3;
delete from emp where id = 5;
show triggers;
目录
相关文章
|
2月前
|
SQL 缓存 关系型数据库
MySQL技能完整学习列表6、查询优化——3、查询缓存——4、SQL优化技巧
MySQL技能完整学习列表6、查询优化——3、查询缓存——4、SQL优化技巧
65 0
|
3月前
|
SQL 测试技术 项目管理
轻松学习SQL外键约束的核心原理和实用技巧
轻松学习SQL外键约束的核心原理和实用技巧
48 0
|
2月前
|
SQL 关系型数据库 MySQL
MySQL技能完整学习列表3、SQL语言基础——1、SQL(Structured Query Language)简介——2、基本SQL语句:SELECT、INSERT、UPDATE、DELETE
MySQL技能完整学习列表3、SQL语言基础——1、SQL(Structured Query Language)简介——2、基本SQL语句:SELECT、INSERT、UPDATE、DELETE
51 0
|
1月前
|
SQL 数据库 内存技术
学习数据库要掌握的54条SQL查询语句
学习数据库要掌握的54条SQL查询语句
15 0
|
2月前
|
SQL 安全 关系型数据库
MySQL技能完整学习列表3、SQL语言基础——3、SQL运算符和函数
MySQL技能完整学习列表3、SQL语言基础——3、SQL运算符和函数
36 0
|
3月前
|
SQL Java 关系型数据库
JDBC技术【SQL注入、JDBC批量添加数据、JDBC事务处理、其他查询方式】(三)-全面详解(学习总结---从入门到深化)
JDBC技术【SQL注入、JDBC批量添加数据、JDBC事务处理、其他查询方式】(三)-全面详解(学习总结---从入门到深化)
33 0
|
4月前
|
SQL 关系型数据库 MySQL
解密 sqli靶场第一关:一步一步学习 SQL 注入技术
解密 sqli靶场第一关:一步一步学习 SQL 注入技术
42 0
解密 sqli靶场第一关:一步一步学习 SQL 注入技术
|
4月前
|
存储 SQL 关系型数据库
小白带你学习linux SQL语句(二十八)
小白带你学习linux SQL语句(二十八)
68 0
|
5月前
|
SQL 存储 程序员
数据库SQL查询知识学习指导
数据库SQL查询知识学习指导
48 1
|
5月前
|
存储 JSON 关系型数据库
认真学习MySQL中的explain分析SQL-2
认真学习MySQL中的explain分析SQL-2
30 0