乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      sql初學者筆記 語法基礎

       丹楓無跡 2022-03-15

      常見注釋

      • -- 很少支持
      • 行內(nèi)注釋

      • /**/段落注釋

      基礎語法

      SELECT

      檢索數(shù)據(jù)

      語法 作用 例子 釋義
      select 查找列,并返回行 select prod_name from products;
      #可使用,分隔列名來查找多個列。
      查找prod_name列,并返回其下的所有行,在products表中。
      * 通配符 select * from products; 查找所有列并返回所有行,在products表中。
      distinct 返回不重復的值 select distinct vend_id from products;
      #不可配合通配符使用除非所有列完全相同
      查找vend_id列并返回其下所有行中不重復的值,在products表中。
      limit 限制 select prod_name from products limit 5,5; 查找prod_name列并返回其下第5行起5行的值,在products表中。

      排序檢索數(shù)據(jù)

      語法 作用 例子 釋義
      order by 排序 select prod_id,prod_price,prod_name
      from Products
      order by 2;
      #默認升序(ASC)排列
      #指定按多個列排列時:僅當指定的第一列中有重復元素時,才對其(存在重復值的)按指定的下一列進行排序。
      即按照查找的第二個列進行排序,也可指定列名(prod_price)
      desc 降序 select prod_id,prod_price,prod_name
      from Products
      order by 2 DESC,3 desc;
      即按照查找的第二個列進行降序排序,desc僅對其前的列有效;

      過濾數(shù)據(jù)

      語法 作用 例子 釋義
      where 在客戶端過濾數(shù)據(jù) select *
      from Products
      where prod_price >= 5.99
      order by prod_price desc;
      #同排序操作一同使用時,不得位于排序操作之前#支持<>=!=等操作,其中<>操作等同于!=
      例:
      select *
      from Products
      where prod_id <>'fc'
      order by prod_price desc;
      #過濾字符串不區(qū)分大小寫
      1.查找所有列,在Products表中,并返回prod_price >=5.99的所有行
      2.查找所有列,在Products表中,并返回除prod_id = "fc"之外的所有行
      between 值的范圍過濾 select prod_name,prod_price
      from Products
      where prod_price between 4 and 10
      查找prod_name,prod_price兩列在Products表中,并返回prod_price值為4-10范圍內(nèi)的的的所有行
      is 可用來檢查null(空值) select prod_name,prod_price
      from Products
      where prod_price is null
      返回所有沒有價格的商品
      and,or 邏輯操作符
      and且
      or與,這里是短路的
      select *
      from Products
      where vend_vend_id ='1001' and prod_price <=4;
      #and的優(yōu)先級比or要高,and,or共同使用時為避免錯誤應用()明確分組,
      #也可使用in代替or,例:
      select prod_name,prod_price from products where vend_id in('1001','1002')
      order by prod_name
      等同于:
      select prod_name,prod_price from products where vend_id = '1001' or vend_id = '1002'
      order by prod_name
      返回所有vend_vend_id ='1001' 且 prod_price <=4;的行
      not 否定其后的條件 select prod_name,prod_price from products where not vend_id in('1001','1002')
      order by prod_name
      可與in連用,返回vend_id=1001 vend_id=1002外的所有行

      通配符搜索

      語法 作用 例子
      % 匹配0、1或多個字符包含空格。不會匹配到null select prod_name
      from products
      where prod_name like 'f%%'
      _ 匹配單個字符,包含空格 select prod_name
      from products
      where prod_name like 'fuse_'
      rtrim()ltrim() 去除右邊、左邊空格

      創(chuàng)建計算字段

      select prod_id ,quantity,item_price,quantity*item_price as expanded_price
      from orderitems
      where order_num = 20008;
      #如上創(chuàng)建了一個expanded_price字段(quantity*item_price的結(jié)果的別名),其僅在此時有效而不會存放到表中。
      

      使用函數(shù)

      select vend_name, upper(vend_name) as vend_name_upcase
      from vendors
      #將vend_name列下的所有行以大寫形式返回
      select avg(prod_price) as avg_peice from products where vend_id ='1001'
      #返回平均值
      select count(*) as num_cust from customers
      #返回長度(數(shù)目),也可對列表中特定值進行計數(shù)
      
      

      分組

      select  vend_id,count(*) as num_prods from  products #對vend_id每行進行計數(shù)
      group by vend_id;#按照vend_id排序并分組
      
      select cust_id,count(*) as orders
      from orders 
      group by cust_id 
      having count(*)>=2#過濾分組中>=2的,having支持where的所有操作
      
      select order_num,count(*) as items
      from orderitems group by order_num
      having count(*) >=3
      order by items,order_num desc#對分組依照選定的列進行排序
      
      

      子句查詢

      select cust_name,cust_contact from customers where cust_id =(select cust_id 
      from orders
      where order_num = (select order_num from orderitems where prod_id = 'jp2000'));
      #由內(nèi)而外,哈哈
      等效于:
      select order_num 
      from orderitems
      where prod_id = 'jp2000';
      select cust_id 
      from orders
      where order_num =20006
      select cust_name,cust_contact from customers where cust_id =10003
      

      聯(lián)結(jié)(返回不在同一個表中的行)

      /*等值語法*/
      select vend_name, prod_name,prod_price
      from vendors,products
      where vendors.vend_id=products.vend_id;#此處過濾聯(lián)結(jié)條件。
      #如沒有聯(lián)結(jié)條件過濾,將檢索出“笛卡爾積”:表1行數(shù)*表2行數(shù)
      /*規(guī)范語法*/
      select vend_name, prod_name,prod_price
      from vendors inner join products
      on vendors.vend_id=products.vend_id
      
      自聯(lián)結(jié)(比子查詢更快)
      /*子查詢*/
      select cust_id, cust_name, cust_contact
      from customers
      where cust_name = (select cust_name from customers where cust_contact ='jim jones');
      /*自聯(lián)結(jié)*/
      select c1.cust_id, c1.cust_name,c1.cust_contact
      from customers as c1,customers as c2#不以別名進行會引發(fā)錯誤
      where c1.cust_name=c2.cust_name and c2.cust_contact='jim jones'#聯(lián)結(jié)cust_name與c2.cust_name ,并過濾cust_contact='jim jones'的行
      /*(c1的cust_name同c2相同,找到了c2的cust_contact='jim jones'也就相當于找到了c1cust_contact='jim jones',知道了cust_contact='jim jones'就可知道cust_id)*/
      

      組合查詢

      select cust_name,cust_contact,cust_email,cust_state
      from customers
      where cust_state in('il','in','mi')
      union  #組合上下select多個select之間需要多個union分隔,union默認排除重復,union all則不排除
      select cust_name,cust_contact,cust_email,cust_state#union中每個查詢必須包含相同的列、表達式、或聚集函數(shù)
      from customers
      where cust_name ='wascals'
      order by cust_name;#不能分別對每條union指定不同的排序
      

      INSERT

      依賴于次序的插入

      /*在得知列的次序后才可使用此方式添加,若發(fā)生了列的次序變動此添加方式將不安全*/
      insert into customers
      values('1000000006','toy land','123 any street','new york','ny', '11111','usa',null, null);
      #依賴于次序的插入,必須為每一列提供一個值,如某列無添加則應寫上null
      

      提供列名的插入

      insert into customers(cust_id,cust_contact,cust_email,cust_name,cust_address,cust_city,cust_state,cust_zip)
      #必須為提供了列名的列給出一個值
      values(null, null,'1000000006','toy land','123 any street','new york','ny', '11111');
      

      從另一個表插入

      insert into 表名(列名)
      select 列名
      from 表名
      where 過濾
      

      復制一個表

      /*sql*/
      select * 
      into custcopy 
      from customers;
      /* mysql*/
      create table custcopy as
      select * 
      from customers;
      

      UPDATE

      更新單個列

      update customers
      set cust_email = 'kim@@thetoystore.com'
      where cust_id = '100000000005'#如不指定,將更新customers表cust_email列下的所有行
      

      更新多個列

      update customers
      set cust_email = 'kim@@thetoystore.com',cust_contact='sam roberts'
      where cust_id = '100000000006'
      

      DELLETE

      delete from customers
      where cust_id = '1000000006'#刪除此行,不過濾則刪除所有行
      #update刪除列
      #truncate刪除表
      

      添加刪除列&&表

      添加表

      /*添加表時為防止覆蓋,應刪除表后再進行添加*/
      create table orderitems
      (order_num integer not null,
      order_item integer not null,
      prod_id char(10) not null,
      quantity integer not null default 1,#設置quantity列下的行默認值為1
      item_price decimal(8,2) not null);#not null即不允許填入null,默認可填入null,只有為 not null的列方可為主鍵及唯一標識
      
      /*add列*/
      alter table vendors
      add vend_phone char (20);
      /*del列*/
      alter table vendors
      drop column vend_phone;#此操作不可逆
      /*刪除表*/
      drop table custcopy;#此操作不可逆
      

      視圖

      create view#創(chuàng)建視圖
      drop view 視圖名#刪除視圖
      /*以視圖簡化聯(lián)結(jié),創(chuàng)建視圖*/
      create view productcustomers as
      select cust_name , cust_contact,prod_id
      from customers,orders,orderitems
      where customers.cust_id=orders.cust_id
      and orderitems.order_num=orders.order_num;
      /*可對視圖采取與表相同的查詢操作*/
      select *
      from productcustomers;
      /*一個視圖過濾查詢例子*/
      create view customeremaillist as
      select cust_id, cust_name,cust_email
      from customers
      where cust_email is not null;#返回查詢中所有cust_email不為空的,并將其添加到視圖中
      /*視圖計算字段例子*/
      create view orderitemsexpanded as 
      select order_num,prod_id,quantity,item_price,quantity*item_price,quantity*item_price as
      expanded_price
      from orderitems
      

      事務管理

      /*撤銷整體*/
      start transaction ;
      -- 標識事務處理塊,塊中內(nèi)容未執(zhí)行完則整體撤銷
      /*撤銷部分操作*/
      savepoint delete1;#標識
      rollback to delete1;返回標識delete1
      
      

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多