ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:dictionary:createtablesql

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
dictionary:createtablesql [2015/08/23 22:13] – created mnewnhamv5:dictionary:createtablesql [2016/03/05 01:55] (current) – [$tableOptions] mnewnham
Line 1: Line 1:
 ====== createTableSql ====== ====== createTableSql ======
 +~~NOTOC~~
 <WRAP right box> <WRAP right box>
 == See Also == == See Also ==
-[[dictionary:addcolumnsql|addColumnSql()]]+ 
 +[[v5:dictionary:column_attributes|Column Attributes Reference]]  
 + 
 +[[v5:dictionary:executesqlarray|executeSqlArray()]]
  
 == Syntax == == Syntax ==
-  string createTableSql (+  array createTableSql (
       string $tableName,       string $tableName,
       string $fields,       string $fields,
Line 12: Line 16:
 </WRAP> </WRAP>
 ===== Description ===== ===== Description =====
-The method ''createTableSql()'' creates a new table in the database and populates it with columns defined in ''$fields'' option.+The method ''createTableSql()'' creates the SQL statement necessary to create a table in the database and populates it with columns defined in ''$fields'' option. In order to create the table, the output of the command must be executed with the [[v5:dictionary:executesqlarray|executeSqlArray()]] method. 
 ===== Parameters ===== ===== Parameters =====
 ==== $tableName ===== ==== $tableName =====
 The table name must not yet exist in the database. Names must conform to any database specific requirements such as length limitations and name casing. The table name must not yet exist in the database. Names must conform to any database specific requirements such as length limitations and name casing.
 +---------
 +<WRAP right tip 200px>
 +$fields is a string, not an array
 +</WRAP>
 ==== $fields ==== ==== $fields ====
-A string holding information regarding the columns to create in the new table. For a detailed description of the format of this string, see [[dictionary:addcolumnsql|addColumnSql()]].+A string holding information regarding the columns to create in the new table. For a detailed description of the format of this string, see [[v5:dictionary:addcolumnsql|addColumnSql()]].
 ==== $tableOptions ==== ==== $tableOptions ====
-An optional array of information that adds information regarding the table creation. This array may also contain field specific information, for example information pertaining to auto-increment fields.+An optional array of information that adds information regarding the table creation. This array may also contain field specific information, for example information pertaining to auto-increment fields. Another feature of this option is that it can be used to provide database specific options to the table itself, e.g.
  
-===== Usage ===== 
 <code php> <code php>
  
 +$tabname = 'TEST';
 +$flds    = 'column definitions....';
 +$opts    = array('MYSQL'=>"ENGINE INNODB,COMPRESSION 'ZLIB'",
 +                 'DB2'=>'TABLESPACE LARGE_SPACE');
  
-# Then create a data dictionary object, using this connection+$sql = $db->createTableSql($tabname,$flds,$opts); 
 +</code> 
 + 
 +In the example above, the values provided in ''$opts'' will be added to the table if the **data provider** matches that of the currently attached database. 
 +===== Usage ===== 
 +<code php> 
 +/* 
 +create a data dictionary object, using the database connection 
 +*/
 $dict = NewDataDictionary($db); $dict = NewDataDictionary($db);
  
Line 37: Line 57:
  
 /* /*
-* create the SQL statement necessary to create the table+* create the SQL statement necessary to create the table and its columns
 */ */
 $sqlarray = $dict->createTableSQL($tabname, $flds); $sqlarray = $dict->createTableSQL($tabname, $flds);
 +
 +/*
 + * This returns (for DB2)
 +Array
 +(
 +    [0] => CREATE TABLE UCTABLE (
 +           COL1         VARCHAR(32) DEFAULT 'abc' NOT NULL,
 +           COL2         INTEGER DEFAULT 0,
 +           COL3         DECIMAL(12,2)
 +           )
 +)
 +*/
 +
  
 /* /*
Line 45: Line 78:
 */ */
 $dict->executeSqlArray($sqlarray); $dict->executeSqlArray($sqlarray);
-</code> 
 </code> </code>
  
v5/dictionary/createtablesql.1440360798.txt.gz · Last modified: 2017/04/21 11:28 (external edit)