What is wrong with this SQL syntax? - sql

UPDATE files
SET filepath = REPLACE(filepath, `sites/somedomain.com/files/`, `sites/someotherdomain.com/files/`);
I have a table called files with a field called filepath. MySQL returns this error: Unknown column 'sites/somedomain.com/files/' in 'field list'

Use normal quotes instead of backquotes: normal quotes identify strings, backquotes identify column names.

To expand on kemp's answer:
Backquotes or backticks ` are used in MySQL to enclose the names of schema objects (databases, tables, columns, indexes, procedures, ...). They cannot be used to enclose strings. You must use regular single- or double-quotes: ' or " for that.

+1 to answers given by #kemp and #Hammerite.
See also my answer to this question: Do different databases use different name quote?

Related

Drop database with special characters

While testing RoR, I created a database that is literally named "<test_project>_development", so when I try to drop it through postgresql I get a syntax error because of the "<>" in the database's name.
Postgres uses quotes to delimit literal entity names:
drop database "<test_project>_development"
See documentation.

Create mixed case table name/columns in oracle WITHOUT using quotes

Is there any way I can create a mixed case table name in Oracle without using quotes?
The table names change to uppercase if I do not use quotes while creating the table.
Let me know if you need more information.
Eg:
create table testTable(testColumn varchar);
This creates a table named TESTTABLE with column name TESTCOLUMN.
I can use quotes, but it makes it more messy and difficult to write queries.
Can you please let me know how I can do this without using quotes? Thanks.
Regards,
Sawan
Please refer to the official Oracle Database documentation:
"Nonquoted identifiers are not case sensitive. Oracle interprets them as uppercase."
https://docs.oracle.com/en/database/oracle/oracle-database/20/sqlrf/Database-Object-Names-and-Qualifiers.html#GUID-3C59E44A-5140-4BCA-B9E1-3039C8050C49
All the data dictionary views will show the identifiers as Oracle interprets them. In other words, without quotes everything will be uppercase. I can testify that this is true even for accented characters.
You can't always get what you want...

Turn off upper-case for table and column names in HSQL?

How to turn off forced upper-case mode for table and column names in HSQL?
<artifactId>hsqldb</artifactId>
<version>2.3.1</version>
OS: Windows 7 x64
The rules around this are explained in the HSQLDB documentation:
When a database object is created with one of the CREATE statements or
renamed with the ALTER statement, if the name is enclosed in double
quotes, the exact name is used as the case-normal form. But if it is
not enclosed in double quotes, the name is converted to uppercase and
this uppercase version is stored in the database as the case-normal
form.
Case sensitivity rules for identifiers can be described simply as
follows:
all parts of SQL statements are converted to upper case before processing, except identifiers in double quotes and strings in single
quotes
identifiers, both unquoted and double quoted, are then treated as case-sensitive
most database engines follow the same rule, except, in some respects, MySQL and MS SQLServer.
AFAIK this behaviour can't be turned off. (It's worth noting that standard SQL is case-insensitive when quoted identifiers aren't used.) But as mentioned above, a lower case identifier can be specified by enclosing in quotes, e.g:
CREATE TABLE "lowercasetablename" ("lowercasecolname" INT);
SELECT "lowercasecolname" FROM "lowercasetablename";
I am not sure, i understand the problem correctly but just trying to put some effort.
SET DATABASE COLLATION SQL_TEXT_UCC
May be you can refer http://hsqldb.org/doc/guide/dbproperties-chapt.html

Escape table name in SQLite?

I have table named References in SQLite, so I can't target it, it seems. SQLite studio I use to edit databases throws an error.
Is there a way to escape database name?
The query is:
UPDATE References
SET DateTimeLastEdited = datetime('now', 'localtime')
WHERE NewsItemID = old.NewsItemID;
(This is part of the trigger I am making.)
You can escape table names with double quotes:
UPDATE "References" SET DateTimeLastEdited = datetime('now', 'localtime') WHERE NewsItemID = old.NewsItemID;
Depending on what you want to escape, you need to use different delimiters:
If you want to use a keyword as a name, you need to quote it. There
are four ways of quoting keywords in SQLite:
'keyword' A keyword in single quotes is a string literal.
"keyword" A keyword in double-quotes is an identifier.
[keyword] A
keyword enclosed in square brackets is an identifier. This is not
standard SQL. This quoting mechanism is used by MS Access and SQL
Server and is included in SQLite for compatibility.
`keyword` A
keyword enclosed in grave accents (ASCII code 96) is an identifier.
This is not standard SQL. This quoting mechanism is used by MySQL and
is included in SQLite for compatibility.
From SQLite documentation

NHibernate: forcing square brackets in schema export?

Is there a way to tell NHibernate to use square brackets for all table and column names (like [MyColumn]) when generating the SQL schema export for MS SQL Server? I have a legacy database that uses reserved names for certain columns and running the SQL script generated using NH throws an error because of it.
I want to avoid having to specify this separately for each column.
UPDATE: I'm using the correct dialect:
MsSqlConfiguration.MsSql2008.ConnectionString(connectionString)
UPDATE 2: #UpTheCreek pointed me in the right direction - backticks, which helped me find the answer in the "NHibernate in Action" book (p. 76):
There is no way, apart from quoting all table and column names in backticks, to force NHibernate to use quoted identifiers everywhere.
Easier approach:
SchemaMetadataUpdater.QuoteTableAndColumns(config)
(Before building SessionFactory)
That will quote all the reserved names automatically.
Use backticks in your mapping files around the column names. NH should replace these with the correct character for your db dialect (in your case square brackets).
i.e. use:
<class name="SomeClass" table="`SomeTable`">
NB - It won't work with an apostrophe. The backtick is located top left on most keyboards.
You need to use (or write) the correct dialect for your database