记录一些日常的mysql常用的使用, 方便随用随查。
show create table table_name;
# 查询表所有列信息
select *
from information_schema.columns
where table_schema = 'db' #表所在数据库
and table_name = 'tablename' ; #你要查的表
# 查询需要的信息
select
column_name, column_type, column_comment,is_nullable
from information_schema.columns
where table_schema = 'o2o_monitor' #表所在数据库
and table_name = 'dws_o2o_overdue_stat' ; #你要查的表
Rename table table_old to table_new
OR
alter table table_old Rename to table_new
# 增加字段
alter table table_name add column_new bigint(20) DEFAULT NULL COMMENT '备注' After somecolumn;
# 修改字段
alter table competitor_goods change column_old column_new bigint(20) DEFAULT NULL COMMENT '备注';
create table table_new like table_old;
# 删除索引
ALTER TABLE table_name drop index appid_idx
# 添加索引
ALTER TABLE table_name ADD INDEX appid_idx (apply_id)
SQL Select语句完整的执行顺序:
select * from order where order_id=1231540
-- 事务开始
BEGIN
-- 更新
update order
set order_date = date_sub(order_date, interval 9 day)
where order_id=1231540
-- 校验
select * from order where order_id=1231540
-- 错误回滚
ROLLBACK
-- 正确提交
COMMIT
要更新的数据不能直接找到,需要做表关联才能找到
update order o
(left) join user u
on o.user_id = u.id
set o.order_date = date_sub(o.order_date, interval 9 day)
where u.user_name='张三'
原文:https://www.cnblogs.com/zhanbingliu/p/10699701.html