)
显示database的字符集及校验规则:
mysql> show variables like 'character_set_database';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| character_set_database | gb2312 |
+------------------------+--------+
1 row in set (0.08 sec)
mysql> show variables like 'collation_database';
+--------------------+-------------------+
| Variable_name | Value |
+--------------------+-------------------+
| collation_database | gb2312_chinese_ci |
+--------------------+-------------------+
1 row in set (0.00 sec)
字符集的级别:服务器级别,数据库级别,表级别,字段级别.
设置数据库字符集的基本规则:
a.如果指定了字符集和校验规则,则使用指定的;
b.如果指定了字符集但没有指定校验规则,则使用字符集默认的校验规则;
c.如果指定了校验规则但未指定字符集,则使用与该校验规则绑定的字符集;
d.如果没有指定字符集和校验规则,则使用服务器的字符集和校验规则.
表的字符集设定 same with the above:
mysql> show create table z1 \G;
*************************** 1. row ***************************
Table: z1
Create Table: CREATE TABLE `z1` (
`id` varchar(11) character set gb2312 default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.23 sec)
修改z1表的字符集:
mysql> alter table z1 character set gbk;
Query OK, 0 rows affected (1.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table z1 \G;
*************************** 1. row ***************************
Table: z1
Create Table: CREATE TABLE `z1` (
`id` varchar(11) character set gb2312 default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
mysql> insert into z1 values('有钱先生就是我');
Query OK, 1 row affected (0.09 sec)
mysql> select * from z1;
+----------------+
| id |
+----------------+
| 有钱先生就是我 |
+----------------+
1 row in set (0.03 sec)
中文问题就解决了.
二.索引的一点小知识:
项目中有时候会有些select查询特别慢,后来大牛加了个索引,速度超快,用的就是索引,
mysql的存储引擎MyISAM和InnoDB默认使用的都是BTREE索引.
create index x on table y(A,B);
在y表,A,B字段建立x索引
a.最适合索引的列是where子句中的列,而不是select中列;
b.使用唯一索引,列的基数越大,索引的效果越好;
c.索引会占用额外的磁盘空间,莫滥用,否则会降低写操作的性能.
对于使用=或<=>操作符的比较,hash索引会比btree索引快
对于>,<,>=,<=,!=,between,like 'pattern'的操作,则btree索引要好些.
三.存储过程及自定义函数(面试必备):
1.delimiter一个函数或sp终结符;
2.create一个function或sp;
3.将终结符替换成mysql使用的';';
4.直接select或call调用.
mysql> delimiter $$
mysql> CREATE FUNCTION myFunction2
-> (
-> in_string VARCHAR(255),
-> in_find_str VARCHAR(20),
-> in_repl_str VARCHAR(20)
-> )
-> RETURNS VARCHAR(255)
-> BEGIN
-> DECLARE l_new_string VARCHAR(255);
-> DECLARE l_find_pos INT;
-> SET l_find_pos=INSTR(in_string,in_find_str);
->
-> IF (l_find_pos>0) THEN
-> SET l_new_string=INSERT(in_string,l_find_pos,LENGTH(in_find_s
tr),in_repl_str);
-> ELSE
-> SET l_new_string=in_string;
-> END IF;
-> RETURN(l_new_string);
-> END $$
Query OK, 0 rows affected (0.06 sec)
mysql> delimiter ;
mysql> select myFunction2('ABC','A','Z');
+----------------------------+
| myFunction2('ABC','A','Z') |
+----------------------------+
| ZBC |
+----------------------------+
1 row in set (0.00 sec)
另一个demo:
mysql> delimiter $$
mysql> CREATE FUNCTION myFunction3(
-> in_title VARCHAR(4),
-> in_gender CHAR(1),
-> in_firstname VARCHAR(20),
-> in_middle_initial CHAR(1),
-> in_surname VARCHAR(20))
->
-> RETURNS VARCHAR(60)
-> BEGIN
-> DECLARE l_title VARCHAR(4);
-> DECLARE l_name_string VARCHAR(60);
->
-> IF ISNULL(in_title) THEN
-> IF in_gender='M' THEN
-> SET l_title="Mr";
-> ELSE
-> SET l_title="Ms";
-> END IF;
-> END IF;
->
-> IF ISNULL(in_middle_initial) THEN
-> SET l_name_string=CONCAT(l_title,' ',in_firstname,' ',in_surnam
e);
-> ELSE
-> SET l_name_string=CONCAT(l_title,' ',in_firstname,' ',
-> in_middle_initial,' ',in