blob: a875f741883d2fa67ce77ebcc00df901b4d5fd75 [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
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400146.. note:: Multiple calls to add_field() are cumulative.
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
148Creating an id field
149--------------------
150
151There is a special exception for creating id fields. A field with type
gnb5a4b672015-06-23 11:41:57 -0500152id will automatically be assigned as an INT(9) auto_incrementing
Derek Jones8ede1a22011-10-05 13:34:52 -0500153Primary Key.
154
155::
156
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400157 $this->dbforge->add_field('id');
158 // gives id INT(9) NOT NULL AUTO_INCREMENT
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
160
161Adding Keys
162===========
163
164Generally speaking, you'll want your table to have Keys. This is
165accomplished with $this->dbforge->add_key('field'). An optional second
166parameter set to TRUE will make it a primary key. Note that add_key()
167must be followed by a call to create_table().
168
169Multiple column non-primary keys must be sent as an array. Sample output
170below is for MySQL.
171
172::
173
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400174 $this->dbforge->add_key('blog_id', TRUE);
175 // gives PRIMARY KEY `blog_id` (`blog_id`)
176
177 $this->dbforge->add_key('blog_id', TRUE);
178 $this->dbforge->add_key('site_id', TRUE);
179 // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`)
180
181 $this->dbforge->add_key('blog_name');
182 // gives KEY `blog_name` (`blog_name`)
183
184 $this->dbforge->add_key(array('blog_name', 'blog_label'));
185 // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`)
Derek Jones8ede1a22011-10-05 13:34:52 -0500186
James L Parry4828f892014-11-25 12:53:53 -0800187
Derek Jones8ede1a22011-10-05 13:34:52 -0500188Creating a table
189================
190
191After fields and keys have been declared, you can create a new table
192with
193
194::
195
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400196 $this->dbforge->create_table('table_name');
197 // gives CREATE TABLE table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
199
200An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause
201into the definition
202
203::
204
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400205 $this->dbforge->create_table('table_name', TRUE);
206 // gives CREATE TABLE IF NOT EXISTS table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500207
Andrey Andreev27f798b2014-01-20 18:19:13 +0200208You could also pass optional table attributes, such as MySQL's ``ENGINE``::
209
210 $attributes = array('ENGINE' => 'InnoDB');
211 $this->dbforge->create_table('table_name', FALSE, $attributes);
212 // produces: CREATE TABLE `table_name` (...) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
213
214.. note:: Unless you specify the ``CHARACTER SET`` and/or ``COLLATE`` attributes,
215 ``create_table()`` will always add them with your configured *char_set*
216 and *dbcollat* values, as long as they are not empty (MySQL only).
Derek Jones8ede1a22011-10-05 13:34:52 -0500217
James L Parry4828f892014-11-25 12:53:53 -0800218
Derek Jones8ede1a22011-10-05 13:34:52 -0500219Dropping a table
220================
221
Andrey Andreeva287a342012-11-05 23:19:59 +0200222Execute a DROP TABLE statement and optionally add an IF EXISTS clause.
Derek Jones8ede1a22011-10-05 13:34:52 -0500223
224::
225
Andrey Andreeva287a342012-11-05 23:19:59 +0200226 // Produces: DROP TABLE table_name
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400227 $this->dbforge->drop_table('table_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500228
Andrey Andreeva287a342012-11-05 23:19:59 +0200229 // Produces: DROP TABLE IF EXISTS table_name
LouisMilotte1db6da32015-04-04 03:22:12 -0700230 $this->dbforge->drop_table('table_name',TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500231
James L Parry4828f892014-11-25 12:53:53 -0800232
Derek Jones8ede1a22011-10-05 13:34:52 -0500233Renaming a table
234================
235
236Executes a TABLE rename
237
238::
239
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400240 $this->dbforge->rename_table('old_table_name', 'new_table_name');
241 // gives ALTER TABLE old_table_name RENAME TO new_table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500242
James L Parry4828f892014-11-25 12:53:53 -0800243
Derek Jones8ede1a22011-10-05 13:34:52 -0500244****************
245Modifying Tables
246****************
247
James L Parry4828f892014-11-25 12:53:53 -0800248Adding a Column to a Table
249==========================
250
251**$this->dbforge->add_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500252
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200253The ``add_column()`` method is used to modify an existing table. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500254accepts the same field array as above, and can be used for an unlimited
255number of additional fields.
256
257::
258
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400259 $fields = array(
260 'preferences' => array('type' => 'TEXT')
261 );
262 $this->dbforge->add_column('table_name', $fields);
Andrey Andreevb67277b2012-11-12 12:51:14 +0200263 // Executes: ALTER TABLE table_name ADD preferences TEXT
264
265If you are using MySQL or CUBIRD, then you can take advantage of their
266AFTER and FIRST clauses to position the new column.
267
268Examples::
269
270 // Will place the new column after the `another_field` column:
271 $fields = array(
272 'preferences' => array('type' => 'TEXT', 'after' => 'another_field')
273 );
274
275 // Will place the new column at the start of the table definition:
276 $fields = array(
277 'preferences' => array('type' => 'TEXT', 'first' => TRUE)
278 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500279
James L Parry4828f892014-11-25 12:53:53 -0800280
281Dropping a Column From a Table
282==============================
283
284**$this->dbforge->drop_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500285
286Used to remove a column from a table.
287
288::
289
290 $this->dbforge->drop_column('table_name', 'column_to_drop');
291
292
James L Parry4828f892014-11-25 12:53:53 -0800293
294Modifying a Column in a Table
295=============================
296
297**$this->dbforge->modify_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500298
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200299The usage of this method is identical to ``add_column()``, except it
Derek Jones8ede1a22011-10-05 13:34:52 -0500300alters an existing column rather than adding a new one. In order to
301change the name you can add a "name" key into the field defining array.
302
303::
304
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400305 $fields = array(
306 'old_name' => array(
307 'name' => 'new_name',
308 'type' => 'TEXT',
309 ),
310 );
311 $this->dbforge->modify_column('table_name', $fields);
James L Parry4828f892014-11-25 12:53:53 -0800312 // gives ALTER TABLE table_name CHANGE old_name new_name TEXT
313
James L Parryd96616c2014-12-03 01:17:04 -0800314
315***************
316Class Reference
317***************
318
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200319.. php:class:: CI_DB_forge
James L Parryd96616c2014-12-03 01:17:04 -0800320
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200321 .. php:method:: add_column($table[, $field = array()[, $_after = NULL]])
James L Parryd96616c2014-12-03 01:17:04 -0800322
Andrey Andreevb9061492014-12-04 16:33:24 +0200323 :param string $table: Table name to add the column to
324 :param array $field: Column definition(s)
James L Parryd96616c2014-12-03 01:17:04 -0800325 :param string $_after: Column for AFTER clause (deprecated)
Andrey Andreevb9061492014-12-04 16:33:24 +0200326 :returns: TRUE on success, FALSE on failure
327 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800328
Andrey Andreevb9061492014-12-04 16:33:24 +0200329 Adds a column to a table. Usage: See `Adding a Column to a Table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800330
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200331 .. php:method:: add_field($field)
James L Parryd96616c2014-12-03 01:17:04 -0800332
Andrey Andreevb9061492014-12-04 16:33:24 +0200333 :param array $field: Field definition to add
334 :returns: CI_DB_forge instance (method chaining)
335 :rtype: CI_DB_forge
James L Parryd96616c2014-12-03 01:17:04 -0800336
Andrey Andreevb9061492014-12-04 16:33:24 +0200337 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 -0800338
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200339 .. php:method:: add_key($key[, $primary = FALSE])
James L Parryd96616c2014-12-03 01:17:04 -0800340
341 :param array $key: Name of a key field
Andrey Andreevb9061492014-12-04 16:33:24 +0200342 :param bool $primary: Set to TRUE if it should be a primary key or a regular one
343 :returns: CI_DB_forge instance (method chaining)
344 :rtype: CI_DB_forge
James L Parryd96616c2014-12-03 01:17:04 -0800345
Andrey Andreevb9061492014-12-04 16:33:24 +0200346 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 -0800347
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200348 .. php:method:: create_database($db_name)
James L Parryd96616c2014-12-03 01:17:04 -0800349
350 :param string $db_name: Name of the database to create
Andrey Andreevb9061492014-12-04 16:33:24 +0200351 :returns: TRUE on success, FALSE on failure
352 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800353
Andrey Andreevb9061492014-12-04 16:33:24 +0200354 Creates a new database. Usage: See `Creating and Dropping Databases`_.
James L Parryd96616c2014-12-03 01:17:04 -0800355
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200356 .. php:method:: create_table($table[, $if_not_exists = FALSE[, array $attributes = array()]])
James L Parryd96616c2014-12-03 01:17:04 -0800357
358 :param string $table: Name of the table to create
Andrey Andreevb9061492014-12-04 16:33:24 +0200359 :param string $if_not_exists: Set to TRUE to add an 'IF NOT EXISTS' clause
360 :param string $attributes: An associative array of table attributes
361 :returns: TRUE on success, FALSE on failure
362 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800363
Andrey Andreevb9061492014-12-04 16:33:24 +0200364 Creates a new table. Usage: See `Creating a table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800365
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200366 .. php:method:: drop_column($table, $column_name)
James L Parryd96616c2014-12-03 01:17:04 -0800367
368 :param string $table: Table name
Andrey Andreevb9061492014-12-04 16:33:24 +0200369 :param array $column_name: The column name to drop
370 :returns: TRUE on success, FALSE on failure
371 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800372
Andrey Andreevb9061492014-12-04 16:33:24 +0200373 Drops a column from a table. Usage: See `Dropping a Column From a Table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800374
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200375 .. php:method:: drop_database($db_name)
James L Parryd96616c2014-12-03 01:17:04 -0800376
377 :param string $db_name: Name of the database to drop
Andrey Andreevb9061492014-12-04 16:33:24 +0200378 :returns: TRUE on success, FALSE on failure
379 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800380
Andrey Andreevb9061492014-12-04 16:33:24 +0200381 Drops a database. Usage: See `Creating and Dropping Databases`_.
James L Parryd96616c2014-12-03 01:17:04 -0800382
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200383 .. php:method:: drop_table($table_name[, $if_exists = FALSE])
James L Parryd96616c2014-12-03 01:17:04 -0800384
Andrey Andreevb9061492014-12-04 16:33:24 +0200385 :param string $table: Name of the table to drop
386 :param string $if_exists: Set to TRUE to add an 'IF EXISTS' clause
387 :returns: TRUE on success, FALSE on failure
388 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800389
Andrey Andreevb9061492014-12-04 16:33:24 +0200390 Drops a table. Usage: See `Dropping a table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800391
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200392 .. php:method:: modify_column($table, $field)
James L Parryd96616c2014-12-03 01:17:04 -0800393
394 :param string $table: Table name
Andrey Andreevb9061492014-12-04 16:33:24 +0200395 :param array $field: Column definition(s)
396 :returns: TRUE on success, FALSE on failure
397 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800398
Andrey Andreevb9061492014-12-04 16:33:24 +0200399 Modifies a table column. Usage: See `Modifying a Column in a Table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800400
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200401 .. php:method:: rename_table($table_name, $new_table_name)
James L Parryd96616c2014-12-03 01:17:04 -0800402
Andrey Andreevb9061492014-12-04 16:33:24 +0200403 :param string $table: Current of the table
James L Parryd96616c2014-12-03 01:17:04 -0800404 :param string $new_table_name: New name of the table
Andrey Andreevb9061492014-12-04 16:33:24 +0200405 :returns: TRUE on success, FALSE on failure
406 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800407
LouisMilotte1db6da32015-04-04 03:22:12 -0700408 Renames a table. Usage: See `Renaming a table`_.