数据库操作命令在IDEA工具database的console命令
数据库表结构与视图
select @@transaction_isolation ;
set session transaction isolation level read committed ;
set session transaction isolation level REPEATABLE READ ; start transaction ; select * from sys_user; commit ; rollback ;
show global status like 'Com_______' ;
show variables like 'slow_query_log' ;
create or replace view v_users as select id, username from sys_user where id < 10 ; explain select id, username from sys_user where username < 10 ; explain select * from v_users where id = 2 ;
show create view v_users; drop view if exists v_users;
create or replace view v_users as select id, username from sys_user where id < 10 with cascaded check option ;
insert into v_users values ( 3 , 'wangliu' ) ;
insert into v_users values ( 30 , 'wangliu' ) ;
CREATE PROCEDURE PRO_USERS( )
BEGIN SELECT COUNT ( * ) FROM sys_user;
end ;
CALL PRO_USERS( ) ;
select * from information_schema. ROUTINES where ROUTINE_SCHEMA = 'test' ;
show create procedure PRO_USERS;
drop procedure if exists PRO_USERS;
show session variables ;
show session variables like 'auto%' ; select @@autocommit ; select @@session.autocommit ;
select @@global.autocommit ;
set @@autocommit = 1 ;
set @myName := 'zxd' ;
set @myAge := 30 ; set @myName := 'lisi' , @myAge := 40 ; select @myName , @myAge ; select count ( * ) into @myCount from sys_user; select @myCount ;
drop procedure if exists pro_test2;
create procedure pro_test2( )
begin declare var_age int default 0 ; set var_age := 20 ; select var_age;
end ; show create procedure pro_test2; call pro_test2( ) ;
create procedure pro_p3( )
begin declare score int default 58 ; declare res varchar ( 10 ) ; if score > 80 then set res:= '优秀' ; elseif score > 60 then set res := '及格' ; else set res:= '不及格' ; end if ; select res;
end ; call pro_p3( ) ;
create procedure pro_p4( in score int , out res varchar ( 10 ) )
begin if score > 80 then set res:= '优秀' ; elseif score > 60 then set res := '及格' ; else set res:= '不及格' ; end if ; select res;
end ; call pro_p4( 18 , @res ) ;
select @res ;
drop procedure if exists p5; create procedure p5( inout score double )
begin set score := score* 0.5 ;
end ; set @score := 178.8 ;
call p5( @score ) ;
select @score ;
drop procedure p6;
create procedure p6( in month int , out res varchar ( 20 ) )
begin case when month >= 1 and month <= 3 then set res:= '第一季度' ; when month >= 4 and month <= 6 then set res:= '第二季度' ; when month >= 7 and month <= 9 then set res:= '第三季度' ; when month >= 10 and month <= 12 then set res:= '第四季度' ; else set res:= '非法参数' ; end case ; select concat( '您输入的月份 ' , month , ',所属季度为' , res) into res;
end ; call p6( 7 , @res ) ;
select @res ;
drop procedure p7;
create procedure p7( inout num int )
begin declare total int default 0 ; while num > 0 do set total:= total + num; set num := num - 1 ; end while ; set num := total;
end ; set @num := 10 ;
call p7( @num ) ;
select @num ;
drop procedure p8;
create procedure p8( inout num int )
begin declare total int default 0 ; repeat set total := total + num; set num:= num - 1 ; until num <= 0 end repeat ; set num:= total;
end ; set @num := - 10 ;
call p8( @num ) ;
select @num ;
drop procedure p9;
create procedure p9( in num int , out res int )
begin set res:= 0 ; sum:loop if num <= 0 then leave sum; end if ; set res := res + num; set num:= num - 1 ; end loop sum;
end ; call p9( - 10 , @res ) ;
select @res ;
drop procedure p10;
create procedure p10( in num int , out res int )
begin set res:= 0 ; sum:loop if num <= 0 then leave sum; end if ; if num% 2 = 1 then set num:= num - 1 ; iterate sum; end if ; set res := res + num; set num:= num - 1 ; end loop sum;
end ; call p10( 10 , @res ) ;
select @res ;
create table tb_user( id int auto_increment primary key , name varchar ( 20 ) , age int , phone int , email varchar ( 100 ) , zhuanye varchar ( 50 )
) ;
show create table tb_user;
desc tb_user;
drop table tb_user;
select * from tb_user;
insert into tb_user values ( null , '唐僧' , 80 , 1990618888 , 'tangsanzang@qq.com' , '玄奘法师' ) , ( null , '猪八戒' , 400 , 1990613333 , 'zhubajie@qq.com' , '净坛使者' ) ;
create procedure p11( in v_num int )
begin declare v_name varchar ( 20 ) ; declare v_age int default 0 ; declare pro_cur cursor for select name , age from tb_user where age > v_num; declare exit handler for not found close pro_cur; drop table if exists pro_tb_user; create table if not exists pro_tb_user( id int auto_increment primary key , name varchar ( 20 ) , age int ) ; open pro_cur; while true do fetch pro_cur into v_name, v_age; insert into pro_tb_user values ( null , v_name, v_age) ; end while ; close pro_cur;
end ;
drop procedure p11; call p11( 100 ) ; select * from pro_tb_user; create procedure p12( in v_num int )
begin declare v_name varchar ( 20 ) ; declare v_age int default 0 ; declare done int default 0 ; declare pro_cur cursor for select name , age from tb_user where age > v_num; declare continue handler for not found set done:= 1 ;
create table if not exists pro_tb_user( id int auto_increment primary key , name varchar ( 20 ) , age int ) ; open pro_cur; ext_loop :loop fetch pro_cur into v_name, v_age; if done = 1 then leave ext_loop; end if ; insert into pro_tb_user values ( null , v_name, v_age) ; end loop ; close pro_cur;
end ; call p12( 50 ) ;
lock tables test. tb_user read ; select * from tb_user;
alter table pro_tb_user add column java int ;
desc pro_tb_user;
alter table pro_tb_user drop column java; unlock tables ;
start transaction ;
select * from tb_user;
select object_type, object_schema, object_name, lock_type, lock_duration from performance_schema. metadata_locks t; commit ;