- create database xx default charset utf8; 创建数据库
- create table xx (id int auto _increment, name char(10)) engine=innodb charset=utf8; 创建表默认的引擎不支持事务
- create table xx (id int not null auto_increment primary key);
- create table xx ( id int auto_increment primary key, name char(10), constraint 自己起的外键名字 foreign key (本表字段) references xx (外表字段) 创建外键
- show databases; 显示数据库
- use XX; 选中要操作的数据库
- show tables; 显示表结构
- show column form xx; desc xx; 显示表中所有字段
- delete from xx; 清空表,id按顺序接着来,所谓清空表,只是删除了表里的数据
- truncate table xx; 清空表,id重新来,快
- drop table xx; 删除表
- insert into xx (id,name) values (1,'xx'), (2,'xxx') 插入数据
- update xx set age=18 where .. 更新数据
- select xx,oo from xx;
- select distinct xx键 from xx; 去重
- select xx from xx limit 5; 只拿5条
- select xx from xx limit 5,5; 返回行5(第四行)开始的5行,行1 是第零行
- select xx from xx limit 4 offset 3; 从行3开始拿4条
- select date_format(oo, '%Y-%m') from xx; 时间格式化,oo字段形如2018-05-25-13:20:01,只拿年月
- select x,xx,xxx from xxoo where ... order by x desc ,xx; 先按x按照降序排列,x相同则按照b升序排列,注意放在where后面(默认是asc升序)
- select xx from xx where between xx and xo; 关键字between,取值范围,左右包含xx,xo
- selec xx from xx where xo is null; 按字面解释
- select xx,xxx from xx where .. and(or)..;
- select xx,xxx from xx where a or b and c; 注意,会优先解释and,然后才是or ,加个括号可以解决
- select xx from x where xx in (xo,ox); 相当于or
- select xx where like 'xx%'; %任意字符出现任意次
- select xx where regexp '1|2' ; 正则匹配,注意'' 还有注意mysql正则转义 \\,是两个, \\\匹配\
- * 0个或多个 + 1个或多个 ?0个或1个 {n} 指定数目 {n,m} 匹配范围
- select xx concat(xx, '(' , xx , ')' ) from xx; 字符串拼接
- selec concat(rtrim(xx)) from xx; 去除两端空格
- where ... group by .. order by group by必须在where 之后 ,order by 之前
- 如果在select中使用表达式,则必须在group by 子句中指定相同的表达式,不能使用别名
- decimal(4,5) 精准的储存小数,xxxx.xxxxx
- char(10) 速度快,10个位置储存,定长
- varchar(10) 变长,节省空间,比如存root字符串,虽然指定空间为10, 但实际只会占用4个空间
- 几种约束 非空约束(not null), 唯一性约束(unique), 主键约束(primary key) PK, 外键约束(foreign key) FK
- where 和 having 的区别: where 是在分组前进行过滤,having 是在分组后进行过滤
- create table consumer( sex enum('male','female')); 枚举类型,只能在指定里面选一个
- create table consumer( hobby set('play','read','study')); 集合类型,可以选多个,nsert into consumer values ('read,study')
- alert table xx auto_increment = xx; 可以改变自增的id
- show session variables like 'auto_inc%'; 会话级别查看 id步长为多少
- set session auto_increment_increment = x; 会话级别改变步长,关了就没了
- set session auto_increment_offset = x; 会话级别改变起始值
- show global variables like 'auto_inc%'; 查看全局id步长值
- set global auto_increment_increment = x; 全局改变步长
- set global auto_increment_offset = x; 全局改变起始值
视图:
就相当于把常用到的字段做个临时表,当然可以跨表,联表,用的时候就当表来用。
创建视图
mysql> create view lal as select nid, username,last_login from app001_userinfo where last_login regexp '2019-08-14 21|22';
Query OK, 0 rows affected (0.02 sec)
使用视图
mysql> select username from lal;
查看所有创建的视图
show table status where comment='view';
删除视图
mysql> drop view if exists lal;
Query OK, 0 rows affected (0.05 sec)
查看视图是否还存在
mysql> show create view lal;
ERROR 1146 (42S02): Table 'blog.lal' doesn't exist
存储过程
相当于定义了一个函数,
创建存储过程
mysql> delimiter //
mysql> create procedure lal()
-> begin
-> select username from app001_userinfo;
-> end //
Query OK, 0 rows affected (0.11 sec)
mysql> delimiter ;
调用存储过程
mysql> call lal();
+----------+
| username |
+----------+
| 123456 |
| 999999 |
| aki |
| ggggg |
| xxxxxx |
| zzzzz |
+----------+
注意:在命令行输入时,要先 delimiter // 改变结束符, call调用
删除存储过程
mysql> drop procedure lal;
显示所有的存储过程
SHOW PROCEDURE STATUS WHERE db = '数据库的名字';
显示存储过程的代码
use '数据库的名字' ;
SHOW CREATE PROCEDURE 存储过程的名字;
带参数的存储过程
在命令行里
in相当于函数的参数,out相当于函数的返回值, into相当于告诉cal 调用那个语句。
mysql> delimiter //
mysql> create procedure lala(
-> in innum int,
-> out outnum VARCHAR(64)
-> )
-> begin
-> select username
-> from app001_userinfo where nid = innum
-> into outnum ;
-> end //
Query OK, 0 rows affected (0.02 sec)
mysql> call lala(3, @outnum);
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select @outnum;
+---------+
| @outnum |
+---------+
| 999999 |
+---------+