MYSQL随手写-持续更新

注:本随笔只是mysql知识的积累,如果您看到此随笔,有错误请指正,谢谢!
# 标准建表语句
DROP TABLE IF EXISTS temp_whc_ceshi2 ;

CREATE TABLE IF NOT EXISTS temp_whc_ceshi2
(
s_no int(20) NOT NULL auto_increment COMMENT "自增主键|学号"
,s_name varchar(25) default "未获取" COMMENT "学生姓名"
,s_cno int(30) default 10 COMMENT "班级号"
,create_time datetime not null default current_timestamp comment'创建时间'
,updatetime datetime not null default current_timestamp on update current_timestamp comment '更新时间'
,PRIMARY KEY (s_no)
)
ENGINE InnoDB DEFAULT CHARSET = UTF8 COMMENT "测试表"
;
# 查看表结构
desc temp_whc_ceshi2;
# 查看建表语句
show create table temp_whc_ceshi2;
# 修改表名
alter table temp_whc_ceshi2 rename to temp_whc_ceshi;
# 修改字段类型
alter table temp_whc_ceshi modify s_cno varchar(30);
# 修改列名字
alter table temp_whc_ceshi change s_no s_id int(30);
# 增加字段
alter table temp_whc_ceshi add column s_sex varchar(5) default "男" comment "性别" after s_name;
alter table temp_whc_ceshi add column (one int(10) default 0 comment "一个字段",two int(10) default 0 comment "二个字段");
# 删除字段
alter table temp_whc_ceshi drop sex;
# 字符类型
decimal(8,2) # 主要金额类定义
# 插入系统当前日期
current_time 时分秒 / now() 年月日时分秒 /current_timestamp 时分秒
# 函数
abs() # 绝对值
mod(a,b) # 求余数
round() # 四舍五入
char_length() # 字符串字符个数
length() # 字符串字节长度
concat() # 字符串连接函数
concat_ws("连接符",字符串1,字符串2) # 字符串连接函数 含连接符 忽略空值
insert(字符串1,起始位,替换长度,目标字符串)
upper()/lower() # 大写小写函数
left(字符串,位数)/right(字符串,位数) # 输出字符串左/右侧指定个字符
locate(搜索字符,指定字符)/position(搜索字符,指定字符)/instr(搜索字符,指定字符) # 输出字符串开始位置
replace(指定字符,目标,期望) # 替换函数
# 系统日期
current_date/curdate() # 2018-12-03
current_time/curtime() # 15:06:00
current_timestamp()/localtime()/now()/sysdate() # 2018-12-03 15:29:00
unix_timestamp() # 10位时间戳
from_unixtime() # 解析出unix时间
utc_date() # 当前时区日期
month() # 12 月份
monthname() # December 月份名字
dayname() # Thursday 星期几
dayofweek() # 日的索引:1代表周日 2代表周一
weekday() # 日的索引:0代表周一 1代表周二
week(时间,参数0/1) # 返回时间是本年的第几周,如果参数为0或不写,则为以周日为一周第一天,如果是1 则以周一为第一天
dayofyear() # 返回时间是本年第几天
dayofmonth() # 返回时间是本月第几天
extract() # year from '2018-12-10 00:00:00' 返回 2018,month from 时间,返回12,day from 时间返回10,year_month from 时间返回201812
adddate()/date_add(时间,interval 增加值 模式) # 模式=day,加日,month 加月,year 加年
date_sub()/subdate(时间,interval 减少值 模式) # 同上
addtime(时间,增加值)/subtime(时间,减少值) # 值='1:1:1'增加1小时1分钟1秒钟
datediff(时间1,时间2) # 返回两个时间的差值(天为单位)
date_format(时间,输出格式) # %Y%m%d=20181210 %Y-%m-%d=2018-12-10 %W %M %Y Monday December 2018 %H:%i:%s 16:21:37
# 逻辑判断
if(条件,值1,值2) # 成立选1,不成立选2
coalesce(目标,替换) # 判断为空则替换为替换值
ifnull(值1,值2) # 返回非空值
case 目标 when 条件 then 值 end # 逻辑判断
# 加密函数
md5()
# 转换字符类型函数
cast(目标 as 字符)/convert()
rand() # 随机值
# 查询
in/not in
between and # 包含起始和结尾值
like # 匹配函数
and/or # 并列/或
distinct # 去重
order by # 排序 desc降序/asc升序 默认升序
group by having # 分组
count() # 计算行数
sum()/min()/max()/avg()
# 表关联
inner join # 内连接 返回共同的值
left outer join # 返回左表全部 N:1
exists/not exists # 表关联查询速度快
# 合并结果
union/union all # 合并查询列数/字符类型应一致,前者去重后者不去重
# 正则查询
regexp # 正则匹配关键字,在这里不进行介绍,以后单独学习正则
# mysql中的存储过程、函数、触发器、视图等不再进行学习,正常用到的时候在进行学习

 
posted @ 2018-11-29 14:44  牛屎虫  阅读(244)  评论(0编辑  收藏  举报