mysql(未完待续)

  1. create database xx default charset utf8;                                创建数据库                 
  2. create table xx (id int auto _increment, name char(10)) engine=innodb charset=utf8;                   创建表默认的引擎不支持事务 
  3. create table xx (id int not null auto_increment primary key);
  4. create table xx ( id int auto_increment primary key, name char(10), constraint 自己起的外键名字 foreign key (本表字段) references xx (外表字段)                                                                         创建外键  
  5. show databases;                                                                    显示数据库
  6. use XX;                                                                                  选中要操作的数据库
  7. show tables;                                                                           显示表结构
  8. show column form xx;  desc xx;                                             显示表中所有字段
  9. delete from xx;                                                                       清空表,id按顺序接着来,所谓清空表,只是删除了表里的数据
  10. truncate table xx;                                                                   清空表,id重新来,快
  11. drop table xx;                                                                         删除表
  12. insert into xx (id,name) values (1,'xx'), (2,'xxx')                     插入数据
  13. update xx set age=18 where ..                                              更新数据
  14. select xx,oo from xx;
  15. select distinct xx键 from xx;                                                    去重
  16. select xx from xx limit  5;                                                        只拿5条
  17. select xx from xx limit 5,5;                                                      返回行5(第四行)开始的5行,行1 是第零行
  18. select xx from xx limit 4 offset 3;                                            从行3开始拿4条    
  19. select date_format(oo, '%Y-%m') from xx;                             时间格式化,oo字段形如2018-05-25-13:20:01,只拿年月
  20. select x,xx,xxx from xxoo where ... order by x desc ,xx;        先按x按照降序排列,x相同则按照b升序排列,注意放在where后面(默认是asc升序)
  21. select xx from xx where between xx and xo;                         关键字between,取值范围,左右包含xx,xo
  22. selec xx from xx where xo is null;                                          按字面解释
  23. select xx,xxx from xx where .. and(or)..;                                          
  24. select xx,xxx from xx where a or b and c;                              注意,会优先解释and,然后才是or ,加个括号可以解决
  25. select xx from x where xx in (xo,ox);                                      相当于or
  26. select xx where like  'xx%';                                                     %任意字符出现任意次
  27. select xx where regexp  '1|2' ;                                                正则匹配,注意''         还有注意mysql正则转义 \\,是两个, \\\匹配\
  28. * 0个或多个       + 1个或多个         ?0个或1个      {n}  指定数目    {n,m}  匹配范围
  29. select xx concat(xx, '(' , xx , ')' ) from xx;                                字符串拼接
  30. selec concat(rtrim(xx)) from xx;                                             去除两端空格
  31. where ... group by .. order by                                                 group by必须在where 之后 ,order by 之前
  32. 如果在select中使用表达式,则必须在group by 子句中指定相同的表达式,不能使用别名
  33. decimal(4,5)                                                                           精准的储存小数,xxxx.xxxxx
  34. char(10)                                                                                  速度快,10个位置储存,定长
  35. varchar(10)                                                        变长,节省空间,比如存root字符串,虽然指定空间为10, 但实际只会占用4个空间
  36. 几种约束   非空约束(not null), 唯一性约束(unique),  主键约束(primary key) PK, 外键约束(foreign key) FK
  37. where 和 having 的区别:                                                      where 是在分组前进行过滤,having 是在分组后进行过滤
  38. create table consumer( sex enum('male','female'));                 枚举类型,只能在指定里面选一个
  39. create table consumer( hobby set('play','read','study'));          集合类型,可以选多个,nsert into consumer values ('read,study')
  40. alert table xx auto_increment = xx;                                          可以改变自增的id
  41. show session variables like 'auto_inc%';                                 会话级别查看 id步长为多少
  42. set session auto_increment_increment = x;                            会话级别改变步长,关了就没了
  43. set session auto_increment_offset = x;                                   会话级别改变起始值
  44. show global variables like 'auto_inc%';                                    查看全局id步长值
  45. set global auto_increment_increment = x;                               全局改变步长
  46. 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  |
+---------+

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值