db_create_table

From Online Manual

Jump to: navigation, search

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');