跳轉到內容

JET 資料庫/物件名稱和常量

來自華夏公益教科書,開放的書籍,開放的世界
JET 資料庫

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

在資料庫中命名物件

[編輯 | 編輯原始碼]

與大多數其他 SQL 資料庫一樣,在命名資料庫物件(如表、列、索引、檢視和儲存過程)時建議小心。使用超出正常命名約定的名稱可能會在編寫 SQL 程式碼以處理這些物件時導致問題,需要在 SQL 程式碼中限定這些名稱。JET 中的約定是

  • 名稱必須以字母字元開頭(a-z 或 A-Z)
  • 名稱只能包含字母數字字元或下劃線
  • 名稱不能是 保留字

限定特殊名稱

[編輯 | 編輯原始碼]

在某些情況下,物件會使用在 SQL 語句中會導致問題的名稱,例如包含空格之類的特殊字元,或使用保留字。有時這是合理的,有時則不然,但無論哪種情況,都需要處理這種情況。

在這種情況下,可以使用以下方法之一限定物件名稱

  • 將名稱括在(方)括號中,[...]
  • 將名稱括在左單引號(重音符)中,`...`

以下是一些示例

Create Table `Name With Spaces` (
 `Complex Column Name a/b %` single, 
 [Text] text(20))
go

Insert Into `Name With Spaces` (`Complex Column Name a/b %`, [Text]) 
 Values (1.45, 'First attempt')
go

Select `Complex Column Name a/b %`, [Text] As [Output Complex Text Name]
 From `Name With Spaces`
go
Complex Column Name a/b % Output Complex Text Name
------------------------- ------------------------
1.45                      First attempt

文字常量

[編輯 | 編輯原始碼]

文字常量可以使用單引號(撇號)分隔符或雙引號分隔符編寫。文字常量內部的任何分隔符例項都需要加倍。

Select 'Eat the "food" at O''Malley''s' As T1, 
 "Eat the ""food"" at O'Malley's" As T2
go
T1                             T2
------------------------------ ------------------------------
Eat the "food" at O'Malley's   Eat the "food" at O'Malley's

數字常量

[編輯 | 編輯原始碼]

數字常量可以採用以下形式

  • 純整數 – 123
  • 小數 – 123.45
  • 科學計數法中的小數 – 123.45E5(等於 12,345,000)

數字常量不能包含逗號、美元符號或其他非數字字元,除了小數點或科學計數法中的 E。

日期/時間常量

[編輯 | 編輯原始碼]

日期和時間常量最好寫成井號分隔的字串,可以是長文字格式、美國日期格式(mm/dd/yyyy)或 ODBC 日期格式(yyyy-mm-dd)。

Select #26 October 2007 6:43pm# As D_Long_Format, 
 #10/26/2007 18:43:00# As D_US_Format, 
 #2007-10-26 18:43:00# As D_ODBC_Format
go

D_Long_Format           D_US_Format             D_ODBC_Format
----------------------- ----------------------- -----------------------
26/10/2007 18:43:00     26/10/2007 18:43:00     26/10/2007 18:43:00

注意:JET **不會** 以無歧義的方式接受其他格式的日期!例如,指定 dd/mm/yyyy 格式的日期,當無法將該日期解釋為美國日期格式時,似乎可以正常工作,否則它將被錯誤地解釋,**無論使用什麼區域設定**。為了消除歧義,最好以 ODBC 格式指定日期。

保留字

[編輯 | 編輯原始碼]

這是 JET 中保留字的列表。這些詞並非全部在用作物件名稱時會導致問題,例如在 JET 4.0 中,但在 JET 的未來版本中可能會出現問題。

absolute action add admindb all allocate
alphanumeric alter and any are as
asc assertion at authorization autoincrement avg
band begin between binary bit bit_length
bnot bor both bxor by byte
cascade cascaded case cast catalog char
character char_length character_length check close coalesce
collate collation column commit comp compression
connect connection constraint constraints container continue
convert corresponding count counter create createdb
cross currency current current_date current_time current_timestamp
current_user cursor database date datetime day
deallocate dec decimal declare default deferrable
deferred delete desc describe descriptor diagnostics
disallow disconnect distinct domain double drop
else end end_exec escape except exception
exclusiveconnect exec execute exists external extract
false fetch first float float4 float8
for foreign found from full general
get global go goto grant group
guid having hour identity ieeedouble ieeesingle
ignore image immediate in index indicator
inheritable initially inner input insensitive insert
int integer integer1 integer2 integer4 intersect
interval into is isolation join key
language last leading left level like
local logical logical1 long longbinary longchar
longtext lower match max memo min
minute module money month names national
natural nchar next no not note
null nullif number numeric object octet_length
of oleobject on only open option
or order outer output overlaps owneraccess
pad parameters partial password percent pivot
position precision prepare preserve primary prior
privileges proc procedure public read real
references relative restrict revoke right rollback
rows schema scroll second section select
selectschema selectsecurity session session_user set short
single size smallint some space sql
sqlcode sqlerror sqlstate string substring sum
system_user table tableid temporary text then
time timestamp timezone_hour timezone_minute to top
trailing transaction transform translate translation trim
true union unique uniqueidentifier unknown update
updateidentity updateowner updatesecurity upper usage user
using value values varbinary varchar varying
view when whenever where with work
write year yesno zone
華夏公益教科書