SQL杂记

作者: Lunafobia | 来源:发表于2019-06-11 23:37 被阅读0次

    SQL是Structured Query Language,其指令分为四个子集,如下图:


    SQL指令分类

    图片来源:https://www.geeksforgeeks.org/sql-ddl-dml-tcl-dcl/

    其中DML用于查询,DDL用于创建、插入、删除等操作,这两者构成了SQL的基础。DCL和TCL一般倾向于数据库&服务器的操作,DCL用于用户授权,TCL中文成为事务控制语言,事务(Transaction)是一系列相关的SQL语句组成的动作单元,用于完成某个固定操作。CSDN上有一篇详解:https://blog.csdn.net/G090909/article/details/53153218,以及Geeksforgeeks上的英文版:https://www.geeksforgeeks.org/sql-transactions/

    § Constraints (5种约束)
    Constraints are the rules that we can apply on the type of data in a table. That is, we can specify the limit on the type of data that can be stored in a particular column in a table using constraints.
    <https://www.geeksforgeeks.org/sql-constraints/> ---- include examples how to use it
    -NOT NULL
    -UNIQUE
    -PRIMARY KEY
    -FOREIGN KEY
    -CHECK
    -DEFAULT

    § Triggers
    A trigger is a stored procedure in database which automatically invokes whenever a special event in the database occurs. For example, a trigger can be invoked when a row is inserted into a specified table or when certain table columns are being updated.
    <https://www.geeksforgeeks.org/sql-trigger-student-database/>

    § Functions
    Aggregate functions: count(), sum(), avg(), min(), max(), first(), last()
    Scalar functions: ucase(), lcase(), mid(), len(), round(), now(), format()
    Mathematical functions: sqrt(), pi(), square(), round(), ceiling(), floor()
    Listagg:
    AGG function in DBMS is used to aggregate strings from data in columns in a database table. Aus <https://www.geeksforgeeks.org/sql-listagg/>
    Date functions (a loooooot):
    https://www.geeksforgeeks.org/sql-date-functions-set-1/
    https://www.geeksforgeeks.org/sql-date-functions-set-2/
    Conditional Expression: case(CASE WHEN ** THEN ** END), decode, coalesce, greatest, ifnull, in, least, nullif
    https://www.geeksforgeeks.org/sql-conditional-expressions/
    String functions (many)
    Numeric functions (many)

    § View

    § 常见query
    §§ like 和通配符(wildcard)(%, _, [])
    通配符搜索只能用于文本字段,非文本数据型字段不能使用。Eg WHERE name LIKE 'Fish%'
    §§ group by
    §§ order by
    §§ union
    §§ join
    §§ having v.s. where: where语句用在分组之前的筛选,having用在分组之后的筛选,having中可以用合计函数,where中就不行。使用where的地方可以用having替代
    ……

    § 理解SQL难点,就两点:
    第一点:数学映射join(一对一,一对多,多对多);
    第二点:就是SQL的执行顺序;
    其他:至于底层的东西,先把SQL数据跑起来,跑正确再说

    § 执行顺序
    https://mp.weixin.qq.com/s?__biz=MzIyNzA5MjM5NA==&mid=2647852899&idx=1&sn=b2dcfe42a09fea4f7507bf7117e05d32&scene=19#wechat_redirect

    摘自知乎下小雨
    如图所示,我们书写的时候,要注意关键字的顺序,比如select,from写完后,要先where,然后group by,然后having,order by永远是放在最后面。但是实际执行的时候,系统先跳过select,从from开始、加载需要处理的表格,然后where进行初步过滤,注意这个过滤是在分组前进行过滤,然后通过group by进行分组,再利用having进行分组后的过滤、筛选满足条件的组,然后才是select及其之后的语句。理解了这一点,也就很容易理解一些语法规定了,例如:where条件中不能跟聚合函数,而having后面可以;执行顺序where>聚合函数(sum,min,max,avg,count)>having,order by是最后的显示结果进行排序,必须放于最后,等等规定。
    <https://zhuanlan.zhihu.com/p/39246170> --很好的实践总结文,推荐阅读

    另,推荐《SQL必知必会》(简单的入门书),以及知乎专栏《有关SQL》https://zhuanlan.zhihu.com/c_180683105 。其中有一篇介绍大数据生态链各阶段入门书的文章,可以作为自学索引 https://zhuanlan.zhihu.com/p/63963773

    相关文章

      网友评论

        本文标题:SQL杂记

        本文链接:https://www.haomeiwen.com/subject/msdhfctx.html