创建mysql数据库
CREATE DATABASE IF NOT EXISTS `database_name` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
创建mysql数据表
drop table if exists `table_name`;create table if not exists `table_name` ( id int auto_increment primary key comment '主键编号', `name` varchar(32) not null default '' comment '名称', `code` varchar(32) not null default '' comment '代码', category_id int not null default 0 comment '类别编号', create_time timestamp not null default CURRENT_TIMESTAMP comment '创建时间(年月日)', INDEX idx_name (`name`), -- 普通索引 INDEX idx_name_category_id (`code`,category_id), -- 复合索引 UNIQUE INDEX idxu_code (`code`) -- 唯一索引 -- 注意,最后一行不能有逗号) ENGINE=InnoDB DEFAULT CHARSET=utf8 comment '创建表\r\n2017-06-21';
例如:
1个中文用UTF8编码是3字节(Byte),用GBK编码是2字节(Byte)。1个英文或数字不管什么编码都是1字节(属于ASCII编码)。'中文' 2个汉字的长度是 3byte * 2 = 6byte'E文' 1个英文+1个汉字的长度是 1byte + 3byte = 4byte'a0' 1个英文+1个数字的长度是 1byte + 1byte = 2byteGBK的文字编码用双字节来表示,即不论中、英文字符均使用双字节来表示'中文' 2个汉字的长度是 2byte * 2 = 4byte'E文' 1个英文+1个汉字的长度是 1byte + 2byte = 3byte'a0' 1个英文+1个数字的长度是 1byte + 1byte = 2byte
创建mysql视图
create or replace view `view_name` as select * from `table_name`;
创建mysql存储过程
/*** mysql游标* @since 1.0 2015-3-28 sochishun Added.*/DELIMITER ;;drop procedure if exists proc_cursor_demo;;create procedure proc_cursor_demo()begin declare vint_id int; declare vstr_name varchar(32); declare done boolean default false; -- 计费游标 declare cur1 cursor for select `id`, `name` from `table_name` where id < 100; -- 将结束标志绑定到游标 declare continue handler for not found set done = true; open cur1; loop_label: loop fetch cur1 into vint_id, vstr_name; -- 声明结束的时候 if done then leave loop_label; end if; call proc_update_demo(vint_id, vstr_name); end loop; close cur1;end;;DELIMITER ;
创建mysql函数
/*** 创建mysql函数* @since 1.0 2016-2-18 by sochishun* @example SIP/301-00000155*/DELIMITER ;;drop function if exists fn_test_demo;;create function fn_test_demo(pstr_channel varchar(32))returns varchar(16) -- 函数返回定义写在这里begin declare vstr_prefix varchar(16); declare vstr_out varchar(16); set vstr_prefix=LEFT(pstr_channel,3); if vstr_prefix='SIP' then set vstr_out=SUBSTR(pstr_channel,5,POSITION('-' IN pstr_channel)-5); elseif vstr_prefix='Loc' then set vstr_out=SUBSTR(pstr_channel,7,POSITION('@' IN pstr_channel)-7); else set vstr_out=pstr_channel; end if; return vstr_out;end;;DELIMITER ;
查看系统信息命令
-- 查询所有数据库select SCHEMA_NAME,DEFAULT_CHARACTER_SET_NAME,DEFAULT_COLLATION_NAME from information_schema.schemata where SCHEMA_NAME not in ('mysql','information_schema','performance_schema');-- 查询数据库中所有表select TABLE_NAME, TABLE_TYPE, ENGINE, DATA_LENGTH, CREATE_TIME, TABLE_COLLATION, TABLE_COMMENT from information_schema.tables where TABLE_SCHEMA='db_test_v1' AND TABLE_TYPE='BASE TABLE';-- 查询存储过程和函数select ROUTINE_NAME from information_schema.routines WHERE ROUTINE_SCHEMA='db_test_v1' AND ROUTINE_TYPE='FUNCTION';select ROUTINE_NAME from information_schema.routines WHERE ROUTINE_SCHEMA='db_test_v1' AND ROUTINE_TYPE='PROCEDURE';-- 查询所有视图select TABLE_NAME from information_schema.views where TABLE_SCHEMA='db_test_v1';select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='db_test_v1' AND TABLE_TYPE='VIEW';-- 查询所有字段select COLUMN_NAME, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, EXTRA, COLUMN_COMMENT from information_schema.columns WHERE TABLE_SCHEMA='db_test_v1' AND TABLE_NAME='t_test';-- 查询索引和主键select * from information_schema.table_constraints where TABLE_SCHEMA='db_test_v1' AND TABLE_NAME='t_test';-- 查询数据库全局变量select VARIABLE_NAME, VARIABLE_VALUE FROM information_schema.global_variables;
版权声明:本文采用署名-非商业性使用-相同方式共享(CC BY-NC-SA 3.0 CN)国际进行许可,转载请注明作者及出处。 本文标题:代码收藏系列--mysql--创建数据库、数据表、函数、存储过程命令本文链接:本文作者:SoChishun (邮箱:14507247#qq.com | 博客:)发表日期:2017年6月23日 |