跳轉到內容

JET 資料庫/資料定義語言

來自華夏公益教科書,自由的教科書
JET 資料庫

簡介 · 建立和連線 · 資料型別 · 物件名稱和常量 · 資料完整性 · 安全性 · 資料定義語言 · 選擇 · 插入 · 更新 · 刪除 · 函式 · 檢視和儲存過程 · 操作工具 · 整合和遷移 · 設計工具

建立和刪除表

[編輯 | 編輯原始碼]

建立表

[編輯 | 編輯原始碼]

透過發出Create Table語句來建立表。該語句必須指定表名和表中的任何列。

Create Table T (
    a integer,
    b char(10)
)

刪除表

[編輯 | 編輯原始碼]

透過發出Drop Table語句來刪除表。

Drop Table T

更改表

[編輯 | 編輯原始碼]

可以透過發出一個或多個Alter Table語句來更改表。可以新增新列,刪除現有列,以及更改現有列。

Alter Table T Add Column c float
go
Alter Table T Drop Column c
go
Alter Table T Alter Column b varchar(20)
go

在 JET SQL 中有幾種建立主鍵的方法。可以在Create Table語句中使用Primary Key指令,如下所示

Create Table P1 (
    i1 int not null,
    c1 varchar(255),
    Primary Key(i1)
)

使用Constraint指令可以建立具有相同主鍵的相同表,無論是在Create Table語句中

Create Table P2 (
    i1 int not null,
    c1 varchar(255),
    Constraint PK_P2 Primary Key(i1)
)

還是在Alter Table語句中之後

Create Table P3 (
    i1 int not null,
    c1 varchar(255)
)
go
Alter Table P3 Add Constraint PK_P3 Primary Key(i1)
go

如果表的主鍵中只有一列,則可以將約束新增到列規範中

Create Table P4 (
    i1 int not null Constraint PK_P4 Primary Key,
    c1 varchar(255)
)

除最後一個示例外,上面的所有示例都支援主鍵中的多列,例如

Create Table P5 (
    i1 int not null,
    c1 varchar(20) not null,
    c2 varchar(255),
    Constraint PK_P5 Primary Key(i1, c1)
)

唯一約束

[編輯 | 編輯原始碼]

唯一約束可以以相同的方式新增,無論是在Create Table語句中(如下所示)還是在Alter Table語句中。

Create Table U1 (
    a int not null, 
    b varchar(20) not null, 
    c varchar(20) not null,
    Constraint U1_pk Primary Key (a),
    Constraint U1_uc Unique (b)
)
go

外部索引鍵約束

[編輯 | 編輯原始碼]

可以在Create Table語句中(如下所示)或透過Alter Table語句將外部索引鍵約束新增到表中。

Create Table F1 (
    a int not null, 
    b varchar(20) not null,
    c varchar(20) not null,
    Constraint F1_pk Primary Key (a, b)
)
go
Create Table F2 (
    i int not null,
    a int not null,
    b varchar(20) not null,
    Constraint F2_pk Primary Key (i),
    Constraint F2_fk1 Foreign Key (a, b) References F1 (a, b)
)
go

JET 4.0 引入了外部索引鍵的級聯更新和刪除。當以Update Cascade建立外部索引鍵時,如果引用列發生更改,則外部索引鍵也會更新。Delete Cascade導致在刪除引用行時刪除引用行,而Delete Set Null在刪除引用行時將外部索引鍵設定為 Null。

Create Table F5 (
    i int not null,
    a int not null,
    b varchar(20) not null,
    Constraint F5_pk Primary Key (i),
    Constraint F5_fk1 Foreign Key (a, b) References F3 (a, b)
        On Update Cascade On Delete Set Null
)
go

檢查約束

[編輯 | 編輯原始碼]

檢查約束可以以幾乎相同的方式新增。請注意,即使檢查約束可能僅與一個特定列相關,但約束是在表級別宣告的,而不是在列級別宣告的

Create Table F6 (
    i int not null,
    a char(1) not null,
    b decimal(15,2) not null,
    c decimal(15,2) not null,
    Constraint F6_pk Primary Key (i),
    Constraint F6_chk_a check (a in ('Y','N')),
    Constraint F6_chk_b check (b >= 0 And b <= 1000),
    Constraint F6_chk_c check (c <= (Select Sum(a) From F5))
)
go

表索引有助於提高對錶進行查詢的效能,包括其他語句(如更新、刪除和外部索引鍵驗證)中的隱式查詢。透過發出Create Index語句來建立表索引。

索引可以在每列中的值以升序ASC)或降序DESC)建立,即最小的值放在最前面或最大的值放在最前面。如果未指定,則索引將以每個索引列中的升序值建立。

以下語句建立了一個包含兩個索引的表。第一個索引僅覆蓋列b,但第二個索引同時覆蓋列cd

Create Table I1 (
    a int not null,
    b varchar(20),
    c varchar(20),
    d varchar(20),
    Constraint I1_pk Primary Key(a)
)
go
Create Index I1_idx1 On Table I1 (b)
go
Create Index I1_idx2 On Table I1 (c ASC, d DESC)
go

透過發出Drop Index語句可以刪除表索引。

Drop Index I1_idx1 On I1

唯一索引

[編輯 | 編輯原始碼]

通常,索引允許重複值。如果每行在索引列中必須具有唯一值,或在索引的列集中必須具有唯一的組合值,則可以將索引指定為唯一。這與新增唯一約束有類似的效果(實際上,這就是 JET 實現唯一約束的方式)。注意:空值不被視為值,因此如果允許唯一索引或唯一約束中的列為空值,則多行可能在該列中具有空值。

Create Table UI1 (
    a int not null, 
    b varchar(20) not null, 
    c varchar(20) not null,
    Constraint UI1_pk Primary Key (a)
)
go
Create Unique Index UI1_idx_ui On UI1 (c)
go

不允許空值

[編輯 | 編輯原始碼]

空值處理通常最好在表列上指定。但是,Create Index語句也支援一個選項,不允許索引列中出現任何空值。

Create Index T5_idx1 On T5(c2) With Disallow Null

忽略空值

[編輯 | 編輯原始碼]

可以完全將索引列中具有空值的行排除在索引之外,使索引在磁碟上的物理大小更小,從而加快搜索速度。

Create Index T5_idx2 On T5(c1) With Ignore Null

使用主鍵

[編輯 | 編輯原始碼]

可以透過使用特殊的With Primary選項建立索引來指定表的primaryKey列。

通常最好使用Primary Key約束指令建立主鍵,除非在主鍵列上建立索引時需要其他選項。一個這樣的例子可能是當主鍵中的一列或多列應該以降序而不是升序索引時,出於效能原因。

Create Table P6 (
    i1 int not null,
    c1 varchar(20) not null,
    c2 varchar(255)
)
go
Create Index P6_idx_pk On P6(i1 Desc) With Primary
go

刪除索引

[編輯 | 編輯原始碼]

刪除不再需要的索引也很簡單。指定索引名稱以及索引所在的表。

Drop Index T5_idx2 On T5
go

安全性

[編輯 | 編輯原始碼]

當多個數據庫使用者(以及可選的組)被新增到資料庫後,可以透過對單個物件授予撤銷許可權來限制這些使用者對資料庫的訪問許可權。

JET 支援 ANSI SQL 標準中以下基本表許可權(基本“CRUD”許可權 - Create、Read、Update、Delete)

選擇 從表中選擇資料
刪除 從表中刪除資料
插入 將新資料插入表中
更新 更新表中的現有資料
Grant Select on T1 to SalesGroup
go
Grant Select, Insert, Update on T1 to AccountsGroup
go
Revoke Update on T1 from AccountsGroup

此外,JET 還支援以下表許可權

所有許可權 一次性授予或撤銷所有許可權
刪除 刪除表
SelectSecurity 查看錶上的許可權(即其他授權)
UpdateSecurity 更新表上的許可權
UpdateIdentity 更改自動遞增列中的值
SelectSchema 查詢表的結構
Schema 更新表的結構
UpdateOwner 更改表的擁有者
華夏公益教科書