blob: 646e3a56eea66c0df094ffa6e9247286ab8e632b [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001####################
2Database Forge Class
3####################
4
Andrey Andreeveaa60c72012-11-06 01:11:22 +02005The Database Forge Class contains methods that help you manage your
Derek Jones8ede1a22011-10-05 13:34:52 -05006database.
7
8.. contents:: Table of Contents
James L Parry4828f892014-11-25 12:53:53 -08009 :depth: 3
Derek Jones8ede1a22011-10-05 13:34:52 -050010
11****************************
12Initializing the Forge Class
13****************************
14
15.. important:: In order to initialize the Forge class, your database
16 driver must already be running, since the forge class relies on it.
17
18Load the Forge Class as follows::
19
20 $this->load->dbforge()
21
Andrey Andreeveaa60c72012-11-06 01:11:22 +020022You can also pass another database object to the DB Forge loader, in case
23the database you want to manage isn't the default one::
24
25 $this->myforge = $this->load->dbforge($this->other_db, TRUE);
26
27In the above example, we're passing a custom database object as the first
28parameter and then tell it to return the dbforge object, instead of
29assigning it directly to ``$this->dbforge``.
30
31.. note:: Both of the parameters can be used individually, just pass an empty
32 value as the first one if you wish to skip it.
33
34Once initialized you will access the methods using the ``$this->dbforge``
Derek Jones8ede1a22011-10-05 13:34:52 -050035object::
36
Andrey Andreeveaa60c72012-11-06 01:11:22 +020037 $this->dbforge->some_method();
Derek Jones8ede1a22011-10-05 13:34:52 -050038
James L Parry4828f892014-11-25 12:53:53 -080039*******************************
40Creating and Dropping Databases
41*******************************
42
43**$this->dbforge->create_database('db_name')**
Derek Jones8ede1a22011-10-05 13:34:52 -050044
45Permits you to create the database specified in the first parameter.
46Returns TRUE/FALSE based on success or failure::
47
Joseph Wensleyf24f4042011-10-06 22:53:29 -040048 if ($this->dbforge->create_database('my_db'))
49 {
50 echo 'Database created!';
51 }
Derek Jones8ede1a22011-10-05 13:34:52 -050052
James L Parry4828f892014-11-25 12:53:53 -080053**$this->dbforge->drop_database('db_name')**
Derek Jones8ede1a22011-10-05 13:34:52 -050054
55Permits you to drop the database specified in the first parameter.
56Returns TRUE/FALSE based on success or failure::
57
Joseph Wensleyf24f4042011-10-06 22:53:29 -040058 if ($this->dbforge->drop_database('my_db'))
59 {
60 echo 'Database deleted!';
61 }
Derek Jones8ede1a22011-10-05 13:34:52 -050062
James L Parry4828f892014-11-25 12:53:53 -080063
Derek Jones8ede1a22011-10-05 13:34:52 -050064****************************
65Creating and Dropping Tables
66****************************
67
68There are several things you may wish to do when creating tables. Add
69fields, add keys to the table, alter columns. CodeIgniter provides a
70mechanism for this.
71
72Adding fields
73=============
74
75Fields are created via an associative array. Within the array you must
76include a 'type' key that relates to the datatype of the field. For
77example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR)
78also require a 'constraint' key.
79
80::
81
Joseph Wensleyf24f4042011-10-06 22:53:29 -040082 $fields = array(
83 'users' => array(
84 'type' => 'VARCHAR',
85 'constraint' => '100',
86 ),
87 );
88 // will translate to "users VARCHAR(100)" when the field is added.
Derek Jones8ede1a22011-10-05 13:34:52 -050089
90
91Additionally, the following key/values can be used:
92
93- unsigned/true : to generate "UNSIGNED" in the field definition.
94- default/value : to generate a default value in the field definition.
95- null/true : to generate "NULL" in the field definition. Without this,
96 the field will default to "NOT NULL".
97- auto_increment/true : generates an auto_increment flag on the
98 field. Note that the field type must be a type that supports this,
99 such as integer.
100
101::
102
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400103 $fields = array(
104 'blog_id' => array(
105 'type' => 'INT',
106 'constraint' => 5,
107 'unsigned' => TRUE,
108 'auto_increment' => TRUE
109 ),
110 'blog_title' => array(
111 'type' => 'VARCHAR',
112 'constraint' => '100',
113 ),
114 'blog_author' => array(
115 'type' =>'VARCHAR',
116 'constraint' => '100',
117 'default' => 'King of Town',
118 ),
119 'blog_description' => array(
120 'type' => 'TEXT',
121 'null' => TRUE,
122 ),
123 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
125
126After the fields have been defined, they can be added using
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200127``$this->dbforge->add_field($fields);`` followed by a call to the
128``create_table()`` method.
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
James L Parry4828f892014-11-25 12:53:53 -0800130**$this->dbforge->add_field()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200132The add fields method will accept the above array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
James L Parry4828f892014-11-25 12:53:53 -0800134
Derek Jones8ede1a22011-10-05 13:34:52 -0500135Passing strings as fields
136-------------------------
137
138If you know exactly how you want a field to be created, you can pass the
139string into the field definitions with add_field()
140
141::
142
143 $this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'");
144
145
Adrian Voicuff50c542015-07-13 11:55:51 +0300146.. note:: Passing raw strings as fields cannot be followed by ``add_key()`` calls on those fields.
Adrian Voicu00cdb812015-07-10 17:04:00 +0300147
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400148.. note:: Multiple calls to add_field() are cumulative.
Derek Jones8ede1a22011-10-05 13:34:52 -0500149
150Creating an id field
151--------------------
152
153There is a special exception for creating id fields. A field with type
gnb5a4b672015-06-23 11:41:57 -0500154id will automatically be assigned as an INT(9) auto_incrementing
Derek Jones8ede1a22011-10-05 13:34:52 -0500155Primary Key.
156
157::
158
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400159 $this->dbforge->add_field('id');
160 // gives id INT(9) NOT NULL AUTO_INCREMENT
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
162
163Adding Keys
164===========
165
166Generally speaking, you'll want your table to have Keys. This is
167accomplished with $this->dbforge->add_key('field'). An optional second
168parameter set to TRUE will make it a primary key. Note that add_key()
169must be followed by a call to create_table().
170
171Multiple column non-primary keys must be sent as an array. Sample output
172below is for MySQL.
173
174::
175
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400176 $this->dbforge->add_key('blog_id', TRUE);
177 // gives PRIMARY KEY `blog_id` (`blog_id`)
178
179 $this->dbforge->add_key('blog_id', TRUE);
180 $this->dbforge->add_key('site_id', TRUE);
181 // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`)
182
183 $this->dbforge->add_key('blog_name');
184 // gives KEY `blog_name` (`blog_name`)
185
186 $this->dbforge->add_key(array('blog_name', 'blog_label'));
187 // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`)
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
James L Parry4828f892014-11-25 12:53:53 -0800189
Derek Jones8ede1a22011-10-05 13:34:52 -0500190Creating a table
191================
192
193After fields and keys have been declared, you can create a new table
194with
195
196::
197
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400198 $this->dbforge->create_table('table_name');
199 // gives CREATE TABLE table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500200
201
202An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause
203into the definition
204
205::
206
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400207 $this->dbforge->create_table('table_name', TRUE);
208 // gives CREATE TABLE IF NOT EXISTS table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500209
Andrey Andreev27f798b2014-01-20 18:19:13 +0200210You could also pass optional table attributes, such as MySQL's ``ENGINE``::
211
212 $attributes = array('ENGINE' => 'InnoDB');
213 $this->dbforge->create_table('table_name', FALSE, $attributes);
214 // produces: CREATE TABLE `table_name` (...) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
215
216.. note:: Unless you specify the ``CHARACTER SET`` and/or ``COLLATE`` attributes,
217 ``create_table()`` will always add them with your configured *char_set*
218 and *dbcollat* values, as long as they are not empty (MySQL only).
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
James L Parry4828f892014-11-25 12:53:53 -0800220
Derek Jones8ede1a22011-10-05 13:34:52 -0500221Dropping a table
222================
223
Andrey Andreeva287a342012-11-05 23:19:59 +0200224Execute a DROP TABLE statement and optionally add an IF EXISTS clause.
Derek Jones8ede1a22011-10-05 13:34:52 -0500225
226::
227
Andrey Andreeva287a342012-11-05 23:19:59 +0200228 // Produces: DROP TABLE table_name
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400229 $this->dbforge->drop_table('table_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500230
Andrey Andreeva287a342012-11-05 23:19:59 +0200231 // Produces: DROP TABLE IF EXISTS table_name
LouisMilotte1db6da32015-04-04 03:22:12 -0700232 $this->dbforge->drop_table('table_name',TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500233
James L Parry4828f892014-11-25 12:53:53 -0800234
Derek Jones8ede1a22011-10-05 13:34:52 -0500235Renaming a table
236================
237
238Executes a TABLE rename
239
240::
241
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400242 $this->dbforge->rename_table('old_table_name', 'new_table_name');
243 // gives ALTER TABLE old_table_name RENAME TO new_table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
James L Parry4828f892014-11-25 12:53:53 -0800245
Derek Jones8ede1a22011-10-05 13:34:52 -0500246****************
247Modifying Tables
248****************
249
James L Parry4828f892014-11-25 12:53:53 -0800250Adding a Column to a Table
251==========================
252
253**$this->dbforge->add_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200255The ``add_column()`` method is used to modify an existing table. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500256accepts the same field array as above, and can be used for an unlimited
257number of additional fields.
258
259::
260
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400261 $fields = array(
262 'preferences' => array('type' => 'TEXT')
263 );
264 $this->dbforge->add_column('table_name', $fields);
Andrey Andreevb67277b2012-11-12 12:51:14 +0200265 // Executes: ALTER TABLE table_name ADD preferences TEXT
266
267If you are using MySQL or CUBIRD, then you can take advantage of their
268AFTER and FIRST clauses to position the new column.
269
270Examples::
271
272 // Will place the new column after the `another_field` column:
273 $fields = array(
274 'preferences' => array('type' => 'TEXT', 'after' => 'another_field')
275 );
276
277 // Will place the new column at the start of the table definition:
278 $fields = array(
279 'preferences' => array('type' => 'TEXT', 'first' => TRUE)
280 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500281
James L Parry4828f892014-11-25 12:53:53 -0800282
283Dropping a Column From a Table
284==============================
285
286**$this->dbforge->drop_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500287
288Used to remove a column from a table.
289
290::
291
292 $this->dbforge->drop_column('table_name', 'column_to_drop');
293
294
James L Parry4828f892014-11-25 12:53:53 -0800295
296Modifying a Column in a Table
297=============================
298
299**$this->dbforge->modify_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500300
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200301The usage of this method is identical to ``add_column()``, except it
Derek Jones8ede1a22011-10-05 13:34:52 -0500302alters an existing column rather than adding a new one. In order to
303change the name you can add a "name" key into the field defining array.
304
305::
306
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400307 $fields = array(
308 'old_name' => array(
309 'name' => 'new_name',
310 'type' => 'TEXT',
311 ),
312 );
313 $this->dbforge->modify_column('table_name', $fields);
James L Parry4828f892014-11-25 12:53:53 -0800314 // gives ALTER TABLE table_name CHANGE old_name new_name TEXT
315
James L Parryd96616c2014-12-03 01:17:04 -0800316
317***************
318Class Reference
319***************
320
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200321.. php:class:: CI_DB_forge
James L Parryd96616c2014-12-03 01:17:04 -0800322
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200323 .. php:method:: add_column($table[, $field = array()[, $_after = NULL]])
James L Parryd96616c2014-12-03 01:17:04 -0800324
Andrey Andreevb9061492014-12-04 16:33:24 +0200325 :param string $table: Table name to add the column to
326 :param array $field: Column definition(s)
James L Parryd96616c2014-12-03 01:17:04 -0800327 :param string $_after: Column for AFTER clause (deprecated)
Andrey Andreevb9061492014-12-04 16:33:24 +0200328 :returns: TRUE on success, FALSE on failure
329 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800330
Andrey Andreevb9061492014-12-04 16:33:24 +0200331 Adds a column to a table. Usage: See `Adding a Column to a Table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800332
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200333 .. php:method:: add_field($field)
James L Parryd96616c2014-12-03 01:17:04 -0800334
Andrey Andreevb9061492014-12-04 16:33:24 +0200335 :param array $field: Field definition to add
336 :returns: CI_DB_forge instance (method chaining)
337 :rtype: CI_DB_forge
James L Parryd96616c2014-12-03 01:17:04 -0800338
Andrey Andreevb9061492014-12-04 16:33:24 +0200339 Adds a field to the set that will be used to create a table. Usage: See `Adding fields`_.
James L Parryd96616c2014-12-03 01:17:04 -0800340
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200341 .. php:method:: add_key($key[, $primary = FALSE])
James L Parryd96616c2014-12-03 01:17:04 -0800342
343 :param array $key: Name of a key field
Andrey Andreevb9061492014-12-04 16:33:24 +0200344 :param bool $primary: Set to TRUE if it should be a primary key or a regular one
345 :returns: CI_DB_forge instance (method chaining)
346 :rtype: CI_DB_forge
James L Parryd96616c2014-12-03 01:17:04 -0800347
Andrey Andreevb9061492014-12-04 16:33:24 +0200348 Adds a key to the set that will be used to create a table. Usage: See `Adding Keys`_.
James L Parryd96616c2014-12-03 01:17:04 -0800349
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200350 .. php:method:: create_database($db_name)
James L Parryd96616c2014-12-03 01:17:04 -0800351
352 :param string $db_name: Name of the database to create
Andrey Andreevb9061492014-12-04 16:33:24 +0200353 :returns: TRUE on success, FALSE on failure
354 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800355
Andrey Andreevb9061492014-12-04 16:33:24 +0200356 Creates a new database. Usage: See `Creating and Dropping Databases`_.
James L Parryd96616c2014-12-03 01:17:04 -0800357
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200358 .. php:method:: create_table($table[, $if_not_exists = FALSE[, array $attributes = array()]])
James L Parryd96616c2014-12-03 01:17:04 -0800359
360 :param string $table: Name of the table to create
Andrey Andreevb9061492014-12-04 16:33:24 +0200361 :param string $if_not_exists: Set to TRUE to add an 'IF NOT EXISTS' clause
362 :param string $attributes: An associative array of table attributes
363 :returns: TRUE on success, FALSE on failure
364 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800365
Andrey Andreevb9061492014-12-04 16:33:24 +0200366 Creates a new table. Usage: See `Creating a table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800367
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200368 .. php:method:: drop_column($table, $column_name)
James L Parryd96616c2014-12-03 01:17:04 -0800369
370 :param string $table: Table name
Andrey Andreevb9061492014-12-04 16:33:24 +0200371 :param array $column_name: The column name to drop
372 :returns: TRUE on success, FALSE on failure
373 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800374
Andrey Andreevb9061492014-12-04 16:33:24 +0200375 Drops a column from a table. Usage: See `Dropping a Column From a Table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800376
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200377 .. php:method:: drop_database($db_name)
James L Parryd96616c2014-12-03 01:17:04 -0800378
379 :param string $db_name: Name of the database to drop
Andrey Andreevb9061492014-12-04 16:33:24 +0200380 :returns: TRUE on success, FALSE on failure
381 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800382
Andrey Andreevb9061492014-12-04 16:33:24 +0200383 Drops a database. Usage: See `Creating and Dropping Databases`_.
James L Parryd96616c2014-12-03 01:17:04 -0800384
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200385 .. php:method:: drop_table($table_name[, $if_exists = FALSE])
James L Parryd96616c2014-12-03 01:17:04 -0800386
Andrey Andreevb9061492014-12-04 16:33:24 +0200387 :param string $table: Name of the table to drop
388 :param string $if_exists: Set to TRUE to add an 'IF EXISTS' clause
389 :returns: TRUE on success, FALSE on failure
390 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800391
Andrey Andreevb9061492014-12-04 16:33:24 +0200392 Drops a table. Usage: See `Dropping a table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800393
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200394 .. php:method:: modify_column($table, $field)
James L Parryd96616c2014-12-03 01:17:04 -0800395
396 :param string $table: Table name
Andrey Andreevb9061492014-12-04 16:33:24 +0200397 :param array $field: Column definition(s)
398 :returns: TRUE on success, FALSE on failure
399 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800400
Andrey Andreevb9061492014-12-04 16:33:24 +0200401 Modifies a table column. Usage: See `Modifying a Column in a Table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800402
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200403 .. php:method:: rename_table($table_name, $new_table_name)
James L Parryd96616c2014-12-03 01:17:04 -0800404
Andrey Andreevb9061492014-12-04 16:33:24 +0200405 :param string $table: Current of the table
James L Parryd96616c2014-12-03 01:17:04 -0800406 :param string $new_table_name: New name of the table
Andrey Andreevb9061492014-12-04 16:33:24 +0200407 :returns: TRUE on success, FALSE on failure
408 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800409
LouisMilotte1db6da32015-04-04 03:22:12 -0700410 Renames a table. Usage: See `Renaming a table`_.