Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Square bracket in table/column name is not supported?

Tags:

sql

postgresql

Is square bracket in table name, column name or datatype is not supported in postgresql ?

I am getting below error while running the query in pgadmin:

CREATE TABLE [Test];

ERROR: syntax error at or near "["

SQL state: 42601

like image 935
Shesha Avatar asked Jun 22 '16 10:06

Shesha


3 Answers

In PostgreSQL you can use double quotes :

CREATE TABLE "Test";

Same for columns, square brackets are used in SQL-Server.

like image 65
sagi Avatar answered Sep 27 '22 22:09

sagi


If you mean the table name would be [Test] with brackets included then you would use "[Test]".

Create table "[Test]" ...;

If you meant it as an identifier, you could simply use without brackets or double quotes as Test.

Create table Test ...;

This way, you could refer to it as Test or test or tESt without double quotes in subsequent queries, ie:

select * from test;

If you use "Test" then postgreSQL would treat it as case sensitive and you would always use "Test".

Create table "Test" ...;
like image 22
Cetin Basoz Avatar answered Sep 27 '22 21:09

Cetin Basoz


If you were trying to emphasize the identifier name by using square brackets, then sagi's answer is correct. On the other hand, if you really want to use square brackets in your table name, postgresql supports this as "[Test]". In this case your table name will include square brackets. You can get additional info from postgresql documentation

like image 26
Nuri Tasdemir Avatar answered Sep 27 '22 22:09

Nuri Tasdemir