Db create table: Difference between revisions From Online Manual

Jump to: navigation, search
m (refresh SMW properties)
No edit summary
 
Line 4: Line 4:
|description=This function allows for creating a table. You can not create a SMF default table.
|description=This function allows for creating a table. You can not create a SMF default table.
|parameters={{parmdesc
|parameters={{parmdesc
   |table_name|d1=(''string'') the name of an existing table
   |table_name|d1=(''string'') the name of the table you want to create
   |columns|d2=(''array'') a multi-dimensional array containing the columns to be in the table. The first-level elements are used as individual columns, depicted by the example. The second-level items are the column details as seen below:{{parmdesc
   |columns|d2=(''array'') a multi-dimensional array containing the columns to be in the table. The first-level elements are used as individual columns, depicted by the example. The second-level items are the column details as seen below:{{parmdesc
     |name|d1=(''string'') the name of the column
     |name|d1=(''string'') the name of the column

Latest revision as of 12:45, 22 December 2013

Usage:

$smcFunc['db_create_table'] (table_name, columns, indexes, parameters, if_exists, error)


Description: This function allows for creating a table. You can not create a SMF default table.

Parameters:

  • table_name (string) the name of the table you want to create
  • columns (array) a multi-dimensional array containing the columns to be in the table. The first-level elements are used as individual columns, depicted by the example. The second-level items are the column details as seen below:
    • name (string) the name of the column
    • type (string) the type of the column (e.g. int, tinyint, varchar, text, etc.)
    • size (integer) defines the size of the column (if required by type)
    • null (boolean) whether to use null or not null
    • default (integer) or string depending on type) should contain the default value for the column
    • auto (boolean) whether the column uses auto_increment or not
    • unsigned (boolean) specifies whether the column is unsigned or not (applicable only to MySQL).
  • indexes a multi-dimensional array containing the indexes to be in the table. The first-level elements are used as individual indexes, depicted by the example. The second-level items are the index details as seen below:
    • name (string) the name of index, can be left blank to use first column name.
    • columns (array) the column names
  • parameters (array) deprecated as of 2.0 RC3, leave empty
  • if_exists (string) what to do if an error was encountered, default fatal
  • error

Return: (bool) false if the table name was a SMF table.

Example:

$columns = array(
	array(
		'name' => 'id_article',
		'type' => 'int',
		'size' => 10,
		'unsigned' => true,
		'auto' => true,
	),
	array(
		'name' => 'id_category',
		'type' => 'int',
		'size' => 10,
		'unsigned' => true,
	),
	array(
		'name' => 'poster_time',
		'type' => 'int',
		'size' => 10,
		'unsigned' => true,
	),
	array(
		'name' => 'content',
		'type' => 'text',
	),
);

$indexes = array(
	array(
		'type' => 'primary',
		'columns' => array('id_article')
	),
	array(
		'columns' => array('id_category')
	),
);

$smcFunc['db_create_table']('{db_prefix}articles', $columns, $indexes, array(), 'update_remove');

$columns = array(
	array(
		'name' => 'id_category',
		'type' => 'int',
		'size' => 10,
		'unsigned' => true,
		'auto' => true,
	),
	array(
		'name' => 'name',
		'type' => 'tinytext',
	),
);

$indexes = array(
	array(
		'type' => 'primary',
		'columns' => array('id_category')
	),
);

$smcFunc['db_create_table']('{db_prefix}article_categories', $columns, $indexes, array(), 'update_remove');



Advertisement: