mysql数据库简单操作
(1) 内容替换语句:
update user_info_tbl set user_name = (replace(user_name,'XH','KL'));
(2) 保留两位小数:
convert(100.223, decimal(10,2));
(3) update条件执行:
update goods_info_tbl set deal_false_num=case when deal_false_num>0 then deal_false_num+1 else deal_false_num end;
(4) 修改自增正的默认值为10
alter table lottery_pc28_info_tbl auto_increment=10;
(5) 查询数据库 表中数据 的数量等信息:
SELECT
table_name,
table_rows
FROM
information_schema.tables
WHERE
table_schema = 'db_22_newans'
ORDER BY
table_rows DESC;
(6) 外键:
show VARIABLES like "foreign%"; //查看外键
SET foreign_key_checks = 0; //关闭外键
SET foreign_key_checks = 1; //开启外键
(7) 删除数据库所有的表
SELECT concat('DROP TABLE IF EXISTS ', table_name, ';')
FROM information_schema.tables
WHERE table_schema = 'mydb';
(8) 将时间戳(秒数) 转为 时间格式:
select from_unixtime(1256540102);
插入时间戳:
select unix_timestamp('2009-10-26 10-06-07'); unix_timestamp(now()) ;
例如:INSERT INTO `log_info_tbl`(`log_id`, `add_time`) VALUES (53, unix_timestamp(now()));
mydb换成你想删除的数据库的名字, 这样可以生成一个批量处理的sql语句,你需要再运行一次这个结果集 就可以删除所有的表而不删除数据库了;
(9): mysql设置复合主键方法
alter table tb_name add primary key (字段1,字段2,字段3);
(10) mysql 数据库删除数据,表碎片查询
show table status from db_1903_answer like 'exam_questions_tbl';
清除碎片:
alter table exam_questions_tbl engine = innodb;
或
optimize table exam_questions_tbl ;