1. 程式人生 > >關系數據庫-----SQL標準語言

關系數據庫-----SQL標準語言

限制 index des 更新 AD -- 降序排序 SQ tab

關系數據庫三級模式結構

  外模式:視圖,基本表導出的表,數據庫中只存放視圖的定義而不存放視圖對應的數據。

  模式:基本表

  內模式:存儲文件

數據定義

操作對象 創建 刪除 修改
模式 create schema drop schema
create table drop table alter table
視圖 create view drop view
索引 create index drop index alter index

    模式的定義與刪除

    要創建模式,調用該命令的用戶必須擁有數據庫管理員權限,或者獲得了數據庫管理員授予的create schema 權限

    語句: create schema 模式名 authorization 用戶名

    刪除模式,drop schema 模式名 <cascade| restrict>

        cascade 級聯 :刪除模式的同時把該模式下的數據庫對象全部刪除、

        restrict 限制:如果該模式下定義有表,視圖,則拒絕該刪除語句的執行。只有當該模式下沒有任何下屬對象時才能執行drop schema語句

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

    定義 create table 表名(<字段1><數據類型>[列級完整性約束條件],...,[<表級完整性約束條件>])

    修改基本表 alter table<表名>

    刪除 drop table <表名>[restric|cascade]

      cascade:該表的刪除沒有限制條件

      restrict:該表的刪除有限制條件,要刪除的基本表不能被其他表的約束條件所引用,不能有視圖,觸發器,存儲過程或函數 (默認情況)

  索引的建立與刪除

    索引可以加速數據庫查詢,但要占用一定存儲空間。基本表更新時,索引要進行相應維護。

    建立索引create [unique][cluster] index <索引名> on 表名(<列名>[<次序>][,<列名>[<次序>]])

    修改索引 alter index <舊索引名> rename to <新索引>

    刪除索引 drop index <索引名>

數據查詢

  select [all | distinct] <目標列表達式>[,目標列表達式] ...

  from <表名 or 視圖名>[,<表名 or 視圖名>,...] | (select 語句) [as]<別名>

  [where <條件表達式>]

  [group by <列名1> [having <條件表達式>]]

  [oder by <列名2>[ASC|DESC]];

根據where子句的條件表達式從from 子句指定的基本表,視圖或派生表中找出滿足條件的元組,再按照select 子句中的目標列表達式選出元組中的屬性值形成結果表。

有group by 子句,則將結果按列名1 的值進行分組,該屬性列值相等的元組為一個組

oder by 子句 按列名2事務值的升序或降序排序

  單表查詢

    指定列 select <列名1>,<列名2>,... from tablename;

    全部列 select * from tablename;

  選擇表中的若幹元組

    消除取值重復的行 select distinct 列名 ,... from tablename;

    滿足一定條件的元組

查詢條件 謂詞
比較 =,>,<,>=,<=,!=,<>,!=,!>,!<,;not +上述比較運算符
確定範圍 between (下限) and (上限), not between and
確定集合 in , not in
字符匹配 like , not like
空值 is null , is not null
多重條件 and , or , not

  聚集函數

count(*) t統計元組個數
count( [distinct|all ] <列名>) t統計一列中的值的個數
sum( [distinct|all ] <列名>) j計算一列值的總和
avg ( [distinct|all ] <列名>) j計算一列值的平均值
max ( [distinct|all ] <列名>) y一列的最大值
min ( [distinct|all ] <列名>) y一列的最小值

          all為默認的

where語句裏不能有聚集函數作為條件表達式,having 裏面可以有

關系數據庫-----SQL標準語言