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 |