v5:userguide:portable_sql
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
reference:portable_sql [2015/12/04 18:37] – [Final Thoughts] mnewnham | v5:reference:portable_sql [2016/01/13 02:31] – ↷ Links adapted because of a move operation mnewnham | ||
---|---|---|---|
Line 26: | Line 26: | ||
^Oracle|select * from (select * from table) where rownum <= 10| | ^Oracle|select * from (select * from table) where rownum <= 10| | ||
- | This feature of getting a subset of data is so useful that in the PHP class library ADOdb, we have a [[reference: | + | This feature of getting a subset of data is so useful that in the PHP class library ADOdb, we have a [[v5:reference:selectlimit]] function that allows you to hide the implementation details within a function that will rewrite your SQL for you: |
<code php> | <code php> | ||
Line 34: | Line 34: | ||
==== Fetch Modes Within Select Statements ==== | ==== Fetch Modes Within Select Statements ==== | ||
- | PHP allows you to retrieve database records as arrays. You can choose to have the arrays indexed by field name or number. However different low-level PHP database drivers are inconsistent in their indexing efforts. ADOdb allows you to determine your prefered mode. You set this by setting the variable [[reference: | + | PHP allows you to retrieve database records as arrays. You can choose to have the arrays indexed by field name or number. However different low-level PHP database drivers are inconsistent in their indexing efforts. ADOdb allows you to determine your prefered mode. You set this by setting the variable [[v5:reference:adodb_fetch_mode]] to either of the constants: |
* ADODB_FETCH_NUM (for numeric indexes) | * ADODB_FETCH_NUM (for numeric indexes) | ||
* ADODB_FETCH_ASSOC (using field names as an associative index). | * ADODB_FETCH_ASSOC (using field names as an associative index). | ||
Line 44: | Line 44: | ||
Another problem with SELECTs is that some databases do not return the number of rows retrieved from a select statement. This is because the highest performance databases will return records to you even before the last record has been found. | Another problem with SELECTs is that some databases do not return the number of rows retrieved from a select statement. This is because the highest performance databases will return records to you even before the last record has been found. | ||
- | In ADOdb, [[reference: | + | In ADOdb, [[v5:reference:recordcount]] returns the number of rows returned, or will emulate it by buffering the rows and returning the count after all rows have been returned. This can be disabled for performance reasons when retrieving large recordsets by setting the global variable [[v5:reference:adodb_countrecs]] = false. This variable is checked every time a query is executed, so you can selectively choose which recordsets to count. |
- | If you prefer to set '' | + | If you prefer to set '' |
<code php> | <code php> | ||
Line 100: | Line 100: | ||
When you create records, you need to generate unique id's for each record. There are two common techniques: (1) auto-incrementing columns and (2) sequences. | When you create records, you need to generate unique id's for each record. There are two common techniques: (1) auto-incrementing columns and (2) sequences. | ||
- | Auto-incrementing columns are not supported by some databases, so for portability, | + | Auto-incrementing columns are not supported by some databases, so for portability, |
<code php> | <code php> | ||
Line 153: | Line 153: | ||
===== DDL and Tuning ===== | ===== DDL and Tuning ===== | ||
- | There are database design tools such as ERWin or Dezign that allow you to generate data definition language commands such as ALTER TABLE or CREATE INDEX from Entity-Relationship diagrams. | + | There are database design tools such as ERWin or Dezign that allow you to generate data definition language commands such as '' |
However if you prefer to use a PHP-based table creation scheme, ADOdb provides you with this feature. Here is the code to generate the SQL to create a table with: | However if you prefer to use a PHP-based table creation scheme, ADOdb provides you with this feature. Here is the code to generate the SQL to create a table with: | ||
Line 191: | Line 191: | ||
</ | </ | ||
- | For date calculations, | + | For date calculations, |
<code php> | <code php> | ||
Line 197: | Line 197: | ||
</ | </ | ||
- | The maximum length of a char/ | + | The maximum length of a char/ |
<code php> | <code php> | ||
Line 213: | Line 213: | ||
In general, I avoid using nulls except for dates and default all my numeric and character fields to 0 or the empty string. This maintains consistency with PHP, where empty strings and zero are treated as equivalent, and avoids SQL ambiguities when you use the ANY and EXISTS operators. However if your database has significant amounts of missing or unknown data, using nulls might be a good idea. | In general, I avoid using nulls except for dates and default all my numeric and character fields to 0 or the empty string. This maintains consistency with PHP, where empty strings and zero are treated as equivalent, and avoids SQL ambiguities when you use the ANY and EXISTS operators. However if your database has significant amounts of missing or unknown data, using nulls might be a good idea. | ||
- | ADOdb also supports a portable [[reference: | + | ADOdb also supports a portable [[v5:reference:ifnull]] function, so you can define what to display if the field contains a null. |
===== Stored Procedures ===== | ===== Stored Procedures ===== | ||
Line 267: | Line 267: | ||
</ | </ | ||
- | Note that we quote the $word variable using the [[reference: | + | Note that we quote the $word variable using the [[v5:reference: |
===== Final Thoughts ===== | ===== Final Thoughts ===== |
v5/userguide/portable_sql.txt · Last modified: 2020/12/30 21:32 by peterdd