Difference between revisions of "Portable SQL/Statements/Data retrieval/Simple SELECT"

From Linuxnetworks
Jump to: navigation, search
(simplest select)
 
(removed backlink)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The SQL language uses SELECT statements for asking the database server to send results based on the conditions back to the client. Contrary to the data manipulation statements which were only extended slightly by the database vendors, the portability of SELECT statements becomes a nightmare very fast.
+
The simplest SELECT statement for fetching records from a single table which is supported by all database engines is
 
+
== Simple SELECTs ==
+
 
+
The simplest but also very limited statement for fetching records from a single table is
+
  
 
  SELECT ''column-list''
 
  SELECT ''column-list''
 
  FROM ''table''
 
  FROM ''table''
 +
WHERE ''conditions''
  
 
To be on the save side ''column-list'' should be either a comma-separated list of column names or an asterisk (*). The asterisk symbol frees you from the burden to list all columns you would like to access in your application. But be careful! If your table has many columns the performance drops significant because more values have to be passed to the client over the network and the wire is the bottleneck most of the time. The problem will get worse if the table contains columns with much data for the same reason. Moreover, additional problems may arise as soon as you begin to join another table so don't use the asterisk without thinking twice if it's worth the potential trouble!
 
To be on the save side ''column-list'' should be either a comma-separated list of column names or an asterisk (*). The asterisk symbol frees you from the burden to list all columns you would like to access in your application. But be careful! If your table has many columns the performance drops significant because more values have to be passed to the client over the network and the wire is the bottleneck most of the time. The problem will get worse if the table contains columns with much data for the same reason. Moreover, additional problems may arise as soon as you begin to join another table so don't use the asterisk without thinking twice if it's worth the potential trouble!
  
This kind of SELECT statement is not very useful most of the time. It will send all records of that table over the network even if there are millions of rows in it. Using this, you are asking for trouble as soon as your application stores user generated content into this table. Maybe the highscore of a small game? What will happen if it gets popular by a link on a big website?
+
Keep an eye on the ''conditions'' list as using uncommon operators or built-in functions provided by the database engine tend to decrease the portability of statements. If your table contains user generated content it's also important to use sane conditions to minimize the amount of records sent by the database server. Otherwise, you will face the same problem as above.

Latest revision as of 15:07, 22 June 2008

The simplest SELECT statement for fetching records from a single table which is supported by all database engines is

SELECT column-list
FROM table
WHERE conditions

To be on the save side column-list should be either a comma-separated list of column names or an asterisk (*). The asterisk symbol frees you from the burden to list all columns you would like to access in your application. But be careful! If your table has many columns the performance drops significant because more values have to be passed to the client over the network and the wire is the bottleneck most of the time. The problem will get worse if the table contains columns with much data for the same reason. Moreover, additional problems may arise as soon as you begin to join another table so don't use the asterisk without thinking twice if it's worth the potential trouble!

Keep an eye on the conditions list as using uncommon operators or built-in functions provided by the database engine tend to decrease the portability of statements. If your table contains user generated content it's also important to use sane conditions to minimize the amount of records sent by the database server. Otherwise, you will face the same problem as above.