學習SQL資料庫第二章

2022-06-21 22:12:17 字數 4674 閱讀 2813

一、select 語句基礎

1.查詢指定列:select 關鍵字

--語法:

--select 《列名》, ... -- 希望查詢列的名稱

--from 《表名》 -- 指定選取資料的表

-- 從 shohin 中取 3 列

select shohin_id, shohin_mei, hanbai_tanka  -- 列的順序可以任意指定,逗號(「,」)分隔,查詢結果的順序和 select 子句中的順序相同

from shohin;

2.查詢表的所有列:星號(*)

--語法

--select * -- 星號(*)代表所有列

--from 《表名》;

【備註】使用星號(*)的話就無法設定列的顯示順序

3.為列設定別名:as 關鍵字

select shohin_id as id, shohin_mei as name, shiire_tanka price

from shohin; -- 不用 as 關鍵字也可以

select shohin_id as "編號", shohin_mei as '名稱', shiire_tanka '**'

from shohin; -- 設定漢語別名:加上雙引號(")或單引號(')

4.常數的查詢

select '產品' as product, -- '產品':字串常數

38 as price, -- 38:數字常數

'2016-09-30' as '生產日期' -- '2009-02-24':日期常數

【備註】字串和日期使用單引號(')。

5.從結果中刪除重複行:distinct

原圖(1)

select distinct shohin_bunrui from dbo.shohin;  -

使用 distinct 移除 shohin_bunrui 列中的重複資料

(2)distinct 對 null 型別的處理:存在多條 null 值的行時,會結合為一條 null 資料。

select distinct shiire_tanka from dbo.shohin;

(3)多列之前使用 distinct

select distinct shohin_bunrui, torokubi

from dbo.shohin

distinct 會將多個列的資料進行組合,將重複的資料結合為一條。

【注意】distinct 關鍵字只能用在第乙個列名之前。

6.篩選記錄:where

where 子句中可以指定「某一列的值和這個字串相等」或者「某一列的值大於這個數字」等條件,找出只符合該條件的記錄。

--語法:

--select 《列名》, ...

--from 《表名》

--where 《條件表示式》;

select shohin_id, shohin_mei, shohin_bunrui

from dbo.shohin

where shohin_bunrui = '衣服';  -- shohin_bunrui = '衣服':為條件表示式

選取行之後,再輸出列

【備註】where 子句:首先通過該子句查詢出符合指定條件的記錄,再選取出 select 語句指定的列。

【注意】sql 子句的書寫格式是固定的,不能隨意更改。如 where 子句必須緊跟在 from 子句後。

7.注釋的寫法

注釋對於 sql 的執行沒有任何影響。

-- 單行注釋

/*多行注釋

*/二、算術運算子和比較運算子

1.算術運算子

select shohin_mei, hanbai_tanka, hanbai_tanka * 2 as 'hanbai_tanka_x2'

from dbo.shohin;

圖 商品單價的兩倍

四則算術運算子

含義 運算子

加法 +

減法 -

乘法 *

除法 /

括號(「(」「)」)可以提高表示式的優先順序。

2.需要注意 null

select 5 + null, 10 - null, 1 * null, 4 / null, null / 9;

【備註】所有包含 null 的計算,結果肯定為 null。

3.比較運算子

比較運算子

運算子 含義

= 相等

<> 不等

= 大於等於

大於<= 小於等於

< 小於

--示例1:

select shohin_mei, shohin_bunrui

from dbo.shohin

where hanbai_tanka = 500;

選取 hanbai_tanka 列為 500 的記錄

--示例2

select shohin_mei, shohin_bunrui

from dbo.shohin

where hanbai_tanka <> 500;

--示例3

select shohin_mei, shohin_bunrui

from dbo.shohin

where hanbai_tanka != 500;

選取 hanbai_tanka 列的值不是 500 的記錄

--示例4

select *

from dbo.shohin

where hanbai_tanka - shiire_tanka >= 500;

3.對字串使用不等號時的注意事項

建立表原圖

--示例:選取出大於'2'的資料的 select 語句

select *

from dbo.chars

where chr > '2';

【注意】chr 為字串型別,對字串型別的資料進行大小比較時,跟數字不一樣。

4.不能對 null 使用比較運算子

複製**

--示例1:

select shohin_mei, shiire_tanka

from dbo.shohin

where shiire_tanka = null; --錯誤的 select 語句

--示例2

select shohin_mei, shiire_tanka

from dbo.shohin

where shiire_tanka is null; --選取 null 的記錄

--示例3

select shohin_mei, shiire_tanka

from dbo.shohin

where shiire_tanka is not null; --選取不為 null 的記錄

複製**

【注意】希望選取 null 記錄時,使用 is null;希望選取不是 null 的記錄時,使用 is not null。

三、邏輯運算子

1.not 運算子:取反

--示例:

select *

from dbo.shohin

where not hanbai_tanka >= 1000;  --等價於 hanbai_tanka < 1000

取 hanbai_tanka 列不大於 1000 的記錄(hanbai_tanka < 1000)

2.and 運算子和 or 運算子

and 運算子:並且,在兩側的查詢條件都成立時整個查詢條件才成立。

or 運算子:在兩側的查詢條件就算只有乙個成立時整個查詢條件都成立。

--示例

select shohin_mei, shiire_tanka

from dbo.shohin

where shohin_bunrui = '廚房用具'

and hanbai_tanka >= 3000;

select shohin_mei, shiire_tanka

from dbo.shohin

where shohin_bunrui = '廚房用具'

or hanbai_tanka >= 3000;

【備註】多個查詢條件進行組合時,需要使用 and 運算子或者 or 運算子。

2.通過括號進行強化

複製**

--示例1

select shohin_mei,

shohin_bunrui,

torokubi

from dbo.shohin

where shohin_bunrui = '辦公用品'

and torokubi = '2009-09-11'

or torokubi = '2009-09-20';

複製**

複製**

--示例2

select shohin_mei,

shohin_bunrui,

torokubi

from dbo.shohin

where shohin_bunrui = '辦公用品'

and (torokubi = '2009-09-11'

or torokubi = '2009-09-20');

複製**

資料庫第二章

關係模式 型 是對關係的描述 是靜態 穩定的 關係 值 關係模式在某一時刻的狀態或內容 動態的 不斷變化的 關係模式和關係往往籠統稱為關係,通過上下文加以區別 關聯式資料庫的型與值 關聯式資料庫的型 關聯式資料庫模式,是對關聯式資料庫的描述 關聯式資料庫的值 關係模式在某一時刻對應的關係的集合,通常...

資料庫 第二章學習總結

1.笛卡爾積的表示方法 笛卡爾積可表示為一張二維表 表中的每行對應乙個元組,表中的每列對應乙個域 2.元組的定義 關係中的每個元素是關係中的元組,通常用t表示。3.關係的表示 關係也是乙個二維表,表的每行對應乙個元組,表的每列對應乙個域 4.屬性 關係中不同列可以對應相同的域為了加以區分,必須對每列...

第二章 SQL Server資料庫

一,變數分類 區域性變數。全域性變數 區域性變數 宣告變數 declare 變數名 資料型別 declare id char 10 宣告乙個長度為個字元的變數id declare age int 宣告乙個存放職員年齡的整型變數 變數賦值 set 變數名 值 用於普通的賦值 set age 20 se...