增删改查
增:增可分为增行(值),增列(关键字),增表
增表:
create table test01(
id varchar(20) primary key ,
name varchar(20)
)
create table test 02 as select *from test01
增行:
insert into test01 values('001','bbb');
增加关键字:
alter table test01 add QQ varchar(20);
改:修改某个值
update :update test01 set name ='qqq' where id ='001'
查:
查找某条数据
select *from test01 where ID ='001'
select id from test01 where name ='qqq'
插入:
insert into test02 select * from test01
删:删可以分为删行,删列,删表
删行:delete from test01
删列:alter table test01 drop column qq
删表:drop table test01
获取去年今天日期
to_char(trunc(sysdate)+(INTERVAL '-1' YEAR),'YYYY-MM-DD' )
今年第一天
to_char(extract(year from sysdate)-1)
排序
:按abcd倒叙
order by (case dim.productzsname
when 'a' then 1 when 'b' then 2 when 'c' then 3
when 'd' then 4 when 'e' then 5 when 'f' then 6
end) desc
MYSQL函数
1.字符函数
mysql并发问题
脏读 : A B 两个事务 A读取到了B更新但未提交的数据 B 回滚 A读取的数据无效 则为脏读
不可重复度 : A读取到了B更新未提交的数据 B回滚之后 数据变成原来的 那么A之前读到的更新后的数据就不能背再次读取到 针对读取
幻读 : A读取某一条件记录 N+1条 其中有一条是B插入后未提交的 B如果回滚 A再次读发现只有n条了 这就是幻读 针对插入数据
如何解决
设置数据亏事务隔离级别
数据库存储引擎
show engines
InnoDB
索引
单值索引: create index idx_user_name on user(name)
drop index idx_user_name on user
show index from table
select testA.id id ,testA.`name` `name`,testB.id id2, testB.`name` name2 from testA LEFT JOIN testB on testA.`name` = testB.`name`
where testB.id IS NULL
MYSQL性能分析
id 用于配置任务优先级 id越大优先级越高
select_type
(1)SIMPLE
简单的SELECT语句(不包括UNION操作或子查询操作)
(2)PRIMARY/UNION
PRIMARY:查询中最外层的SELECT(如两表做UNION或者存在子查询的外层的表操作为PRIMARY,内层的操作为UNION)
table partitions type possible_keys key key_len ref rows filtered
4)SUBQUERY/DEPENDENT SUBQUERY
SUBQUERY:子查询中首个SELECT(如果有多个子查询存在):
5)DERIVED/MATERIALIZED
延伸的子表
DERIVED:被驱动的SELECT子查询(子查询位于FROM子句)
访问类型 type
System>const>eq_ref>ref>range>index>ALl
EXPLAIN select
*from
tb_blog tb
where tb.blog_category_id = 26
And
tb.blog_status>1
order by blog_views
DESC LIMIT 1;
EXPLAIN select
*from
tb_blog tb
where tb.blog_category_id = 26
And
tb.blog_status=1
order by blog_views
DESC LIMIT 1;
范围索引打断了index排序
多表联查
left join index +right
right join index +left
、
mysql 时间
CREATE TABLE `mytest` (
`id` bigint NOT NULL AUTO_INCREMENT,
`comments` varchar(255) DEFAULT '' COMMENT '内容',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CURRENT_TIMESTAMP
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
时间 无需代码控制