PostgreSQL中關於關鍵字(保留字)在表名和字段名中的應用文件解決


標識符和關鍵詞

受限標識符或被引號修飾的標識符。它是由雙引號(")包圍的一個任意字符序列。一個受限標識符總是一個標識符而不會是一個關鍵字。因此"select"可以用於引用一個名為“select”的列或者表,而一個沒有引號修飾的select則會被當作一個關鍵詞,從而在本應使用表或列名的地方引起解析錯誤。在上例中使用受限標識符的例子如下:UPDATE "my_table" SET "a" = 5;

在PostgreSQL關系型數據庫中存在關鍵字的使用的問題,例如user 做表名,create table user (id int, name,varchar(20));創建的時候需要給表名user加上雙引號"user";

[postgres@node1 bin]$ ./psql
psql (9.5.1)
Type "help" for help.

postgres=# create table user(id int,name varchar(20));
ERROR: syntax error at or near "user" at character 14
STATEMENT: create table user(id int,name varchar(20));
ERROR: syntax error at or near "user"
LINE 1: create table user(id int,name varchar(20));

postgres=# create table "user"(id int,name varchar(20));
CREATE TABLE

postgres=# insert into user values (1,'PostgreSQL');
ERROR: syntax error at or near "user" at character 13
STATEMENT: insert into user values (1,'PostgreSQL');
ERROR: syntax error at or near "user"
LINE 1: insert into user values (1,'PostgreSQL');

postgres=# insert into "user" values (1,'PostgreSQL');
INSERT 0 1

postgres=# insert into "user" values (1,'PostgreSQL');
INSERT 0 1
postgres=# select * from user;
current_user
--------------
postgres
(1 row)

postgres=# select * from "user";
id | name
----+------------
1 | PostgreSQL
(1 row)

postgres=# create table user_info(order int,name varchar(20));
ERROR: syntax error at or near "order" at character 24
STATEMENT: create table user_info(order int,name varchar(20));
ERROR: syntax error at or near "order"
LINE 1: create table user_info(order int,name varchar(20));

postgres=# create table user_info("order" int,name varchar(20));
CREATE TABLE

postgres=# insert into user_info values (1,'PostgreSQL');
INSERT 0 1

表中字段大小寫問題

hotel_db_p7=# \d hotel_db_p.gt10_business_area;
Table "hotel_db_p.gt10_business_area"
Column | Type | Modifiers
----------+-----------------------+-----------
Code | character varying(20) | not null
CityCode | character varying(10) |
Name | character varying(50) |
hot_flag | character(1) |
Indexes:
"gt10_business_area_pkey" PRIMARY KEY, btree ("Code")

hotel_db_p7=# select CityCode from hotel_db_p.gt10_business_area;
ERROR: column "citycode" does not exist
LINE 1: select CityCode from hotel_db_p.gt10_business_area;
^
HINT: Perhaps you meant to reference the column "gt10_business_area.CityCode".

hotel_db_p7=# select "CityCode" from hotel_db_p.gt10_business_area;
CityCode
----------
1101
1101
1101
1101
1101
1101
1101
1101
1101
1101
1101
1101
1101
1101
1101


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM