zoukankan      html  css  js  c++  java
  • SQL基础语法

    SQL语句的书写顺序

    1. select子句-->2. from子句--> 3.where 子句--> 4.GROUP BY 子句--> 5.HAVING 子句--> 6.ORDER BY子句

    SQL语句的执行顺序

    FROM->WHERE->GROUP BY->HAVING->SELECT->ORDER BY

    1.创建数据库

    CREATE DATABASE shop;

    2.创建表

    CREATE TABLE Product(
    product_id CHAR(4) NOT NULL,
    product_name VARCHAR(100) NOT NULL,
    product_type VARCHAR(32) NOT NULL,
    sale_price INTEGER ,
    purchase_price INTEGER ,
    regist_date DATE ,
    PRIMARY KEY (product_id)
    );

    3.删除表

    DROP TABLE Product;

    4.表定义更新,添加列

    ALTER TABLE Product ADD COLUMN product_name_pinyin VARCHAR(100);

    5.删除列

    ALTER TABLE Product DROP COLUMN product_name_pinyin;

    6.向表中插入数据

    START TRANSACTION;
    INSERT INTO Product VALUES ('0001', 'T恤' ,'衣服', 1000, 500, '2018-09-20');
    INSERT INTO Product VALUES ('0002', '打孔器', '办公用品', 500, 320, '2018-09-11');
    INSERT INTO Product VALUES ('0003', '运动T恤', '衣服', 4000, 2800, NULL);
    INSERT INTO Product VALUES ('0004', '菜刀', '厨房用具', 3000, 2800, '2018-09-20');
    INSERT INTO Product VALUES ('0005', '高压锅', '厨房用具', 6800, 5000, '2018-01-15');
    INSERT INTO Product VALUES ('0006', '叉子', '厨房用具', 500, NULL, '2018-09-20');
    INSERT INTO Product VALUES ('0007', '擦菜板', '厨房用具', 880, 790, '2018-04-28');
    INSERT INTO Product VALUES ('0008', '圆珠笔', '办公用品', 100, NULL, '2018-11-11');
    COMMIT;

    7.变更表名

    RENAME TABLE product to product1;

    8.查询相关语句

      查询基础列

    SELECT product_id, product_name, purchase_price
      FROM Product;

      使用AS关键字为列设定别名(若为中文名,用双引号括起来)

    SELECT product_id    AS id,
           product_name  AS name,
           purchase_price AS price
      FROM Product;

      使用DISTINCT关键字从结果中删除重复行(NULL数据也会被保存起来,多条NULL数据会被合并成一条)

    SELECT DISTINCT product_type
      FROM Product;

      where语句(比较运算符:<>表示不等于)

    SELECT product_name
      FROM Product
     WHERE product_type = '衣服';

      选择null的记录(选择不是null的记录时使用:IS NOT NULL)

    SELECT product_name, purchase_price
      FROM Product
     WHERE purchase_price IS NULL;

      使用and和or运算符

    SELECT product_name, product_type, regist_date
      FROM Product
     WHERE product_type = '办公用品'
       AND (   regist_date = '2009-09-11'
            OR regist_date = '2009-09-20');

    9.聚合函数

      COUNT:  计算表中的记录数

      SUM:  计算表中数值列中数据的合计值;

      AVG:  计算表中数值列中数据的平均值;

      MAX:  求出表中任意列中数据的最大值;

      MIN:  求出表中任意列中数据的最小值;

    //计算表中数据行数
    SELECT COUNT(*) FROM Product;
    //计算该列值非null的行数
    SELECT COUNT(purchase_price) FROM Product;
    //去除重复数据列数
    SELECT COUNT(DISTINCT product_type) FROM Product;
    
    //合计值(计算时排除null)
    SELECT SUM(sale_price) FROM Product;
    
    //平均值(计算时排除null)
    SELECT AVG(sale_price) FROM Product;
    
    //最大值、最小值
    SELECT MAX(sale_price), MIN(purchase_price) FROM Product;

    10.GROUP BY对表进行分组(不要使用列的别名)

    SELECT product_type, AVG(sale_price)
      FROM Product
     GROUP BY product_type;

    11.HAVING子句的用法

    where子句指定行所对应的条件

    having子句指定组所对应的条件

    SELECT product_type, AVG(sale_price)
      FROM Product
     GROUP BY product_type
    HAVING AVG(sale_price) >= 2500;

    12.对查询结果进行排序

    ORDER BY 子句(ASC升序,DESC降序,可以不写ASC默认升序);

    指定多个排序键时,优先级按从左到右;

    排序键中包含null值是,会排序在开头或者结尾。

    SELECT product_id, product_name, sale_price, purchase_price
      FROM Product
    ORDER BY sale_price ASC;

    13.从其他表中复制数据

    INSERT INTO ProductCopy (product_id, product_name, product_type, sale_price, purchase_price, regist_date)
    SELECT product_id, product_name, product_type, sale_price, purchase_price, regist_date
      FROM Product;

     14.delete语句

      清空表数据

    DELETE FROM Product;

    15.update语句的用法

    UPDATE Product
       SET sale_price = sale_price * 10,
           purchase_price = purchase_price / 2
     WHERE product_type = '厨房用具';

    16.创建视图CREATE VIEW语句

    CREATE VIEW ProductSum (product_type, cnt_product)
    AS
    SELECT product_type, COUNT(*)
      FROM Product
     GROUP BY product_type;

      创建好视图之后,可以使用视图,如:

    SELECT product_type, cnt_product
      FROM ProductSum;

      删除视图

    DROP VIEW ProductSum;

    17.子查询(特点相当于一张一次性视图)

    SELECT product_type, cnt_product
      FROM (SELECT product_type, COUNT(*) AS cnt_product
              FROM Product
             GROUP BY product_type) AS ProductSum;

    18.标量子查询(只能返回一行一列值),如在where子句中使用标量子查询

    SELECT product_id, product_name, sale_price
      FROM Product
     WHERE sale_price > (SELECT AVG(sale_price)
                             FROM Product);

    19.关联子查询

    SELECT product_type, product_name, sale_price
      FROM Product AS P1
     WHERE sale_price > (SELECT AVG(sale_price)
                             FROM Product AS P2
                            WHERE P1.product_type = P2.product_type
                            GROUP BY product_type);

    20.like关键字用法

    SELECT *
      FROM SampleLike
     WHERE strcol LIKE 'ddd%';

    ‘dd%'表示前方一致查询,'%dd%'表示中间一致,'%dd'表示后方一致

    %代表任意个字符,_(下划线)代表”任意一个字符“

    21.IS NULL、IS NOT NULL--判断是否为NULL

    SELECT product_name, purchase_price
      FROM Product
     WHERE purchase_price IS NULL;

    22.IN、NOT IN

    SELECT product_name, purchase_price
      FROM Product
     WHERE purchase_price IN (320, 500, 5000);

    23.内联结(INNER JOIN ...... ON)

    内联结只能选取同时存在于两张表的数据

    SELECT SP.shop_id, SP.shop_name, SP.product_id, P.product_name, P.sale_price
      FROM ShopProduct AS SP INNER JOIN Product AS P
        ON SP.product_id = P.product_id
     WHERE SP.shop_id = '000A';

      多表联结(例如三张表)

    SELECT SP.shop_id, SP.shop_name, SP.product_id, P.product_name, P.sale_price, IP.inventory_quantity
      FROM ShopProduct AS SP INNER JOIN Product AS P
        ON SP.product_id = P.product_id
                   INNER JOIN InventoryProduct AS IP
                       ON SP.product_id = IP.product_id
    WHERE IP.inventory_id = 'S001';

    24.外联结

    SELECT SP.shop_id, SP.shop_name, SP.product_id, P.product_name, P.sale_price
      FROM Product AS P LEFT OUTER JOIN ShopProduct AS SP
        ON SP.product_id = P.product_id;

    25.集合运算

    表的加法UNION(并集)

    包含重复行UNION ALL

    选出表中公共部分:INTERSECT(交集)

    记录的减法:EXCEPT

  • 相关阅读:
    20.GC日志详解及日志分析工具
    19.JVM调优工具锦囊
    两个页面的传参(转自博客园的春哥也编程)
    纯js实现背景图片切换
    关于引用类型用ref传参的问题
    C++ return
    C++内存管理
    Chrome插件开发一(配置文件)
    C++对象传递
    const 与 #define 的比较
  • 原文地址:https://www.cnblogs.com/chao-zjj/p/9948187.html
Copyright © 2011-2022 走看看