zoukankan      html  css  js  c++  java
  • 代码收藏系列--mysql--创建数据库、数据表、函数、存储过程命令

    创建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 '创建表
    2017-06-21';
    


    补充说明:
    1. MySQL数据库的varchar(M),M表示的是字符数量,而不是字节数量,占用的字节数和数据表使用的编码有关。(一个中文、英文、数字等都算一个字符)
    2. MySQL要求一个行的定义长度不能超过65535字节,因此varchar的长度理论上最大是65535字节,编码若为gbk,每个字符最多占2个字节,最大长度不能超过32766个字符;编码若为utf8,每个字符最多占3个字节,最大长度不能超过21845个字符,即不论字母、数字或汉字,只能存储21785个
    3. create_timestamp int not null default UNIX_TIMESTAMP() comment '创建时间(UNIX时间戳格式)' 这种语法不能通过,因为mysql默认值只能支持常量,变量是不被允许的,所有可以使用其他的方式,或者程序每次都插入时间戳来实现

    文章参考: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 ;

    查看系统信息命令

    -- 查询所有数据库
    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--创建数据库、数据表、函数、存储过程命令
    本文链接:http://www.cnblogs.com/sochishun/p/7061693.html
    本文作者:SoChishun (邮箱:14507247#qq.com | 博客:http://www.cnblogs.com/sochishun/)
    发表日期:2017年6月23日

  • 相关阅读:
    js Object.freeze()
    js Object.seal()
    js数字千分位,三种写法,,拿走。。。
    nodejs mongoose连接mongodb报错,command find requires authentication
    nodejs express 服务代理
    容器监控之cadvisor
    kubernetes 集群卸载清理
    解决目录太大-建立软链接
    与运算(&)、或运算(|)、异或运算(^)
    jpa中将查询的字段返回为Map键值对类型
  • 原文地址:https://www.cnblogs.com/sochishun/p/7061693.html
Copyright © 2011-2022 走看看