1. 程式人生 > >資料庫系統概論複習總結3 --- 第二章關係資料庫標準語言SQL

資料庫系統概論複習總結3 --- 第二章關係資料庫標準語言SQL

3.1 SQL 概念

===結構化查詢語句

3.1.2SQL的特點

1、綜合統一

2、高度非過程化

3、面向集合的操作方式

4、以同一種語法結構提供多種使用方式

3.1.3 SQL的基本概念

支援SQL的關係資料庫管理系統同樣支援關係資料庫三級模式結構。

其中外模式包括若干檢視和部分基本表,資料庫模式包括若干基本表

內模式包括若干儲存檔案

基本表是本身獨立存在的表,在關係資料庫管理系統中一個關係對應一個基本表,一個或多個基本表對應一個儲存檔案,一個表可以帶若干索引,索引也存放在儲存檔案中。

儲存檔案的邏輯結構組成了關係資料庫的內模式、對使用者是隱藏的

檢視是從一個或幾個基本表中匯出的表,它本身不獨立儲存在資料庫中,即資料庫中只存放檢視的定義而不存放檢視對應的資料。

3.3 資料定義

3.3.1 模式的定義與刪除

1、定義模式

create schema <模式名> authorization<使用者名稱>

例:為使用者zhang建立一個模式test,並且在其中定義一個基本表 table1

create schema test authorization zhang

create table table1 (

col1 smallint,

col2 int,

col3 char(20),

col4 numeric(10,3),

col5 decimal(5,2)

);

2、刪除模式

drop schema <模式名><cascade|restrict>

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

1、定義基本表

create table < 表名> (列名 資料型別 列級完整性約束條件)

例:建立一個:“學生”表student

create table student(

Sno char(9) primary key,

Sname char(20) unique,

Ssex char(2),

Sage smallint,

Sdept char(20)

);

例:建立 一個“課程”表course

create table course (

cno char(4) primary key,

cname char(30) not null,

cpno char(4),

ccedit smallint,

foreign key (cpno) references course(cno)

);

2、資料型別

char(n),character(n)   長度為n的定長字串

varchar(n), charactervarying(n)最大長度為n的變長字串

clob 字串大物件

blob 二進位制大物件 

int , interger 長整數(4位元組)

smallint 短整數(2位元組)

bigint 大整數 (8位元組)

numberi (p,d) 定點數

decimal(p,dP 同numeric

real 取決於機器精度的單精度浮點數

double precision 取決於機器精度的雙精度浮點數

float(n) 可選精度的浮點數,至少為n位數字

boolean 邏輯布林量

date 日期

time 時間

timestamp 時間戳型別

intergval 時間間隔型別

4、修改基本表

alter table <表名> 

add column 新列名 資料型別完整性約束

drop column 列名 

drop constaint 完整性約束

alter column列名資料型別

例:向sudent表增加入學時間列,其資料型別為日期型

alter table student add column _entrance date;

例:將年齡的資料型別由字元型改為整數

alter table student alter column sage int;

5、刪除表

drop table < 表名> restrinct cascade可選擇

例: 刪除student表

DROP TABLE STUDENT CASECADE

3.3.3 索引的建立與刪除

1、建立索引

create [unique][cluster] index<索引名> on <表名>

例:為學生-課程資料庫中的student-coursr-sc三個表建立索引。其中student表按學號升序建立唯一索引,course按課程號升序建立唯一 索引,sc表按學號升序和課程號降序建立唯一索引

create unique index stusno on student(sno);

create unique index coucno on course(cno);

create unique index scno on sc(sno asc, cno desc);

2、修改索引

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

例;將sc表的scno索引名改為scsno

alter index scno rename to scsno;

3、刪除索引

drop index<索引名>

例:刪除student 表的stusname 索引

drop index stusname;

3.4 資料查詢

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

from <表名> 

[where <條件表示式>]

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

[order by[列名] [asc| decs]]

3.4.1 單表查詢

1、選擇表中若干列

例:查詢全班學生的學號與姓名

select sno, sname from student;

2、選擇表中的若干元組

(1)消除取值重複的行

例:查詢選修了課程的學生學號

select distinct  sno from sc 

( 2) 查詢滿足條件的元組

查詢條件表示式:

比較  = > < =>  =< != !=

確定範圍 between and , not between and

確定集合 in , not in

字元匹配 like not like 

空值is null, is not null

多值條件 and or not

例: 查詢計算機科學系全體學生的名單

select  sname from student 

where sdept = 'cs";

例: 查詢所有年齡在20歲以下的學生姓名及其年齡

select  sname, sage from student where age < 20;

例: 查詢年齡在20--30之間的學生的學號,系別和年齡

select sname,sdept, sage from sage between 20 and 30 ;

例:查詢年齡不在20--30之間的學生姓名,系別和年齡

select sname ,sdept , sage from student where not between 20 and 30

例: 查詢計算機科學系(cs),數學系(ma),資訊系(is)學生的姓名,性別

select sname ,ssex from student where in ('cs',"ma","is");

字元匹配 like not like 

萬用字元 % >=0長度_  代表任意長度

例:查詢學號為201215121 的學生的詳細資訊

select * from student where  sno like '201215121";  ===  select * from student where sn0  = "201215121";

例: 查詢姓"歐陽:且全名為三個漢字的學生的姓名

select *  sname from student  sname like ' 歐陽_';

例: 查詢所有姓“劉” 的學生的姓名,學號,和性別

select * from student  sname like '劉%';

注意: 如果使用者要查詢的字串本身就含有萬用字元%_,這就要使用escape來進行轉義

例: 查詢DB_Design課程的課程號和學分

select * from course where cname  like 'DB_Deign' escape '\';

3、order by 子句

使用者可以用order by 對查詢結果按照一個或多個屬性列的升序asc , desc 排列

不好意思,我要吃飯了,學校人多 --- 再敘