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

## SQL

![](/files/-L_l6yYfkkzHEfZuezme)

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

如果要對很多個資料表作動作就要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
)
```

### 流水編號&#x20;

可看出哪裡被刪除過

且可定義

```
--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&#x20;

( 欄位層級 )

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

( 資料表層級 ) 組合兩鍵當作組合索引鍵&#x20;

### 條件式約束 check constraints

![](/files/-LaDgRWqYpTVZ7mY8bRn)

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

#### 將條件式約束取名稱

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

### unique constraints 唯一條件約束

{% embed url="<https://docs.microsoft.com/zh-tw/sql/relational-databases/tables/unique-constraints-and-check-constraints?view=sql-server-2017>" %}

#### 選擇主鍵原則

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

具有代表性

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

```
--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 限制關聯

資料庫圖表&#x20;

```
--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

```

&#x20;**級聯反應**（cascade）：它指一系列連續事件，並且前面一種事件能激發後面一種事件

{% embed url="<http://cht.a-hospital.com/w/%E7%BA%A7%E8%81%94%E5%8F%8D%E5%BA%94>" %}

{% embed url="<http://fecbob.pixnet.net/blog/post/40666701-mysql%E5%A4%96%E9%8D%B5%E8%A8%AD%E7%BD%AE%E4%B8%AD%E7%9A%84%E7%9A%84-cascade%E3%80%81no-action%E3%80%81restrict%E3%80%81>" %}

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

![](/files/-LaDrxkaJ1BiIgm5C79k)

### 修改 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 欄位的意思

![](/files/-LaDw5fc3Lu_UhTmceet)

### DROP 刪除

要考慮參考完整性

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

![](/files/-LaEKmwADwE_lshC-680)

#### 重點應該是

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

開放網路資料共用即可

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

{% embed url="<https://www.oschina.net/news/79567/apache-vs-nginx-vs-node-js>" %}

{% embed url="<https://cnodejs.org/topic/5411035ea0c965223b99d7d1>" %}

{% embed url="<https://medium.com/%E5%89%8D%E7%AB%AF%E5%A3%B9%E5%85%A9%E4%B8%89%E4%BA%8B/%E8%81%8A%E8%81%8A%E9%97%9C%E6%96%BC%E5%9F%BA%E6%9C%AC%E7%9A%84-nginx-reverse-proxies-and-nodejs-express-web-server-2a1c8e7e7de1>" %}

{% embed url="<https://www.zhihu.com/question/20757944>" %}

{% embed url="<https://www.zhihu.com/question/57688289>" %}

{% embed url="<https://kknews.cc/zh-tw/tech/652v3v.html>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://johch3n611u.gitbook.io/c50108/ju-li-cheng-bei/201903/untitled-7.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
