Difference between revisions of "Portable SQL/Common/Aliases"

From Linuxnetworks
Jump to: navigation, search
(content split)
m (Reverted edits by 193.239.74.228 (Talk) to last version by Nose)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__TOC__
 
__TOC__
  
 +
== In SELECT statements ==
  
= Using aliases =
+
=== Aliasing tables ===
 
+
== Aliasing tables ==
+
  
 
Don't use the keyword "AS" between the table name and alias as no DBMS requires it and some don't recognize it. Instead, always write:
 
Don't use the keyword "AS" between the table name and alias as no DBMS requires it and some don't recognize it. Instead, always write:
Line 10: Line 9:
 
  SELECT * FROM table t WHERE t.id = 0
 
  SELECT * FROM table t WHERE t.id = 0
  
== Aliasing columns ==
+
=== Aliasing columns ===
  
 
Contrary to "Aliasing tables" the keyword "AS" between column name and alias is required by most database servers and should always be present:
 
Contrary to "Aliasing tables" the keyword "AS" between column name and alias is required by most database servers and should always be present:
Line 16: Line 15:
 
  SELECT count(*) AS cnt FROM table
 
  SELECT count(*) AS cnt FROM table
  
== Aliases for computed columns ==
+
=== Aliases for computed columns ===
  
 
The naming of computed columns implemented by the database servers is very different. Some name it like the computational expression ('count(*)'), most often in upper case and sometimes it isn't named at all. Therefore computed columns should be always given a name:
 
The naming of computed columns implemented by the database servers is very different. Some name it like the computational expression ('count(*)'), most often in upper case and sometimes it isn't named at all. Therefore computed columns should be always given a name:
Line 22: Line 21:
 
  SELECT MAX(id) AS maxid FROM table
 
  SELECT MAX(id) AS maxid FROM table
  
= Quoting in statements =
+
== In other statements ==
 
+
== Quoting identifiers ==
+
 
+
Looking for good identifiers for tables, columns or views can often be a serious pain when your application should work with different database servers. There are a lot of reserved words which can't be used and each database vendor constantly extends his list. How can an application developer be sure that there will be no name clashes in the future? Similarly, databases tend to change the character case of identifiers. Some of them convert all to upper case, some to lower case while many preserve the case. If you want to access the value of a column by using the name instead of the position, things get really tricky.
+
 
+
To avoid both situations, the OpenDBX library forces all databases to operate in ANSI standard mode. Thus, they all support double quotes (") for quoting identifiers, e.g.
+
 
+
CREATE TABLE "order" ( "id" INTEGER, "limit" DECIMAL(10,2) )
+
+
SELECT o."limit" FROM "order" o WHERE o."id" = 1
+
 
+
Quoted identifiers can't clash anymore with reserved words (like "order" with ORDER BY) when they are quoted and they always keep their case. The disadvantage is that using them unquoted won't work any more in most cases.
+
 
+
The second possibility is to use unquoted names which are unlikely to get in conflict with reserved SQL words like ''so_limit''
+
 
+
CREATE TABLE shop_order ( so_id INTEGER, so_limit DECIMAL(10,2) )
+
+
SELECT so.so_limit FROM shop_order so WHERE so.so_id = 1
+
  
This also works if other applications will access the tables which aren't using the OpenDBX library and don't adjust the connections to ANSI mode if necessary.
+
Some database engines doesn't support aliases for tables in other types of statements than SELECT. Namely PostgreSQL and SQLite bail out with a SQL parsing error if aliases for tables are used in these statement types:
  
 +
* INSERT
 +
* UPDATE
 +
* DELETE
  
----
+
Aliases should be avoided in them to ensure portable statements.
Back to [[OpenDBX|Overview]]
+

Latest revision as of 09:49, 16 August 2010

In SELECT statements

Aliasing tables

Don't use the keyword "AS" between the table name and alias as no DBMS requires it and some don't recognize it. Instead, always write:

SELECT * FROM table t WHERE t.id = 0

Aliasing columns

Contrary to "Aliasing tables" the keyword "AS" between column name and alias is required by most database servers and should always be present:

SELECT count(*) AS cnt FROM table

Aliases for computed columns

The naming of computed columns implemented by the database servers is very different. Some name it like the computational expression ('count(*)'), most often in upper case and sometimes it isn't named at all. Therefore computed columns should be always given a name:

SELECT MAX(id) AS maxid FROM table

In other statements

Some database engines doesn't support aliases for tables in other types of statements than SELECT. Namely PostgreSQL and SQLite bail out with a SQL parsing error if aliases for tables are used in these statement types:

  • INSERT
  • UPDATE
  • DELETE

Aliases should be avoided in them to ensure portable statements.