1. 程式人生 > >結構化查詢語句

結構化查詢語句

比較運算符 like 比較 mysql 類型 sum 影響 star 結束

By TreeDream

基本表的定義,修改,刪除簡單查詢單表查詢連接查詢等值與非等值查詢外連接查詢復合條件查詢自身連接查詢嵌套查詢數據更新插入數據修改數據刪除數據視圖創建視圖查詢視圖更新視圖刪除視圖

基本表的定義,修改,刪除

建表考慮列名,數據類型,列級完整性,表級完整性(參照性約束)

create table Salvaging
(
    prj_num char(8) primary key,
    prj_name varchar(50),
    start_date datetime,
    end_date datetime,
    prj_status bit,
);
?
create table Stock
(
    mat_num char(8) primary key,
    mat_name varchar(50) not null,
    speci varchar(20) not null,
    warehouse char(20),
    amount int,
    unit decimal(18,2),
    total as(amount*unit),
    check(mat_num like ‘[m][0-9][0-9][0-9]‘),
);
?
create table Out_stock
(
    prj_num char(8),
    mat_num char(8),
    amount int,
    get_date datetime default getdate(),
    department char(20),
    primary key (prj_num,mat_num),
    foreign key(prj_num) references Salvaging(prj_num),
    foreign key(mat_num) references Stock(mat_num),
);
?
alter table Salvaging add prj_director varchar(10);
alter table Salvaging drop column prj_director
?
drop table Salvaging

簡單查詢

單表查詢

select prj_name 項目名稱,start_date 開始日期,end_date 結束日期,DATEDIFF(day,start_date,end_date) 搶修天數
from Salvaging

select *
from Stock
where unit>=50 and unit<=100 --條件

in集合

select mat_num,speci,amount,warehouse
from Stock
where warehouse not in (‘供電局1#倉庫‘,‘供電局2#倉庫‘)

字符匹配

select *
from Stock
where mat_name like ‘__絕緣%‘

NULL值(is)

select *
from Stock
where unit is null

排序:只能對最後的查詢結果排序

select *
from Stock
where mat_name=‘護套絕緣電線‘
order by unit desc  --默認從低到高(asc),desc從高到底

聚集函數

select MAX(amount),min(amount),avg(amount)
from Out_stock
where mat_num = ‘m001‘

分組

select prj_num 項目號,count(*) 物資種類
from Out_stock
group by prj_num

select prj_num 項目號,count(*) 物資種類
from Out_stock
group by prj_num
having count(*)>=2

連接查詢

等值與非等值查詢

等值 與非等值根據連接謂詞

廣義笛卡爾積不帶謂詞,沒有意義;

自然連接:在等值連接的基礎上,去除重復列

select Salvaging.prj_num,Salvaging.prj_name,mat_num,amount,get_date,end_date,department
from Salvaging,Out_stock
where Salvaging.prj_num = Out_stock.prj_num

外連接查詢

連接操作中,如果有一個關系沒有與之對應,就不會有輸出,但是也丟失了另一個關系的基本情況,解決方案是外連接

外連接:左外連接,右外連接,全外連接

select Salvaging.prj_num,Salvaging.prj_name,mat_num,amount,get_date,end_date,department
from Salvaging left outer join Out_stock on (Salvaging.prj_num = Out_stock.prj_num)

復合條件查詢

select distinct Salvaging.prj_num,Salvaging.prj_name
from Salvaging,Out_stock,Stock
where Salvaging.prj_num = Out_stock.prj_num and Out_stock.mat_num = Stock.mat_num and Stock.mat_name=‘護套絕緣電線‘

自身連接查詢

select A.prj_num
from Out_stock A,Out_stock B
where A.prj_num = B.prj_num and A.mat_num = ‘m001‘ and B.mat_num = ‘m002‘

嵌套查詢

帶謂詞in的嵌套查詢(子查詢往往是一個集合)

select Stock.mat_name,speci,amount
from Stock
where warehouse in (        -- = 亦可
        select warehouse
        from Stock
        where speci = ‘BVV-120‘ and mat_name = ‘護套絕緣電線‘
    )

/*
select Stock.mat_num,Stock.mat_name
from Salvaging,Out_stock,Stock
where Salvaging.prj_name=‘觀瀾站光纜搶修‘ and Salvaging.prj_num = Out_stock.prj_num and Out_stock.mat_num = Stock.mat_num
*/
?
select mat_num,mat_name
from Stock
where mat_num in (
    select mat_num
    from Out_stock
    where prj_num in (
        select prj_num
        from Salvaging
        where prj_name = ‘觀瀾站光纜搶修‘
    )
)

帶比較運算符的嵌套查詢

select mat_num,mat_name
from Stock s1
where amount > (
    select avg(amount) 
    from Stock s2
    where s2.warehouse = s1.warehouse
)

帶any或all謂詞的嵌套查詢

select mat_name,speci,amount
from Stock
where warehouse <> ‘供電局1#倉庫‘ and amount < ALL (
    select amount
    from stock
    where warehouse = ‘供電局1#倉庫‘
)

帶exists謂詞的嵌套查詢

select prj_name
from Salvaging
where exists (
    select*
    from Out_stock
    where prj_num = Salvaging.prj_num and mat_num = ‘m001‘
)

sql中沒有全稱量詞,把全稱量詞轉換為存在量詞

--查詢被所有工程使用過了的物資——沒有一個工程沒有使用過他
select mat_name,speci
from Stock
where not exists (
    select*
    from Salvaging
    where not exists (
        select*
        from Out_stock
        where mat_num = Stock.mat_num and prj_num = Salvaging.prj_num
    )
)

數據更新

插入數據

insert
into Salvaging
values (‘20110011‘,‘觀瀾站電纜接地搶修‘,‘2011-2-3 0:00:00‘,‘2011-2-5 12:00:00‘,1)

插入查詢結果

insert
into Prj_cost
select prj_num,sum(out_stock.amount*unit)
from Out_stock,Stock
where Out_stock.mat_num = stock.mat_num
group by prj_num

修改數據

update Stock
set unit = 44.5
where mat_num = ‘m020‘

刪除數據

delete
from Out_stock
where prj_num = ‘20110001‘ and mat_num = ‘m001‘

視圖

  • 視圖是數據庫數據的特定子集。可以禁止所有用戶訪問數據庫表,而要求用戶只能通過視圖操作數據,這種方法可以保護用戶和應用程序不受某些數據庫修改的影響。

  • 視圖是抽象的,他在使用時,從表裏提取出數據,形成虛的表。 不過對他的操作有很多的限制 。

創建視圖

create view s1_stock
as
select mat_num,mat_name,speci,amount,unit
from Stock
where warehouse = ‘供電局1#倉庫‘

查詢視圖

--像基本表一樣查詢視圖
select *
from s1_stock

更新視圖

--insert,delete
update s1_stock
set amount = 100
where mat_num = ‘m001‘

刪除視圖

drop view s1_stock

結構化查詢語句