mysql常用sql记录

注意以下语法为mysql5.7
一、操作表相关的sql
1、给psi_register表添加一个列 TIME_FLAG 插入数据时并给该列当前系统时间

alter table psi_register add column TIME_FLAG TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

2、删除psi_register表的TIME_FLAG字段

ALTER TABLE psi_register DROP TIME_FLAG;

3、随机删除mysql数据表部分数据

delete from psi_register order by rand() limit 10

4、查询数据库表的某一字段重复的次数

select t.userid,t.username count(*) as count from USER_NAME t group by t.userid having count>1;

5、将含有外键的表结构文件查询出并生成批处理文件

SELECT 
     concat('ALTER TABLE ',t.TABLE_NAME,' DROP FOREIGN KEY ',t.CONSTRAINT_NAME,' ;')
FROM
    information_schema.TABLE_CONSTRAINTS t
where t.TABLE_SCHEMA = '数据库名称'  -- and t.TABLE_NAME = '表名'
 and t.CONSTRAINT_TYPE = 'FOREIGN KEY';

你可能感兴趣的:(mysql)