欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL 5.7中的关键字与保留字详解

程序员文章站 2023-11-18 17:43:34
前言 mysql和oracle的关键字还是不尽相同的,在oracle数据库中,我们的数据表中定义了大量的code字段用来表示主键,但是在mysql中code是关键字,使用...

前言

mysql和oracle的关键字还是不尽相同的,在oracle数据库中,我们的数据表中定义了大量的code字段用来表示主键,但是在mysql中code是关键字,使用以前的处理方法就有些“水土不服”。

下面我们来了解一下mysql中的关键字和保留字。

什么是关键字和保留字

关键字是指在sql中有意义的字。 某些关键字(例如select,delete或bigint)是保留的,需要特殊处理才能用作表和列名称等标识符。 这一点对于内置函数的名称也适用。

如何使用关键字和保留字

非保留关键字允许作为标识符,不需要加引号。 如果您要适用保留字作为标识符,就必须适用引号。

举个例子,begin和end是关键字,但不是保留字,因此它们用作标识符不需要引号。 interval是保留关键字,必须加上引号才能用作标识符。

mysql>
mysql> use hoegh;
database changed
mysql>
mysql> create table interval (begin int, end int);
error 1064 (42000):
mysql>
mysql> create table `interval` (begin int, end int);
query ok, 0 rows affected (0.42 sec)

mysql>
mysql> show create table `interval`;
+----------+---------------------------------------------------------
| table | create table
+----------+---------------------------------------------------------
| interval | create table `interval` (
 `begin` int(11) default null,
 `end` int(11) default null
) engine=innodb default charset=latin1 |
+----------+---------------------------------------------------------
1 row in set (0.00 sec)

mysql>

我们看到,第一条语句中表名使用了保留字interval,执行失败;

第二条语句对interval加了引号,执行成功。

在这里需要注意的是,引号必须是反引号,而非单引号。否则会报错,如下所示:

mysql>
mysql> drop table `interval`;--使用反引号
query ok, 0 rows affected (0.11 sec)

mysql>
mysql> create table 'interval' (begin int, end int);--使用单引号,报错
error 1064 (42000):

mysql>

有一个例外

如果标识符在限定名称(数据库名)的句点之后,即使是保留关键字也不需要引号。

我们以hoegh数据库为例,如果表名写为hoegh.interval就不需要对保留字interval加引号了。

mysql>
mysql> create table hoegh.interval (begin int, end int);
query ok, 0 rows affected (0.19 sec)

mysql>
mysql> show create table hoegh.interval;
+----------+---------------------------------------------------------
| table | create table
+----------+---------------------------------------------------------
| interval | create table `interval` (
 `begin` int(11) default null,
 `end` int(11) default null
) engine=innodb default charset=latin1 |
+----------+---------------------------------------------------------
1 row in set (0.00 sec)

mysql>

关于使用内置函数名称

