2019/0318/SQL DDL & 伺服器 自學基礎

SQL

新增修改刪除只能一次對一個資料表作動作

如果要對很多個資料表作動作就要key很多次 (查詢則可以一次動很多資料表

資料定義語言 ( 不涉及資料本身 ( 容易被駭客攻擊

--create
create database 教務系統2
go

use 教務系統2
go

TSQL GO 批次處理 GO前先處理不是逐行

USE MYSTEAM 就是切換到 MYSTEAM 資料庫

邏輯群寫完 也可以寫個 GO

GUI 方式

資料庫 右鍵 新增資料表

資料類型 值域完整性

bigint 64位元整數

nvarchar 字元 不分中文字元(一般中文會佔2字元

char 字元 如果留10個 那資料不足十字元也會佔10個

varchar 限制最大字元 沒限制就根據資料大小

text 快被淘汰了 ( 備註欄 )

image ( 存2元編碼 )

底層資料結構B TREE

資料型態長度固定 效率會比較快

command line

create database / table

table 資料表專用名詞

--create
create database 教務系統2
go

use 教務系統2
go
---------------------------------------
create table 課程(
	課程編號 char(5) not null primary key,
	名稱  nvarchar(30) not null,
	學分 int not null
)

骯票特咖任 - > 計算欄位 compute 與 預設值 DEFAULT

--default值與計算欄位的定義
create table 員工(
	身分證字號 char(10) primary key,
	姓名 nvarchar(30) not null,
	城市 nvarchar(3) default '高雄市',
	街道 nvarchar(50),
	電話 varchar(16) not null,
	薪水 money not null default 23000,
	保險 money not null,
	扣稅 as 薪水*0.05,      -- 計算欄位
	淨所得 as 薪水-保險-薪水*0.05
)

流水編號

可看出哪裡被刪除過

且可定義

--identity唯一識別值(流水號)
create table 教授(
	流水編號 bigint identity,
	--流水編號 bigint identity(1000,50),
	教授編號 char(4) primary key,
	職稱 nvarchar(4) not null,
	科系 varchar(5) not null,
	身分證字號 char(10) not null
)

約束一定要有值且不能重複 primary key

一定層度上有某種約束

約束 (型態)

----------Constraints (條件約束)----------
--primary key constraints
create table 訂單明細(
	訂單編號 char(10) not null,
	商品編號 char(5) not null,
	價格 money not null,
	數量 money not null
)

定義的層級

教授編號 char(4) primary key

( 欄位層級 )

primary key(訂單編號,商品編號)

( 資料表層級 ) 組合兩鍵當作組合索引鍵

條件式約束 check constraints

create table 商品(
	商品編號 char(5) not null primary key,
	定價 money not null check(定價>=0),)

將條件式約束取名稱

庫存數量 int not null constraint check_庫存量不小於1  check(庫存數量>=0),

unique constraints 唯一條件約束

選擇主鍵原則

重籍索引長度不會變動 效能會比 長度會變動好

具有代表性

交替鍵 具有主鍵特性 不能重複 但因為不是主鍵所以准許null

--unique constraints
create table 會員資料(
	會員編號 char(8) primary key, 
	姓名 nvarchar(20) not null,
	身分證字號 char(10) unique,
	帳號 varchar(20) not null unique,
	密碼 varchar(20) not null
)

foreign key constraints 限制關聯

資料庫圖表

--foreign key constraints
create table 班級(
	學號 char(4),
	課程編號 char(5),
	教授編號 char(4),
	上課時間 datetime,
	教室 varchar(8),
	primary key(學號,課程編號,教授編號),
	foreign key(學號) references 學生(學號) on delete no action on update no action,
	foreign key(課程編號) references 課程(課程編號)  on delete cascade on update cascade,
	foreign key(教授編號) references 教授(教授編號)  on delete cascade on update no action
)
on delete no action on update no action
不可刪除 不可更新
on delete cascade on update cascade

on delete cascade on update no action

級聯反應(cascade):它指一系列連續事件,並且前面一種事件能激發後面一種事件

查看 關聯約資料庫圖表 右鍵新增資料圖表

修改 alter table 資料表資料定義

--alter table
alter table 教授
	 add foreign key(身分證字號) references 員工(身分證字號)

create table 訂單(
	訂單編號 char(10) not null primary key,
	訂單日期 datetime
)

alter table 訂單明細
	add foreign key(訂單編號) references 訂單(訂單編號)
go 
alter table 訂單明細
	add foreign key(商品編號) references 商品(商品編號)


--增加一個欄位
alter table 課程
	add 備註 nvarchar(max) not null
	go
--修改欄位
alter table 課程
	alter column 備註 nvarchar(50) 

完整動作 要修改訂單 增加欄位會員編號 1.要讓資料型態相同 原因是關聯KEY

GO 執行完後 才能下 關聯KEY


alter table 訂單
	add 會員編號 char(8) not null
go

alter table 訂單
	add foreign key(會員編號) references 會員資料(會員編號)

增加 必須是 修改式增加連原始屬性也必須KEY IN

刪除 增加 修改 相關約束 都必須用他的 NICK NAME

alter table 商品
	drop constraint CK__商品__定價__31EC6D26

alter table 課程
	drop column 備註


drop table 訂單 

column 欄位的意思

DROP 刪除

要考慮參考完整性

伺服器 網路架構資料夾權限

重點應該是

伺服器自身使用者(個人/群組)權限建立清楚

開放網路資料共用即可

遠志、石菖蒲、枸杞、黃耆、決明子、菊花、荷葉、秦艽

Last updated