標籤:href null ima comm iter l資料庫 base handle 字元
建立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 ‘類別編號‘, 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建立資料庫、資料表、函數、預存程序命令