sql初學者筆記 語法基礎

来源:https://www.cnblogs.com/MR---Zhao/archive/2020/06/24/13190123.html
-Advertisement-
Play Games

常見註釋 -- 很少支持 #行內註釋 /**/段落註釋 基礎語法 SELECT 檢索數據 語法 作用 例子 釋義 select 查找列,並返回行 select prod_name from products;#可使用,分隔列名來查找多個列。 查找prod_name列,並返回其下的所有行,在produ ...


常見註釋

  • -- 很少支持
  • 行內註釋

  • /**/段落註釋

基礎語法

SELECT

檢索數據

語法 作用 例子 釋義
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表中。

排序檢索數據

語法 作用 例子 釋義
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僅對其前的列有效;

過濾數據

語法 作用 例子 釋義
where 在客戶端過濾數據 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;
#過濾字元串不區分大小寫
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範圍內的的的所有行
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的優先順序比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() 去除右邊、左邊空格

創建計算欄位

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

使用函數

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
#返回長度(數目),也可對列表中特定值進行計數

分組

select  vend_id,count(*) as num_prods from  products #對vend_id每行進行計數
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'));
#由內而外,哈哈
等效於:
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

聯結(返回不在同一個表中的行)

/*等值語法*/
select vend_name, prod_name,prod_price
from vendors,products
where vendors.vend_id=products.vend_id;#此處過濾聯結條件。
#如沒有聯結條件過濾,將檢索出“笛卡爾積”:表1行數*表2行數
/*規範語法*/
select vend_name, prod_name,prod_price
from vendors inner join products
on vendors.vend_id=products.vend_id
自聯結(比子查詢更快)
/*子查詢*/
select cust_id, cust_name, cust_contact
from customers
where cust_name = (select cust_name from customers where cust_contact ='jim jones');
/*自聯結*/
select c1.cust_id, c1.cust_name,c1.cust_contact
from customers as c1,customers as c2#不以別名進行會引發錯誤
where c1.cust_name=c2.cust_name and c2.cust_contact='jim jones'#聯結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中每個查詢必須包含相同的列、表達式、或聚集函數
from customers
where cust_name ='wascals'
order by cust_name;#不能分別對每條union指定不同的排序

INSERT

依賴於次序的插入

/*在得知列的次序後才可使用此方式添加,若發生了列的次序變動此添加方式將不安全*/
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#創建視圖
drop view 視圖名#刪除視圖
/*以視圖簡化聯結,創建視圖*/
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 ;
-- 標識事務處理塊,塊中內容未執行完則整體撤銷
/*撤銷部分操作*/
savepoint delete1;#標識
rollback to delete1;返回標識delete1


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ls list(列表) 列表目錄文件 例子:ls / 列根/目錄下文件與內容 -l(long)長格式 ll是個別名alias ll='ls -l --color=auto' -a顯示所有文件和文件夾,(包含隱藏文件).預設點開頭的文件都是隱藏的。 -h 要與1一起用,顯示文件大小(k,M,G)單位 ...
  • 前言 最近項目上需要用到搜索引擎,由於之前自己沒有瞭解過,所以整理了一下搜索引擎的相關概念知識。 正文 想查數據就免不了搜索,搜索就離不開搜索引擎,百度、谷歌都是一個非常龐大複雜的搜索引擎,他們幾乎索引了互聯網上開放的所有網頁和數據。然而對於我們自己的業務數據來說,肯定就沒必要用這麼複雜的技術了,如 ...
  • Linux下有三個命令:ls、grep、wc。通過這三個命令的組合可以統計目錄下文件及文件夾的個數。 統計當前目錄下文件的個數(不包括目錄) $ ls -l | grep "^-" | wc -l 統計當前目錄下文件的個數(包括子目錄) $ ls -lR| grep "^-" | wc -l 查看某 ...
  • 實操題 網路管理 1、在eNSP中使用S5700交換機進行配置,通過一條命令劃分vlan2、vlan3、vlan1004,通過埠組的方式配置埠1-5為access模式,並添加至vlan2中。配置埠10為trunk模式,並放行vlan3。創建三層vlan2,配置IP地址為:172.16.2.1/ ...
  • 在我們將U盤插入裝有CentOS的系統時,經常會出現如圖所示的錯誤提示。這是因為linux系統並不能相容NTFS的文件系統。其解決方法如下(建議先進入root模式): **1、首先下載“ntfs-3g”**(NTFS-3G 是一個開源的軟體,可以實現 Linux、Free BSD、Mac OSX、N ...
  • 1. 概念 自旋鎖的目的是在短期間內進行輕量級的鎖定,解決對某項共用資源的互斥使用,在等待鎖重新可用期間進行自旋,所以自旋鎖不應該被持有時間過長,如果需要長時間鎖定的話,推薦使用信號量。實際操作的數據結構如下: 2. 獲取鎖 最終執行的代碼是體繫結構相關的自旋鎖實現:arch_spin_lock。 ...
  • 實驗一 安裝OpenShift1.1 前置準備[student@workstation ~]$ lab review-install setup1.2 配置規劃OpenShift集群有三個節點:master.lab.example.com:OpenShift master節點,是一個不可調度pod的 ...
  • 鍵盤俠Linux乾貨| ELK(Elasticsearch + Logstash + Kibana) 搭建教程 全網最簡單 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...