一、编码
学习链接:https://www.cnblogs.com/beiyeren/p/3835412.html
1、编辑/etc/my.cnf改变编码
#在[mysqld]下添加
default-character-set=utf8
#在[client]下添加
default-character-set=utf8
(1)mysqld 下的default_character_set=utf8;,这个需要吗?答案是不需要。查看refman-5.1。
它的作用是:
当启动mysqld时,根据使用的初始选项设置来确定服务器字符集和 校对规则。可以使用--default-character-set设置字符集;
如果在CREATE TABLE语句中没有指定表字符集和校对规则,则使用数据库字符集和校对规则作为默认值。默认数据库的字符集和校对规则可以用作character_set_database和 collation_database系统变量。无论何时默认数据库更改了,服务器都设置这两个变量的值。如果没有 默认数据库,这两个变量与相应的服务器级别的变量(character_set_server和collation_server)具有相同的值。
大白话就是:如果你建表的时候没有指定字符编码,则会使用character_set_database,如果character_set_database也没有设值,则使用character_set_server
(2)client 下的default_character_set=utf8; 它是需要的,可是它的作用是干吗的?
它的作用等同执行以下3个命令
SET character_set_client = utf8
SET character_set_results = utf8;
SET character_set_connection = utf8;
系统变量character_set_client:用来指定解析客户端传递数据的编码
系统变量character_set_connection:用来指定数据库内部处理时使用的编码
系统变量character_set_results:用来指定数据返回给客户端的编码方式
2、登录MySQL去set操作
mysql> show variables like ‘charac%‘; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) mysql> set character_set_database=‘utf8‘; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> show variables like ‘char%‘; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) mysql> set character_set_server = ‘utf8‘; Query OK, 0 rows affected (0.00 sec)
二、 标识符
保留字即关键字有:
as-- 设置别名, like-- 模糊查询, where, [between,and/or-- 范围查询], in--, distinct-- 过滤重复数据, limit-- 限制查询结果的条数, order by-- 对查询结果排序, group by-- 分组查询, having-- 过滤分组, left/right join-- 外连接, inner join-- 内连接, cross join-- 交叉连接 union regexp
三、 注释
mysql> select 1+1; #1+1=? +-----+ | 1+1 | +-----+ | 2 | +-----+ 1 row in set (0.00 sec) mysql> select 1+1; -- 1+1=? +-----+ | 1+1 | +-----+ | 2 | +-----+ 1 row in set (0.00 sec) 注意‘-- ‘(双破折号)注释风格要求第2个破折号后面至少跟一个空格符(例如空格、tab、换行符等等)。 mysql> select 1+1; /* 1+1=? */ +-----+ | 1+1 | +-----+ | 2 | +-----+ 1 row in set (0.00 sec)
四、 变量
1、 定义变量
2、 全局变量和局部变量
3、 数据类型
五、运算符
1、 算术运算符:+ - * /或DIV %或MOD
在除法运算和模运算中,如果除数为0,将是非法除数,返回结果为NULL。
mysql> select 1+3; +-----+ | 1+3 | +-----+ | 4 | +-----+ 1 row in set (0.00 sec)
2、 比较运算符:=,<>,!=,>,>=,<,<=,between,not between,in,not in,<=>,like,REGEXP或RLIKE,is null,is not null
3、 逻辑运算符: not, !, and, or, xor,
4、 位运算符:%, |, ^, !, >>, <<,
六、SQL语句
insert into: insert into table_name(column_name1,column_name1,...) values(‘char‘,4,...) insert into table_name select * from table_name1 #把table_name1表所有数据复写到table_name里 delete: delete from table_name where column_name=value; delete from table_name; update: update table_name set column_name1=value1,column_name2=value2 where column_name=value; select: select column_name1,column_name2 from table_name where column_name=value; select * into table_name from table_name1; #把table_name1表所有数据复写到table_name里 select column_name into table_name from table_name1; #把table_name1表某列数据复写到table_name里 select 1+1; #进行运算符运算 select verison(); select @variable_name:=,@variable_name1:=value1; mysql> select @shuoshi=2; +------------+ | @shuoshi=2 | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> select @shuoshi; +----------+ | @shuoshi | +----------+ | NULL | +----------+ 1 row in set (0.00 sec) mysql> select @boshi; +--------+ | @boshi | +--------+ | NULL | +--------+ 1 row in set (0.00 sec) ################################################################################ create: create database database_name; create table table_name(column_name,column_name1,column_name2,...); create [unique] index index_name on table_name(column_name); create view view_name as select column_name1 from table_name where column_name=value; create user user_name identified by ‘password‘; drop: drop database database_name; drop table table_name; drop index index_name; drop view view_name; drop user ‘user_name‘@‘host_name‘; truncate: truncate table table_name; #清空table_name表记录 alter: alter table table_name add [column] column_name data_type; alter table table_name drop [column] column_name data_type; alter table table_name modify [column] column_name new_data_type; alter table table_name change [column] column_name new_column_name new_data_type; alter table table_name rename new_table_name ############################################################################### grant: grant select,insert on *.* to ‘user_name‘@‘host_name‘; revoke: revoke select,insert on *.* from ‘user_name‘@‘host_name‘; ############################################################################### set Transaction,commit,rollback,save ############################################################################### show: show variables like ‘char%‘; show databases; show tables; show tables from database_name; show status; show table status; show table status from database_name; show columns from table_name; show index from table_name; show grants for ‘user_name‘@‘host_name‘; set: set password = ‘2wsx#EDC‘; set character_set_server = ‘utf8‘; set @variable_name = value,@variable_name1 = value1; #变量分为局部变量和全局变量,局部变量用@来标识,全局变量用@@来标识(常用的全局变量一般都是已经定义好的)。 #一个变量名可以由当前字符集的数字字母字符和“_”、“$”和“.”组成。 #对于SET,可以使用=或:=来赋值。 #对于SELECT只能使用:=来赋值,没有赋值的话就是默认null值。 #用户变量为session级别,当我们关闭客户端或退出登录时用户变量全部消失。 #如果想用就保存自定义的变量,需要自行创建一个表,将标量insert到表里。 mysql> set @xiaoxue = 6; Query OK, 0 rows affected (0.00 sec) mysql> set @xiaoxue := 6; Query OK, 0 rows affected (0.00 sec) mysql> select @xiaoxue; +----------+ | @xiaoxue | +----------+ | 6 | +----------+ 1 row in set (0.00 sec) mysql> set @xiaoxue := 6,@zhongxue=3; Query OK, 0 rows affected (0.00 sec) mysql> select @daxue:=3; +-----------+ | @daxue:=3 | +-----------+ | 3 | +-----------+ 1 row in set (0.00 sec) mysql> select @daxue + @zhongxue; +--------------------+ | @daxue + @zhongxue | +--------------------+ | 6 | +--------------------+ 1 row in set (0.00 sec) mysql> do sleep(2); Query OK, 0 rows affected (2.00 sec) mysql> do (select @daxue + @zhongxue); Query OK, 0 rows affected (0.00 sec) #do语句的使用:结果集对你并不重要;你对时间比较敏感,也就是说你想执行的更快一点。