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

From Linuxnetworks
Jump to: navigation, search
(Alias and quotes)
 
m (Reverted edits by 193.239.74.228 (Talk) to last version by Nose)
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__TOC__
 
__TOC__
  
 +
== In SELECT statements ==
  
= Alias for tables and columns =
+
=== Aliasing tables ===
  
Don't use the keyword "AS" between 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:
  
 
  SELECT * FROM table t WHERE t.id = 0
 
  SELECT * FROM table t WHERE t.id = 0
  
= Quotes and numeric values =
+
=== Aliasing columns ===
  
MS SQL Server and Sybase ASE doesn't allow single quotes (') around numeric values (integer and floating point values), for example
+
Contrary to "Aliasing tables" the keyword "AS" between column name and alias is required by most database servers and should always be present:
  
  INSERT INTO table (floatval,string) VALUES ('-3.14','some text')
+
  SELECT count(*) AS cnt FROM table
  
generates an error when using these servers. They only accept
+
=== Aliases for computed columns ===
  
INSERT INTO table (floatval,string) VALUES (-3.14,'some text')
+
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:
  
This gets tricky if the values are supplied by a user. You must validate the input to be sure it is a numeric value and you can't use the escape() function. Otherwise, if you don't look at the input, your application will be vulnerable to SQL injection and you will be in deep trouble.
+
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:
Back to [[OpenDBX|Overview]]
+
 
 +
* INSERT
 +
* UPDATE
 +
* DELETE
 +
 
 +
Aliases should be avoided in them to ensure portable statements.

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.