标签:des comm 包含 font eal enum 命名 sts 实现
1.描述表得结构
desc table_name;
desc table_name;
2.删除表中得数据
delete from table_name where [约束条件]
delete from table_name where [约束条件]
3.可变浮点数定义用decimal(n,m)
n :是浮点数的(二进制)位数
m:是小数分的位数
eg:decimal(10,2)
n :是浮点数的(二进制)位数
m:是小数分的位数
eg:decimal(10,2)
4.标准的存储过程模板如下:
drop procedure if exists pro_1;
delimiter $
create procedure pro_1(
)
begin
end $
delimiter ;
drop procedure if exists pro_1;
delimiter $
create procedure pro_1(
)
begin
end $
delimiter ;
set @var_name = 0,@var_id = 12;
select @var_name , @var_id;
set @var_name = 0,@var_id = 12;
select @var_name , @var_id;
select username,
case username
when ‘belong‘ then ‘handsome‘
when ‘tom‘ then ‘sou‘
else ‘ban‘
end
from user;
select username,
case username
when ‘belong‘ then ‘handsome‘
when ‘tom‘ then ‘sou‘
else ‘ban‘
end
from user;
红色部分就是查询的第二个字段
select * ,if(id>4,‘男‘,‘女‘) sex from user;
select * ,if(id>4,‘男‘,‘女‘) sex from user;
ifnull(expr1,expr2):如果expr1 为空 返回 expr2 否则返回expr1
set @a:=null;
select ifnull(@a,2);
set @a:=10;
select ifnull(@a,19);
ifnull(expr1,expr2):如果expr1 为空 返回 expr2 否则返回expr1
set @a:=null;
select ifnull(@a,2);
set @a:=10;
select ifnull(@a,19);
如果expr1 = expr2 成立,那么返回值为NULL,否则返回值为expr1这和Case when expr1 = expr2 then NULL ELSE expr1 END相同
eg:
select NULLIF(1,1)
如果expr1 = expr2 成立,那么返回值为NULL,否则返回值为expr1这和Case when expr1 = expr2 then NULL ELSE expr1 END相同
eg:
select NULLIF(1,1)
<7>.存储过程与函数区别
<8>.分支语句if…then…else ……end if;
DELIMITER $
create procedure pro_1()
begin
declare var int ;
set var = parameter +1;
if var=0 then
select * from user where id = 1058;
end if;
if parameter = 0 then
select * from user where id = 1060;
else
select * from user where id = 1059;
end if;
end $
DELIMITER ;
DELIMITER $
create procedure pro_1()
begin
declare var int ;
set var = parameter +1;
if var=0 then
select * from user where id = 1058;
end if;
if parameter = 0 then
select * from user where id = 1060;
else
select * from user where id = 1059;
end if;
end $
DELIMITER ;
DELIMITER $
create procedure pro_2()
begin
declare var int;
set var = 0;
while var<6 do
select var;
set var = var +1;
end while;
end $
DELIMITER ;
DELIMITER $
create procedure pro_2()
begin
declare var int;
set var = 0;
while var<6 do
select var;
set var = var +1;
end while;
end $
DELIMITER ;
DELIMITER $
create procedure pro_3()
begin
declare v int;
set v:=0;
repeat
select v;
set v = v+1;
until v>-5
end repeat;
end $
DELIMITER ;
DELIMITER $
create procedure pro_3()
begin
declare v int;
set v:=0;
repeat
select v;
set v = v+1;
until v>-5
end repeat;
end $
DELIMITER ;
DELIMITER $
create procedure pro_4()
begin
declare v int;
set v:=0;
LOOP_Lable:loop
select v;
set v = v+1;
if v >= 5 then
leave LOOP_Lable;#离开循环
end if;
end loop;
end $
DELIMITER ;
DELIMITER $
create procedure pro_4()
begin
declare v int;
set v:=0;
LOOP_Lable:loop
select v;
set v = v+1;
if v >= 5 then
leave LOOP_Lable;#离开循环
end if;
end loop;
end $
DELIMITER ;
例:
set @v_sql= sqltext;
PREPARE stmt FROM @v_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
例:
set @v_sql= sqltext;
PREPARE stmt FROM @v_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
drop function if exists fun_1;
delimiter $
create function fun_1()returns int
begin
代码块
return 1;
end $
delimiter ;
drop function if exists fun_1;
delimiter $
create function fun_1()returns int
begin
代码块
return 1;
end $
delimiter ;
8.Mysql存储过程和函数区别介绍
9.使用存储过程与函数的原则:
10.更新数据
update movies set views = views+1 where Vid = 5;
update movies set views = views+1 where Vid = 5;
11.concat()函数中间的参数可以链接无数个,用逗号隔开
12.存储过程中也可以向高级语言一样可以定义准备语句,用于执行SQL语句 select insert ...等等
13.在存储过程中要想得到select 的查询结果就一定要用准备语句,才可以把结果在存储过程外得到返回值
prepare var_name from sql_str;#sql_str是select的字符串
execute var_name;
prepare var_name from sql_str;#sql_str是select的字符串
execute var_name;
15.mysql 导出数据库
mysqldump -u username -p db_name > 导出的数据库文件
mysqldump -u username -p db_name > 导出的数据库文件
mysqldump -u username -p --database dbname1 dbname2 > backup.sql;
mysqldump -u username -p --database dbname1 dbname2 > backup.sql;
mysqldump -u username -p --all-database > backup.sql;
x
mysqldump -u username -p --all-database > backup.sql;
mysql -u root -p [dbname] <backup.sql
eg:mysql -u root -p < backup.sql;
mysql -u root -p [dbname] <backup.sql
eg:mysql -u root -p < backup.sql;
mysql -u root -p db_name --one-database < backup.sql;
mysql -u root -p db_name --one-database < backup.sql;
16.mysql把用户的信息都存到了名为mysql的数据的数据库里来管理用户
select user from user;//查看所有用户
select user from user;//查看所有用户
show grants for user_name;
show grants for user_name;
18.为用户分配所有权限
grant all on *.* to username;
grant all on *.* to username;
19.declare continue handler for sqlstate ‘02000‘ set done = 1;
20.查看表的属性结构
<1>.desc <table_name>;
<2>.show columns from <table_name>
<1>.desc <table_name>;
<2>.show columns from <table_name>
alter table <table_name> drop column <column_name>;
alter table <table_name> drop column <column_name>;
alter table user change `password varchar` password varchar(20) character set utf8 not null
alter table user change `password varchar` password varchar(20) character set utf8 not null
alter table table_name change column_name rename_column_name column_type;
alter table table_name change column_name rename_column_name column_type;
rename database music to audio;
rename database music to audio;
alter table video_type_config ADD
CONSTRAINT pk_Video_no PRIMARY KEY (video_no);
alter table video_type_config ADD
CONSTRAINT pk_Video_no PRIMARY KEY (video_no);
表注释
ALTER TABLE table_name COMMENT=‘这是表的注释‘;
字段注释
ALTER table table_name
MODIFY `column_name` datetime DEFAULT NULL COMMENT ‘这是字段的注释‘
表注释
ALTER TABLE table_name COMMENT=‘这是表的注释‘;
字段注释
ALTER table table_name
MODIFY `column_name` datetime DEFAULT NULL COMMENT ‘这是字段的注释‘
SHOW FULL COLUMNS FROM video_type_config;
SHOW FULL COLUMNS FROM video_type_config;
#schema表示的是数据库(模式下有多个数据库schema:db=1:n)
-- use information_schema;
#table_schema表示的是数据库
SELECT TABLE_NAME, DATA_LENGTH + INDEX_LENGTH, TABLE_ROWS
FROM TABLES
WHERE TABLE_SCHEMA = ‘video‘
AND TABLE_NAME = ‘video_type_config‘;
#schema表示的是数据库(模式下有多个数据库schema:db=1:n)
-- use information_schema;
#table_schema表示的是数据库
SELECT TABLE_NAME, DATA_LENGTH + INDEX_LENGTH, TABLE_ROWS
FROM TABLES
WHERE TABLE_SCHEMA = ‘video‘
AND TABLE_NAME = ‘video_type_config‘;
select @@global.sql_mode
select @@global.sql_mode
set @@global.sql_mode =‘STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION‘;
set @@global.sql_mode =‘STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION‘;
show table status like ‘video_type_config‘;
show table status like ‘video_type_config‘;
show table status like ‘video_type_config‘ \G;
mysql> show table status like ‘video_type_config‘ \G;
*************************** 1. row ***************************
Name: video_type_config
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 156
Avg_row_length: 105
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2017-04-28 09:29:48
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment: 地址和类型配置表
1 row in set (0.01 sec)
show table status like ‘video_type_config‘ \G;
mysql> show table status like ‘video_type_config‘ \G;
*************************** 1. row ***************************
Name: video_type_config
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 156
Avg_row_length: 105
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2017-04-28 09:29:48
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment: 地址和类型配置表
1 row in set (0.01 sec)
SELECT
CONCAT(
SUM(CHAR_LENGTH(video_no)) / 1024,
‘Kb‘
) video_no,
CONCAT(
SUM(CHAR_LENGTH(video_type)) / 1024,
‘Kb‘
) video_type
FROM
video_type_config
ORDER BY
video_no,
video_type;
SELECT
CONCAT(
SUM(CHAR_LENGTH(video_no)) / 1024,
‘Kb‘
) video_no,
CONCAT(
SUM(CHAR_LENGTH(video_type)) / 1024,
‘Kb‘
) video_type
FROM
video_type_config
ORDER BY
video_no,
video_type;
在mysql中,每个数据库最多可创建20亿个表,一个表允许定义1024列,
在mysql中,每个数据库最多可创建20亿个表,一个表允许定义1024列,
show status like ‘Handler_read%‘;
大家可以注意:
handler_read_key:这个值越高越好,越高表示使用索引查询到的次数
handler_read_rnd_next:这个值越高,说明查询低效
show status like ‘Handler_read%‘;
大家可以注意:
handler_read_key:这个值越高越好,越高表示使用索引查询到的次数
handler_read_rnd_next:这个值越高,说明查询低效
ERROR: ASCII ‘\0‘ appeared in the statement, but this is not allowed unless
ERROR ASCII ‘\0‘ appeared in the statement, but this is not allowed unless
标签:des comm 包含 font eal enum 命名 sts 实现
原文地址:http://www.cnblogs.com/Mrbelong/p/7634675.html