SQL常用知识点
2022/2/11 19:15:29
本文主要是介绍SQL常用知识点,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
-- 表清除数据 1.drop table 清除数据并且销毁表(不可撤销,速度最快) 2.truncate table 表清空,包含自增计数器重置(不可撤销) 3.delete table 删除(符合某些条件)数据,执行后可撤销 truncate product; -- 等价于 delete from product; alter table product auto_increment=1; //自增计数器重置 -- 替换函数 replace(s,s1,s2) 使用字符串s2代替s中所有的s1 -- 插入语句 (插入替换数据,用新数据替换已经存在数据,如果没有则和insert into一样) replace into product(pro_name) values (“产品1”); -- 时间差 timestampdiff(interval,time_start,time_end) 单位以interval为准,常用可选 second 秒;minute 分钟;hour 小时;day 天;month 月;year 年 -- 创建表 -- 从另一张表复制表结构创建表 create table product_new like product -- 从另一张表的查询结果创建表 create table product_new as select * from product where pro_name =’’ -- 修改表(选项集合) -- 添加列 alter table product add column pro_name varchar(10) -- 删除列 alter table product drop column pro_name -- 修改列名和类型 alter table product change column pro_name pro_new varchar(10) -- 修改列类型 alter table product modify column pro_name text -- 修改表名 alter table product rename to product_new -- 删除表 drop table if exists product -- 创建索引 1.索引不包含有null值的列 2.一个查询只使用一次索引,where中使用了索引,order by就不会使用 3.在列上进行运算后不会使用索引,如year(start_time)<2020不会使用start_time上的索引 -- 创建普通索引 create INDEX idx_pro_name on product(pro_name) -- 创建唯一索引 create unique INDEX uniq_idx_pro_id on product(pro_id) -- 创建全文索引 create fulltext INDEX full_idx_pro_text on product(pro_text) -- 创建索引(alter方式) alter table product add INDEX idx_pro_name(pro_name) -- 删除索引(drop方式) drop INDEX idx_pro_name on product -- 删除索引(alter方式) alter table product drop INDEX idx_pro_name -- 运算符 1.所有包含null的计算,结果肯定是null 2.符号=就是比较运算符,一定要让不等号在左,等号在右 -- 各种函数 -- 聚合函数 1.常用聚合函数 count,max,min,sum,avg 2.除了函数count(*)外,其余聚合函数(包括count(<列名>)) 都会忽略空值 3.group by 子句中不能使用select子句中列的别名 4.group by 子句的聚合结果是无序的 5.为了得到相同的结果,将条件写在where子句中要比写在having子句中的处理速度更快 -- 算术函数 1.ABS函数(计算绝对值),ABS(列名) 2.MOD函数(计算除法余数,求余函数),MOD(被除数,除数),只能对整数类型的列使用 3.ROUND函数(四舍五入),ROUND(列,保留小数的位数) -- 日期函数 1.select current_date from product; --2022-02-10 2.select current_time from product; -- 16:01:05 +0800 3.select current_timestamp from product; -- 2022-02-10 16:01:05.591 +0800 4.select extract(month from current_timestamp) from product; -- 2 (month、day、year) -- 转换函数 1.select cast(pro_name as varchar(30)) from product; -- 数据类型转换 2.select coalesce(pro_price,0) from product; -- 将null值转换成0 -- 谓词 常用的谓词有:between、is null、is not null、in、or 需要注意的是:在使用in和not in时是无法选取出null数据
这篇关于SQL常用知识点的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南