sql_create

The sql_create() function is used to create an SQL table according to the schema provided.

It accepts 7 parameters:

  • $nom is the name of the table to create
  • $champs is an array of column descriptions
  • $clefs is an array of key descriptions
  • $autoinc: if a field is to be a primary key and is numeric, then the auto-increment property will be added. false by default.
  • $temporary: is this a temporary table? Default value: false
  • $serveur,
  • $option

It is used as shown below:

sql_create("spip_tables",
	array(
		"id_table" => "bigint(20) NOT NULL default '0'",
		"column1"=> "varchar(3) NOT NULL default 'oui'",
		"column2"=> "text NOT NULL default ''"
	),
	array(
		'PRIMARY KEY' => "id_table",
		'KEY column1' => "column1"
	)
);

As a general rule, plugins should declare their SQL tables using the pipelines intended for the purpose: declarer_tables_principales and declarer_tables_auxiliaires, and use the creer_base() or maj_tables('spip_tables') functions during installation of each plugin, which will call the sql_create() function when necessary. Read more on this topic here: "Table updates and installation".

Example

Example of creating a "spip_mots_tordus" table which will be a link with "spip_tordus". Note that the primary key is composed from 2 columns:

sql_create("spip_mots_tordus",
	array(
		"id_mot" => "bigint(20) NOT NULL default '0'",
		"id_tordu"=> "bigint(20) NOT NULL default '0'"
	),
	array(
		'PRIMARY KEY' => "id_tordu,id_mot"
	)
);

Author Mark Baber Published : Updated : 12/03/23

Translations : English, français