Is there a way to execute SQL queries on MongoDB if the collection's name contains special characters such as 1a84375b-9bd0-4ec3-9f93-536ce380f813? I encounter org.apache.calcite.sql.parser.impl.ParseException when I execute my statement. Are there any escape characters?
In Calcite SQL, you can quote identifiers (table names and column names). In the default dialect, you use double quotes. For example,
SELECT "a column"
FROM "a table with spaces in the name"
Also note that when if identifiers are quoted, Calcite retains their case (it does not convert to upper or lower case) and uses case-sensitive matching.
By the way, this is the same as Oracle and several other common SQL dialects.
Related
I have a db table say, persons in Postgres handed down by another team that has a column name say, "first_Name". Now am trying to use PG commander to query this table on this column-name.
select * from persons where first_Name="xyz";
And it just returns
ERROR: column "first_Name" does not exist
Not sure if I am doing something silly or is there a workaround to this problem that I am missing?
Identifiers (including column names) that are not double-quoted are folded to lowercase in PostgreSQL. Column names that were created with double-quotes and thereby retained uppercase letters (and/or other syntax violations) have to be double-quoted for the rest of their life:
"first_Name"
Values (string literals / constants) are enclosed in single quotes:
'xyz'
So, yes, PostgreSQL column names are case-sensitive (when double-quoted):
SELECT * FROM persons WHERE "first_Name" = 'xyz';
Read the manual on identifiers here.
My standing advice is to use legal, lower-case names exclusively so double-quoting is never required.
To quote the documentation:
Key words and unquoted identifiers are case insensitive. Therefore:
UPDATE MY_TABLE SET A = 5;
can equivalently be written as:
uPDaTE my_TabLE SeT a = 5;
You could also write it using quoted identifiers:
UPDATE "my_table" SET "a" = 5;
Quoting an identifier makes it case-sensitive, whereas unquoted names are always folded to lower case (unlike the SQL standard where unquoted names are folded to upper case). For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other.
If you want to write portable applications you are advised to always quote a particular name or never quote it.
The column names which are mixed case or uppercase have to be double quoted in PostgresQL. So best convention will be to follow all small case with underscore.
if use JPA I recommend change to lowercase schema, table and column names, you can use next intructions for help you:
select
psat.schemaname,
psat.relname,
pa.attname,
psat.relid
from
pg_catalog.pg_stat_all_tables psat,
pg_catalog.pg_attribute pa
where
psat.relid = pa.attrelid
change schema name:
ALTER SCHEMA "XXXXX" RENAME TO xxxxx;
change table names:
ALTER TABLE xxxxx."AAAAA" RENAME TO aaaaa;
change column names:
ALTER TABLE xxxxx.aaaaa RENAME COLUMN "CCCCC" TO ccccc;
You can try this example for table and column naming in capital letters. (postgresql)
//Sql;
create table "Test"
(
"ID" integer,
"NAME" varchar(255)
)
//C#
string sqlCommand = $#"create table ""TestTable"" (
""ID"" integer GENERATED BY DEFAULT AS IDENTITY primary key,
""ExampleProperty"" boolean,
""ColumnName"" varchar(255))";
I generate a 10-character alphanumeric random string as a schema name for each new tenant in my DB.
In this one instance, it generated "5ku2mug7m8" as the string. The schema was created correctly (in pic) but when accessing the schema, an error of
"ERROR: syntax error at or near "5"
LINE 1: SELECT * FROM 5ku2mug7m8.tablename"
which I gather is due to the leading numeric. I tried wrapping the schema in double-quotes ("") but postgres doesn't accept that.
My question is: if I cannot access a schema with a leading numeric, why does postgres allow the creation of it in the first place? Now I have sitting in the DB a schema that can't be accessed -- unless it can be and I'm just missing it?
EDIT: From documentation,
SQL identifiers and key words must begin with a letter (a-z, but also
letters with diacritical marks and non-Latin letters) or an underscore
(_). Subsequent characters in an identifier or key word can be
letters, underscores, digits (0-9), or dollar signs ($). Note that
dollar signs are not allowed in identifiers according to the letter of
the SQL standard, so their use might render applications less
portable...
But why allow creation of the schema without any errors in the first place? Curious.
You probably created the schema with pgAdmin, and that tool automatically added the double quotes for you.
You can certainly delete the schema with
DROP SCHEMA "5ku2mug7m8";
unless you have nasty things like leading or trailing blanks in the schema name.
You can find out the real schema name with:
SELECT quote_ident(nspname)
FROM pg_namespace
WHERE nspname LIKE '%5%';
Your experience and your research of the documentation is valuable. You have seen why it is a bad idea to pick object or column names (“identifiers”) that are valid SQL identifiers and don't need double quotes.
You can create a schema whose name starts with a digit if the schema name is using double quotes.
Demo with psql:
# select version();
version
--------------------------------------------------------------------------------
-------------------------
PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (R
ed Hat 4.8.5-39), 64-bit
# create schema "5ku";
CREATE SCHEMA
# create table "5ku".t(x int);
CREATE TABLE
# select * from "5ku".t;
x
---
(0 rows)
This works fine;
<targets>
<target name="database" xsi:type="Database"
dbProvider="Npgsql.NpgsqlConnection, Npgsql"
connectionString="User ID=postgres;Password=xxx;Host=192.xx;Port=5432;Database=xxx;">
<!--Pooling=true;-->
<commandText>
insert into systemlogs(...;
</commandText>
But when changed to table name as
"SystemLogs"
(same done in database as well) it throws exception;
"couldnt find table name "systemlogs"
which make sense there isnt but why nlog dont realize updated table name?
In PostgreSQL all quoted identifiers (e.g. table and column names) are case sensitive:
See: Are PostgreSQL column names case-sensitive?
So NLog can't find them is you use quotes and the wrong casing.
So don't use quotes or use the correct casing
If you specified the table name as "SystemLogs" inside double quotes then you will need to use it that way also:
insert into "SystemLogs" ...
In Postgresql quoted identifiers retain the case they are quoted with and need to be referred to the same way. If you create as unquoted name SystemLogs then it will be folded to lower case. See below for more detail:
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
I have a table that has a column, file_name. I added file names that contain underscores using insert. Upon select I see spaces where the underscores should be. Apparently the underscore is used for formatting and needs to be escaped.
What is the proper way to insert values with underscores?
How can I update the values with spaces by replacing all spaces with underscores?
What is the proper way to select a row using where when the value has an underscore, such as where file_name = "some_file.txt"
In other words, how and when does the underscore need to be escaped?
There is no need to escape underscores in string literals in Postgres. See this example. Your case may be caused by a strange behavior of client application. Test your queries in psql.
I have converted a database from MySQL to Firebird, all tables name and field name are in lowercase, when I query the database, it gives me an error of table not found, because Firebird automatically converted the name of table in the query in UPPERCASE letters, but my table name in database is lowercase.
To query the database, I'm required to enclose the name of the table or the name of the field in double quotes, for example:
SELECT "field1","field2" FROM "table"
Is there a setting in Firebird to allow to query database with table/field name in lowercase letter without quoting it?
No. BTW, I suggest you to always create DB objects in uppercase (and without double quotes) in Firebird. This simplifies a lot, since you can access them writing in any case (even mixed case).