資料庫ch3 — Introduction to SQL

慈慈
7 min readOct 26, 2020

--

(3–1~3–3)

3–1 Overview of The SQL Query Language

DDL(data-defination language)
提供relation新增、修改、刪除等指令

DML(data-manipulation language)
提供data新增、修改、刪除等指令

integrity
由DDL提供 用來確保資料的正確性、可靠性

view definition
由DDL提供 定義檢視表

transaction control
可控制交易的開始&結束

embedded SQL and dynamic SQL
定義SQL怎麼嵌入一般的程式語言

authorization
由DDL提供 包含設定資料存取權限

3–2 Data Definition Language

基本類型

char(n)
字串 長度固定為n(多的就補空白字元)

varchar(n)
字串 長度剛剛好(是動態的) 最長是n

-> char&varchar在比較的時候
通常會先補空白字元 直到長度相等
但不一定 要看系統

int
整數 長度為4位元

smallint
整數 長度為2位元

numeric(p, d)
小數 p位整數 d位小數

real, double precision
近似浮點數 精準度跟機器有關

float(n)
浮點數 精準到n位數

null
空值 代表不知道、不確定、不存在

Create Table Constructor

create table instructor(
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2));

instructor:relation的名稱
ID, name, dept_name, salary:attribute
char, archer, numeric:attribute的data type

Integrity Constraints in Create Table

create table instructor(
ID char(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2),
primary key(ID),
foreign key(dept_name) references department);

primary key(ID):具有唯一性 也要求not null
可以有很多attribute

foreign key(dept_name) references department
foreign key是dept_name 參照的是department
一個relation 可以有很多foreign key
前面加括號 後面不加

not null:不能是null

Updates to Tables

insert

//正確範例
insert into
instructor
values('10211', 'Smith', 'Biology', null);
//出現錯誤 上面有說name是not null
insert into instructor
values('10211', null, 'Biology', 66000);

字串要記得加單引號

delete vs drop

delete from student;

把student這個relation裡的所有tuple刪除
!delete後面不會有attribute!

drop table student;

把student這個relation裡的所有tuple刪除
除此之外 還把整個架構都刪掉

alter

alter table instructor add year int;

alter table relation名稱 add attribute data type
在instructor 新增year這個attribute 它的資料型態是int

alter table instructor drop year;

把year這個attribute刪除

3–3 Basic Query Structure

單一關聯查詢(from一個relation)

select & from

select name
from instructor;
select all name
from instructor;

把instructor裡的所有name都找出來
預設是all 可寫可不寫
兩個結果一樣 (如果name都不重複)

select distinct dept_name
from instructor;

dept_name有可能重複 但我希望我的結果不重複
就在select 後面加上distinct即可

select ID, name, salary/12
from instructor;

可以select多的attributes
也可以在select裡面做運算 !不會改到原本資料庫的內容!

where

select name
from instructor
where dept_name = 'Comp. Sci.' and salary > 70000

where用來過濾資料

邏輯運算子 and、or、not
比較運算子 <<=>>==<>(不等於)

多關聯查詢(from多個relation)

select name, instructor.dept_name, building
from instructor, department
where instructor.dept_name = department.dept_name;

若from的relation有相同的attributes 就需要清楚標明

語法順序為 select -> from -> where
執行順序為 from -> where -> select

natural join

select name, course_id
from instructor, teaches
where instructor.ID = teaches.ID;
select name, course_id
from instructor natural join teaches;

這兩個是一樣的 (因為instructor&teaches 只有ID是一樣的)
//下面有圖

select name, title
from (instructor natural join teaches) join course using (course_id);

join…using…:用來明確的表示要結合的attribute

隨堂測驗

題:Find the names of all instructors who have taught some

course and the course_id

答:

select name, course_id
from instructor, teaches
where instructor.ID = teaches.ID

題:Find the names of all instructors in the Art department

who have taught some course and the course_id

答:

select name, course_id
from instructor, teaches
where instructor.dept_name = 'Art' and instructor.ID = teaches.ID

--

--

No responses yet