create view 视图名称 as select 语句;
create or replace view course_v_1 as
select name from course where name = 'MySQL';
查看创建视图语句:show create view 视图名称;
查看视图数据:select * from 视图名称;
show create view course_v_1;
select * from course_v_1;//视图查询也可以加上where条件
方式一:create or replace view 视图名称 as select 语句;
方式二:alter view 视图名称 as select 语句;
create or replace view course_v_1 as
select id,name from course where name = 'MySQL';
alter view course_v_1 as
select id,name from course where name = 'MySQL';
drop view [if exist]视图名称;
drop view if exists course_v_1;
insert into 视图名字 values (字段值,字段值);
create or replace view 视图名称 as select 语句 with cascaded/local check option;
create or replace view course_v_1 as
select id,name from course where id <=20
with cascaded check option;
-- 创建视图
create or replace view course_v_1 as
select id,name from course where id <=20;
create or replace view course_v_2 as
select id,name from course_v_1 where id >=10
with cascaded check option;
-- 插入数据
insert into course_v_2 values (6,'Airflow');
insert into course_v_2 values (25,'GO');
insert into course_v_2 values (15,'VB');
-- 创建视图
create or replace view course_v_1 as
select id,name from course where id <=20;
create or replace view course_v_2 as
select id,name from course_v_1 where id >=10
with cascaded check option;
create or replace view course_v_3 as
select id,name from course_v_2 where id <=15;
insert into course_v_3 values (11,'Python');
insert into course_v_3 values (17,'Python');
insert into course_v_3 values (28,'Python');
create or replace view course_v_count as
select COUNT(*) from course;
-- 执行失败,报错:不可执行插入操作
insert into course_v_count values(11);
[HY000][1471] The target table course_v_count of the INSERT is not insertable-into
create or replace view tb_user_view as
select
id,name,profession,age,gender,status,createtime
from
tb_user;
-- 后续直接查询视图
select * from tb_user_view;
create view tb_stu_course as
select
s.name studentname,s.no student_no,c.name course_name
from
student s ,student_course sc,course c
when
s.id = sc.studentid
and
sc.courseid = c.id;
-- 后续直接查询视图
select * from tb_stu_course;
create procedure 存储过程名称([参数列表])
begin
-- SQL语句
end;
create procedure p1()
begin
select count(*) from tb_user;
end;
-- 因为命令行中见到分号默认结束,因此先定义结束符
mysql> delimiter $$
mysql> create procedure p1()
mysql> begin
mysql> select count(*) from tb_user;
mysql> end
mysql>$$
call 名称([参数]);
call p1();
select * from information_schema.ROUTINES
where ROUTINE_SCHEMA = 'itcast';
-- information_schema是库,routines是表,routine_schema是字段
select * from information_schema.routines where routine_schema = 'xxx';
show create procedure p1;
drop procedure [if exist] 存储过程名称;
show [session/global] variables; --查看所有系统变量
show [session/global] variables like '......' -- 可以通过like模糊匹配方式查找变量
select @@[session/global] 系统变量名 -- 查看指定变量的值
show session variables;
show session variables like 'auto%';
select @@session.autocommit;
set [session/global] 系统变量名 = 值;
set @@[session/global] 系统变量名 = 值;
set session autocommit = 0;
set @@session.autocommit = 0;
set @@global.autocommit = 0;
select @@global.autocommit;
set @变量名 = 值;
set @变量名 := 值;
select @变量名 := 值;
select 字段名 into @变量名 from 表名;
set @myname = '芬芬';
set @myage := '18';
set @mygender := '女',@myhobby:='大数据';
select @mycolor := 'red';
select COUNT(*) into @mycount from tb_user;
select @变量名;
select @myname,@myage,@mygender,@myhobby;
select @mycolor,@mycount;
-- 就算没赋值,也可以查,就是0而已
select @abc;
declare 变量名 变量类型[default...];
变量类型有:int,bigint,char,varchar,date,time等
set 变量名 = 值;
set @变量名 := 值;
select @变量名 := 值;
-- 声明
-- 赋值
create procedure p2()
begin
declare course_count int default 0;
set course_count :=100;
select course_count;
end;
-- 调用
call p2();
if 条件1 then
...
elseif 条件2 then
...
else
...
end if;
create procedure p3 ()
begin
declare score int default 58;
declare result varchar(10);
if score >= 85 then
set result := '优秀';
elseif score >= 60 then
set result := '及格';
else
set result := '不及格';
end if;
select result;
end;
call p3();
见下章5.5 参数的传参写法
create procedure 存储构成名称(in/out/inout 参数名 参数类型)
begin
--sql语句
end;
create procedure p4 (in score int,out result varchar(10))
begin
-- declare score int default 58;
-- declare result varchar(10);
if score >= 85 then
set result := '优秀';
elseif score >= 60 then
set result := '及格';
else
set result := '不及格';
end if;
-- select result;
end;
-- 第二个参数需要一个变量来接收,然后打印出来
call p4(68,@result);
select @result;
将传入的200分制的分数,进行换算,换算成百分制,然后返回分数
分析:分数即使传入值,也是传出值,用inout
create procedure p5 (inout score int)
begin
set score := score * 0.5;
end;
set @score := 178; -- 初始化
call p5(@score); -- 调用
select @score; -- 打印
case 字段
when 条件 then 程序
when 条件 then 程序
else 程序
end case;
create procedure p6(in month int)
begin
declare result varchar(10);
case
when month >=1 and month <=3 then
set result = '第一季度';
when month >=4 and month <=6 then
set result = '第二季度';
when month >=7 and month <=9 then
set result = '第三季度';
when month >=10 and month <=12 then
set result = '第四季度';
else
set result:= '非法参数';
end case;
select concat('你输入的月份为',month,'所属的季度为',result);
end;
call p6(4);
while 条件 do
SQL逻辑
end while;
create procedure p7 (in n int)
begin
declare total int default 0;
while n > 0 do
set total:= total + n;
set n := n - 1;
end while;
select total ;
end;
call p7(10);
repeat
SQL逻辑
until 条件
end repeat;
create procedure p8(in n int)
begin
declare total int default 0;
repeat
set total:= total+n;
set n:=n-1;
until n<=0
end repeat;
select total;
end;
call p8(10);
call p8(100);
begin_label :LOOP
SQL逻辑
END LOOP end_label;
create procedure p9(in n int)
begin
declare total int default 0;
sum:loop
if n<=0 then
leave sum;
end if;
set total:=total + n;
set n := n-1;
end loop sum;
select total;
end;
call p9(10);
call p9(100);
create procedure p10(in n int)
begin
declare total int default 0;
sum:loop
if n<=0 then
leave sum;
end if;
if n%2 = 1 then
set n := n-1; -- 这部是因为奇数的时候回跳过下面的n-1,因此上面也减一次
iterate sum;
end if;
set total:=total + n;
set n := n-1;
end loop sum;
select total;
end;
call p10(10);
call p10(100);
declare 游标名 cursor for 查询语言;
open 游标名;
fetch 游标名 into 变量;
close 游标名;
create procedure p11(in uage int)
begin
-- 声明两个变量,方便后续赋值,普通变量声明要在声明游标之前
declare uname varchar(100);
declare upro varchar(100);
-- 声明游标
declare u_cursor cursor for select name,profession from tb_user where age <= uage; -- 逻辑写这边
-- 创建一张表
drop table if exists tb_user_pro;
create table if not exists tb_user_pro(
id int primary key auto_increment,
name varchar(100),
profession varchar(100)
);
-- 打开游标
open u_cursor;
-- 获取游标记录
-- 循环遍历集合并赋值
while true do
fetch u_cursor into uname,upro;
-- 插入表结构
insert into tb_user_pro values(null,uname,upro);-- null是自增的id,不用慌
end while;
-- 关闭游标
close u_cursor;
end;
declare handler_action HANDLER FOR condition_value [condition_value]...statment;
handler_action:
continue:继续执行当前程序
exit:终止执行当前程序
condition_value:
如下图一部分,如有兴趣去MySQL官方文档那边看下各个状态码的用途
create procedure p11(in uage int)
begin
-- 声明两个变量,方便后续赋值,普通变量声明要在声明游标之前
declare uname varchar(100);
declare upro varchar(100);
-- 声明游标
declare u_cursor cursor for select name,profession from tb_user where age <= uage;
-- 声明handler
declare exit handler for SQLSTATE '02000' close u_cursor;
-- 也可以 declare exit handler for not found close u_cursor;表示处理02开头的状态码
-- 创建一张表
drop table if exists tb_user_pro;
create table if not exists tb_user_pro(
id int primary key auto_increment,
name varchar(100),
profession varchar(100)
);
-- 打开游标
open u_cursor;
-- 获取游标记录
-- 循环遍历集合并赋值
while true do
fetch u_cursor into uname,upro;
-- 插入表结构
insert into tb_user_pro values(null,uname,upro);
end while;
-- 关闭游标
close u_cursor;
end;
call p11(40);
create function 存储函数名称[参数列表]
returns type [参数]
begin
--SQL语句
return...;
end;
参数:
deterministic:相同的输入参数总是产生相同的结果
no sql:不包含SQL
read sql data:包含读取数据的语句,但不包含
create function fun1(n int )
returns int deterministic
begin
declare total int default 0;
while n>0 do
set total:=total+n;
set n := n-1;
end while;
return total;
end;
select fun1(100);
create trigger trigger_name
before/after/ insert/update/delete --指定类型触发器
on tbl_name for each row --行级触发器
begin
trigger_stmt;
end
show triggers;
drop trigger trigger_name;
create table user_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;
-- 插入数据时的触发器
create trigger tb_user_insert_trigger
after insert on tb_user for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params) values
(null,'insert',now(),new.id,-- null 表示默认id自增
concat('插入数据的内容为:id=',NEW.id,',name=',new.name,',phone=',new.phone,',email=',new.email,',profession=',new.profession));
end;
show triggers ;
insert into tb_user(id, name, phone, email, profession, age, gender, status, createtime)
VALUES (25,'二皇子','18809091212','erhuangzi@163.com','软件工程',23,'1','1',now());
-- 和insert触发器不同的点是:
create trigger tb_user_update_trigger
after update on tb_user for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params) values
(null,'update',now(),new.id,-- null 表示默认id自增
concat('更新之前的数据:id=',OLD.id,',name=',OLD.name,',phone=',OLD.phone,',email=',OLD.email,',profession=',OLD.profession,
'|更新之后的数据:id=',NEW.id,',name=',new.name,',phone=',new.phone,',email=',new.email,',profession=',new.profession));
end;
show triggers ;
update tb_user set age = 20 where id = 23;
update tb_user set age = 20 where id <= 5; -- 因为是行级触发器,所以回触发5次
-- 删除数据时的触发器
create trigger tb_user_delete_trigger
after delete on tb_user for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params) values
(null,'delete',now(),OLD.id,-- null 表示默认id自增
concat('更新之前的数据:id=',OLD.id,',name=',OLD.name,',phone=',OLD.phone,',email=',OLD.email,',profession=',OLD.profession));
end;
show triggers ;
delete from tb_user where id = 25;
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
我正在编写一个简单的静态Rack应用程序。查看下面的config.ru代码:useRack::Static,:urls=>["/elements","/img","/pages","/users","/css","/js"],:root=>"archive"map'/'dorunProc.new{|env|[200,{'Content-Type'=>'text/html','Cache-Control'=>'public,max-age=6400'},File.open('archive/splash.html',File::RDONLY)]}endmap'/pages/search.
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
需求:要创建虚拟机,就需要给他提供一个虚拟的磁盘,我们就在/opt目录下创建一个10G大小的raw格式的虚拟磁盘CentOS-7-x86_64.raw命令格式:qemu-imgcreate-f磁盘格式磁盘名称磁盘大小qemu-imgcreate-f磁盘格式-o?1.创建磁盘qemu-imgcreate-fraw/opt/CentOS-7-x86_64.raw10G执行效果#ls/opt/CentOS-7-x86_64.raw2.安装虚拟机使用virt-install命令,基于我们提供的系统镜像和虚拟磁盘来创建一个虚拟机,另外在创建虚拟机之前,提前打开vnc客户端,在创建虚拟机的时候,通过vnc
我去了这个website查看Rails5.0.0和Rails5.1.1之间的区别为什么5.1.1不再包含:config/initializers/session_store.rb?谢谢 最佳答案 这是删除它的提交:Setupdefaultsessionstoreinternally,nolongerthroughanapplicationinitializer总而言之,新应用没有该初始化器,session存储默认设置为cookie存储。即与在该初始值设定项的生成版本中指定的值相同。 关于
我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin
我正在关注Hartl的railstutorial.org并已到达11.4.4:Imageuploadinproduction.我做了什么:注册亚马逊网络服务在AmazonIdentityandAccessManagement中,我创建了一个用户。用户创建成功。在AmazonS3中,我创建了一个新存储桶。设置新存储桶的权限:权限:本教程指示“授予上一步创建的用户读写权限”。但是,在存储桶的“权限”下,未提及新用户名。我只能在每个人、经过身份验证的用户、日志传送、我和亚马逊似乎根据我的名字+数字创建的用户名之间进行选择。我已经通过选择经过身份验证的用户并选中了上传/删除和查看权限的框(而不
我正在使用mechanize登录网站,然后检索页面。我遇到了一些问题,我怀疑这是由于cookie中的某些值造成的。当Mechanize登录网站时,我假设它存储了cookie。如何通过Mechanize打印出存储在cookie中的所有数据? 最佳答案 代理有一个cookie方法。agent=Mechanize.newpage=agent.get("http://www.google.com/")agent.cookiesagent.cookies.to_scookie返回一个Mechanize::Cookiesobject
我以为它们存储在cookie中-但不,检查cookie没有任何结果。session也不存储它们。那么,我在哪里可以找到它们?我需要这个来直接设置它们(而不是通过flashhash)。 最佳答案 它们存储在inyoursessionstore.自rails2.0以来的默认设置是cookie存储,但请检查config/initializers/session_store.rb以检查您是否使用默认设置以外的东西。 关于ruby-on-rails-闪存消息存储在哪里?,我们在StackOverf