1.删除重复元素
mysql> delete from [table] where id not in (select minid from (select min(id) as minid from onlinetest group by [字段]) b);
2.删除库和表
mysql>drop database [database name]
mysql>drop table [table name]
3.删除表内数据
mysql> delete from [table name] [where ...]
4.改变字段长度
mysql 修改字段长度
alter table news modify column title varchar(130);
alter table 表名 modify column 字段名 类型;
如:news 表里的title 字段 原来长度是 100个字符,现长度要改成130个字符
alter table news modify column title varchar(130);
5.修改字段名称,类型
修改某个表的字段名称及指定为空或非空
>alter table 表名称 change 字段原名称 字段新名称 字段类型 [是否允许非空
4如果要删除某一字段,可用命令:ALTER TABLE mytable DROP 字段 名;
5.统计表内某个字段不重复值的个数
select count(distinct(name)) from [table]
6.数据写入报错
创建table,插入汉字,报错Incorrect string value,大概是编码问题。
mysql> alter table onlinetest convert to character set utf8;将table编码噶为utf8就可以了
7.pymysql写入mysql的数据都带有引号,在数据库操作中很不方便。
UPDATE [table] SET title=REPLACE(title,'\'',''); title表示要操作的字段
8.创建table并建立时间戳
mysql> CREATE TABLE userdata( -> id INT NOT NULL AUTO_INCREMENT, -> name VARCHAR(20) NOT NULL, -> passward VARCHAR(100) NOT NULL, -> created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -> PRIMARY KEY(id));
9.查询某张表的存储空间
mysql > select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') from information_schema.tables where table_schema='test' AND table_name='t9';