分享

SQL 基础整理

 路人甲Java 2022-03-09
  • SQL语句执行顺序
  1. From
  2. 表连接
  3. on连接条件,形成新的虚拟表
  4. where 筛选条件
  5. group by 生成新的结果集合  group by 分组列表
  6. having 分组后筛选
  7. select 选出显示的
  8. order by 排序
  9. limit        分页/个数限制
  • 子查询顺序

  除了exists,先执行子查询,再外查询

  exists:相关子查询,先执行外查询,再根据子查询字段进行过滤。

  • 分页查询

  limit (page-1)*size,size

  • 字符控制函数

substr('helloworld',1,5)  hello

instr('helloworld','w')      6

LPAD(salary,10,'*')        ****salary

TRIM('H' from 'Helloworld') elloworld

  • 视图应用场景
    • 多个地方用到同样的查询结果
    • 该查询结果会用的SQL语句较复杂
  • DML 表操作 无table
insert into tableName( column1,column2,...) values(值1,...)

 

update student
set age=18
where  name='hwj'

 

/* 表数据删除 */
delete
from student where age=18
/* 多表 */
delete from student s1,course c1
where s1.class=c1.class
and s1.name='mm';
# 方式2
truncate 语句
truncate table student

 

  •  DDL 管理操作,对表操作要加上字段table
# 创建
create table if not exists student(column1,...)
# 修改 字段名
Alter table student change column 旧 新字段

# 修改表名
Alter table student rename to student1

# 修改字段类型和约束
Alter table tablename modify column column_name  约束

# 添加字段
Alter table 表名 Add column 字段;

# 删除字段
Alter table 表名 drop column 字段

# 删除表
drop table if exists 表名

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多