当前位置:Gxlcms > 数据库问题 > 代码收藏系列--mysql创建数据库、数据表、函数、存储过程命令

代码收藏系列--mysql创建数据库、数据表、函数、存储过程命令

时间:2021-07-01 10:21:17 帮助过:15人阅读

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 类别编号,
    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. MySQL数据库的varchar(M),M表示的是字符数量,而不是字节数量,占用的字节数和数据表使用的编码有关。(一个中文、英文、数字等都算一个字符)
2. MySQL要求一个行的定义长度不能超过65535字节,因此varchar的长度理论上最大是65535字节,编码若为gbk,每个字符最多占2个字节,最大长度不能超过32766个字符;编码若为utf8,每个字符最多占3个字节,最大长度不能超过21845个字符,即不论字母、数字或汉字,只能存储21785个

文章参考:http://www.cnblogs.com/sochishun/p/7026762.html

 

例如:
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 = 2byte
GBK的文字编码用双字节来表示,即不论中、英文字符均使用双字节来表示
‘中文‘ 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 ;

 

版权声明:本文采用署名-非商业性使用-相同方式共享(CC BY-NC-SA 3.0 CN)国际许可协议进行许可,转载请注明作者及出处。
本文标题:代码收藏系列--mysql--创建数据库、数据表、函数、存储过程命令
本文链接:http://www.cnblogs.com/sochishun/p/7061693.html
本文作者:SoChishun (邮箱:14507247#qq.com | 博客:http://www.cnblogs.com/sochishun/)
发表日期:2017年6月23日

代码收藏系列--mysql创建数据库、数据表、函数、存储过程命令

标签:href   null   ima   comm   iter   l数据库   base   handle   字符   

人气教程排行