允许内置函数的名称可以作为标识符,但最好谨慎使用。例如,count作为列名称是合法的。但是,默认情况下,在函数名和后面的(之间的函数调用中不允许有空格。这个限制使解析器能够区分名称是用于函数调用还是用在非函数上下文中。

附录

在某些时候,您可能需要升级到更高版本,因此最好查看一下未来的保留字。您可以在涵盖更高版本的mysql的手册中找到这些。对于表中的大多数保留字,在标准sql中禁止作为列或表的名称(例如,group)。其中一些保留字,是由于mysql需要它们并使用一个yacc解析器。

以下列出三张表格:

第一个表格10.2显示mysql 5.7中的关键字和保留字。保留的关键字标记为(r)。此外,_filename是保留的。

第二个表格10.3显示mysql 5.7相比5.6版本新增的保留字。

第三个表格10.4显示mysql 5.7相比5.6版本删除的保留字。

table 10.2 keywords and reserved words in mysql 5.7

accessible (r) account action
add (r) after against
aggregate algorithm all (r)
alter (r) always analyse
analyze (r) and (r) any
as (r) asc (r) ascii
asensitive (r) at autoextend_size
auto_increment avg avg_row_length
backup before (r) begin
between (r) bigint (r) binary (r)
binlog bit blob (r)
block bool boolean
both (r) btree by (r)
byte cache call (r)
cascade (r) cascaded case (r)
catalog_name chain change (r)
changed channel char (r)
character (r) charset check (r)
checksum cipher class_origin
client close coalesce
code collate (r) collation
column (r) columns column_format
column_name comment commit
committed compact completion
compressed compression concurrent
condition (r) connection consistent
constraint (r) constraint_catalog constraint_name
constraint_schema contains context
continue (r) convert (r) cpu
create (r) cross (r) cube
current current_date (r) current_time (r)
current_timestamp (r) current_user (r) cursor (r)
cursor_name data database (r)
databases (r) datafile date
datetime day day_hour (r)
day_microsecond (r) day_minute (r) day_second (r)
deallocate dec (r) decimal (r)
declare (r) default (r) default_auth
definer delayed (r) delay_key_write
delete (r) desc (r) describe (r)
des_key_file deterministic (r) diagnostics
directory disable discard
disk distinct (r) distinctrow (r)
div (r) do double (r)
drop (r) dual (r) dumpfile
duplicate dynamic each (r)
else (r) elseif (r) enable
enclosed (r) encryption end
ends engine engines
enum error errors
escape escaped (r) event
events every exchange
execute exists (r) exit (r)
expansion expire explain (r)
export extended extent_size
false (r) fast faults
fetch (r) fields file
file_block_size filter first
fixed float (r) float4 (r)
float8 (r) flush follows
for (r) force (r) foreign (r)
format found from (r)
full fulltext (r) function
general generated (r) geometry
geometrycollection get (r) get_format
global grant (r) grants
group (r) group_replication handler
hash having (r) help
high_priority (r) host hosts
hour hour_microsecond (r) hour_minute (r)
hour_second (r) identified if (r)
ignore (r) ignore_server_ids import
in (r) index (r) indexes
infile (r) initial_size inner (r)
inout (r) insensitive (r) insert (r)
insert_method install instance
int (r) int1 (r) int2 (r)
int3 (r) int4 (r) int8 (r)
integer (r) interval (r) into (r)
invoker io io_after_gtids (r)
io_before_gtids (r) io_thread ipc
is (r) isolation issuer
iterate (r) join (r) json
key (r) keys (r) key_block_size
kill (r) language last
leading (r) leave (r) leaves
left (r) less level
like (r) limit (r) linear (r)
lines (r) linestring list
load (r) local localtime (r)
localtimestamp (r) lock (r) locks
logfile logs long (r)
longblob (r) longtext (r) loop (r)
low_priority (r) master master_auto_position
master_bind (r) master_connect_retry master_delay
master_heartbeat_period master_host master_log_file
master_log_pos master_password master_port
master_retry_count master_server_id master_ssl
master_ssl_ca master_ssl_capath master_ssl_cert
master_ssl_cipher master_ssl_crl master_ssl_crlpath
master_ssl_key master_ssl_verify_server_cert(r) master_tls_version
master_user match (r) maxvalue (r)
max_connections_per_hour max_queries_per_hour max_rows
max_size max_statement_time max_updates_per_hour
max_user_connections medium mediumblob (r)
mediumint (r) mediumtext (r) memory
merge message_text microsecond
middleint (r) migrate minute
minute_microsecond (r) minute_second (r) min_rows
mod (r) mode modifies (r)
modify month multilinestring
multipoint multipolygon mutex
mysql_errno name names
national natural (r) nchar
ndb ndbcluster never
new next no
nodegroup nonblocking none
not (r) no_wait no_write_to_binlog (r)
null (r) number numeric (r)
nvarchar offset old_password
on (r) one only
open optimize (r) optimizer_costs (r)
option (r) optionally (r) options
or (r) order (r) out (r)
outer (r) outfile (r) owner
pack_keys page parser
parse_gcol_expr partial partition (r)
partitioning partitions password
phase plugin plugins
plugin_dir point polygon
port precedes precision (r)
prepare preserve prev
primary (r) privileges procedure (r)
processlist profile profiles
proxy purge (r) quarter
query quick range (r)
read (r) reads (r) read_only
read_write (r) real (r) rebuild
recover redofile redo_buffer_size
redundant references (r) regexp (r)
relay relaylog relay_log_file
relay_log_pos relay_thread release (r)
reload remove rename (r)
reorganize repair repeat (r)
repeatable replace (r) replicate_do_db
replicate_do_table replicate_ignore_db replicate_ignore_table
replicate_rewrite_db replicate_wild_do_table replicate_wild_ignore_table
replication require (r) reset
resignal (r) restore restrict (r)
resume return (r) returned_sqlstate
returns reverse revoke (r)
right (r) rlike (r) rollback
rollup rotate routine
row rows row_count
row_format rtree savepoint
schedule schema (r) schemas (r)
schema_name second second_microsecond (r)
security select (r) sensitive (r)
separator (r) serial serializable
server session set (r)
share show (r) shutdown
signal (r) signed simple
slave slow smallint (r)
snapshot socket some
soname sounds source
spatial (r) specific (r) sql (r)
sqlexception (r) sqlstate (r) sqlwarning (r)
sql_after_gtids sql_after_mts_gaps sql_before_gtids
sql_big_result (r) sql_buffer_result sql_cache
sql_calc_found_rows (r) sql_no_cache sql_small_result (r)
sql_thread sql_tsi_day sql_tsi_hour
sql_tsi_minute sql_tsi_month sql_tsi_quarter
sql_tsi_second sql_tsi_week sql_tsi_year
ssl (r) stacked start
starting (r) starts stats_auto_recalc
stats_persistent stats_sample_pages status
stop storage stored (r)
straight_join (r) string subclass_origin
subject subpartition subpartitions
super suspend swaps
switches table (r) tables
tablespace table_checksum table_name
temporary temptable terminated (r)
text than then (r)
time timestamp timestampadd
timestampdiff tinyblob (r) tinyint (r)
tinytext (r) to (r) trailing (r)
transaction trigger (r) triggers
true (r) truncate type
types uncommitted undefined
undo (r) undofile undo_buffer_size
unicode uninstall union (r)
unique (r) unknown unlock (r)
unsigned (r) until update (r)
upgrade usage (r) use (r)
user user_resources use_frm
using (r) utc_date (r) utc_time (r)
utc_timestamp (r) validation value
values (r) varbinary (r) varchar (r)
varcharacter (r) variables varying (r)
view virtual (r) wait
warnings week weight_string
when (r) where (r) while (r)
with (r) without work
wrapper write (r) x509
xa xid xml
xor (r) year year_month (r)
zerofill (r)    

account: added in 5.7.6 (nonreserved)

always: added in 5.7.6 (nonreserved)

channel: added in 5.7.6 (nonreserved)

compression: added in 5.7.8 (nonreserved)

encryption: added in 5.7.11 (nonreserved)

file_block_size: added in 5.7.6 (nonreserved)

filter: added in 5.7.3 (nonreserved)

follows: added in 5.7.2 (nonreserved)

generated: added in 5.7.6 (reserved)

group_replication: added in 5.7.6 (nonreserved)

instance: added in 5.7.11 (nonreserved)

json: added in 5.7.8 (nonreserved)

master_tls_version: added in 5.7.10 (nonreserved)

max_statement_time: added in 5.7.4 (nonreserved); removed in 5.7.8

never: added in 5.7.4 (nonreserved)

nonblocking: removed in 5.7.6

old_password: removed in 5.7.5

optimizer_costs: added in 5.7.5 (reserved)

parse_gcol_expr: added in 5.7.6 (reserved); became nonreserved in 5.7.8

precedes: added in 5.7.2 (nonreserved)

replicate_do_db: added in 5.7.3 (nonreserved)

replicate_do_table: added in 5.7.3 (nonreserved)

replicate_ignore_db: added in 5.7.3 (nonreserved)

replicate_ignore_table: added in 5.7.3 (nonreserved)

replicate_rewrite_db: added in 5.7.3 (nonreserved)

replicate_wild_do_table: added in 5.7.3 (nonreserved)

replicate_wild_ignore_table: added in 5.7.3 (nonreserved)

rotate: added in 5.7.11 (nonreserved)

stored: added in 5.7.6 (reserved)

validation: added in 5.7.5 (nonreserved)

virtual: added in 5.7.6 (reserved)

without: added in 5.7.5 (nonreserved)

xid: added in 5.7.5 (nonreserved)

table 10.3 keywords and reserved words added in mysql 5.7 compared to mysql 5.6

account always channel
compression encryption file_block_size
filter follows generated (r)
group_replication instance json
master_tls_version never optimizer_costs (r)
parse_gcol_expr precedes replicate_do_db
replicate_do_table replicate_ignore_db replicate_ignore_table
replicate_rewrite_db replicate_wild_do_table replicate_wild_ignore_table
rotate stacked stored (r)
validation virtual (r) without
xid    

table 10.4 keywords and reserved words removed in mysql 5.7 compared to mysql 5.6

old_password    

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。