![79cc4bf4994605bb5a9a113a937ef3c5.png](https://i-blog.csdnimg.cn/blog_migrate/904043ec084fe2ab7a30f22caafbb3ca.jpeg)
1,修改表名
rename table <表名> to <表名新>
2,添加列
alter table <表名> add column <列名> 定义
3,修改列名
alter table <表名> change column 列名 列名 定义
4,删除列
alter table <表名> drop column <列>
5,查询重复值
select count(*) as repetitions,<列名>
from <表名>
group by <列名>
having repetitions > 1;
6,根据条件删除数据
delect
7,设置主键
alter table <表名> add primary key (列名)
8,设置主键并自增长
alter table <表名> Modify <列名> int not null auto_increment primary key;
5,把列设为第一列
ALTER
6,删除字段中空格
replace(替换字段,被替换内容,替换内容)
update <表名> set <列名>=replace(`列`, ' ', '')
7,大小写转化
update table set <列名>=upper(列名) UPPER() 将小写转成大写
update table set 列=lower(列) LOWER()将大写转成小写
8,mysql中控制数据库名和表名的大小写敏感由参数lower_case_table_names控制,为0时表示区分大小写,为1时,表示将名字转化为小写后存储,不区分大小写。
mysql> show variables like '%case%';
9、locate函数可以实现类似indexof的功能,
locate(substr,str)返回substr子串在字符串str中的位置。
10、substring函数,截取字符串:
substring(str, pos)
substring(str, pos, length)
说明:substring(被截取字段,从第几位开始截取)
substring(被截取字段,从第几位开始截取,截取长度)
11、按关键字截取字符串 :
substring_index(str,delim,count)
说明:substring_index(被截取字段,关键字,关键字出现的次数)
例:select substring_index(444.55.22.3",".",2) as abstract from <表名>
结果:444.55
12,替换null值
COALESCE函数 返回不是null
SELECT COALESCE(NULL, NULL, 1);
-- Return 1
13,拼接:concat()
id name
01 张
02 王
##拼接字符串
update <表名> set <列> = CONCAT("添加字符",test,"添加字符串",test,"添加的字符串") where id = 1
id name
01 添加字符张添加字符串张添加的字符串
02 王