blob: 5af4f2248436de9fee6ca2ad9e76ba796385ed9f [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.
Andrey Andreeve8bcc9e2016-02-02 14:01:50 +0200100- unique/true : to generate a unique key for the field definition.
Derek Jones8ede1a22011-10-05 13:34:52 -0500101
102::
103
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400104 $fields = array(
105 'blog_id' => array(
106 'type' => 'INT',
107 'constraint' => 5,
108 'unsigned' => TRUE,
109 'auto_increment' => TRUE
110 ),
111 'blog_title' => array(
112 'type' => 'VARCHAR',
113 'constraint' => '100',
Andrey Andreeve8bcc9e2016-02-02 14:01:50 +0200114 'unique' => TRUE,
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400115 ),
116 'blog_author' => array(
117 'type' =>'VARCHAR',
118 'constraint' => '100',
119 'default' => 'King of Town',
120 ),
121 'blog_description' => array(
122 'type' => 'TEXT',
123 'null' => TRUE,
124 ),
125 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
127
128After the fields have been defined, they can be added using
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200129``$this->dbforge->add_field($fields);`` followed by a call to the
130``create_table()`` method.
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
James L Parry4828f892014-11-25 12:53:53 -0800132**$this->dbforge->add_field()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200134The add fields method will accept the above array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500135
James L Parry4828f892014-11-25 12:53:53 -0800136
Derek Jones8ede1a22011-10-05 13:34:52 -0500137Passing strings as fields
138-------------------------
139
140If you know exactly how you want a field to be created, you can pass the
141string into the field definitions with add_field()
142
143::
144
145 $this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'");
146
147
Adrian Voicuff50c542015-07-13 11:55:51 +0300148.. note:: Passing raw strings as fields cannot be followed by ``add_key()`` calls on those fields.
Adrian Voicu00cdb812015-07-10 17:04:00 +0300149
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400150.. note:: Multiple calls to add_field() are cumulative.
Derek Jones8ede1a22011-10-05 13:34:52 -0500151
152Creating an id field
153--------------------
154
155There is a special exception for creating id fields. A field with type
gnb5a4b672015-06-23 11:41:57 -0500156id will automatically be assigned as an INT(9) auto_incrementing
Derek Jones8ede1a22011-10-05 13:34:52 -0500157Primary Key.
158
159::
160
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400161 $this->dbforge->add_field('id');
162 // gives id INT(9) NOT NULL AUTO_INCREMENT
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
164
165Adding Keys
166===========
167
168Generally speaking, you'll want your table to have Keys. This is
169accomplished with $this->dbforge->add_key('field'). An optional second
170parameter set to TRUE will make it a primary key. Note that add_key()
171must be followed by a call to create_table().
172
173Multiple column non-primary keys must be sent as an array. Sample output
174below is for MySQL.
175
176::
177
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400178 $this->dbforge->add_key('blog_id', TRUE);
179 // gives PRIMARY KEY `blog_id` (`blog_id`)
Andrey Andreeve8bcc9e2016-02-02 14:01:50 +0200180
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400181 $this->dbforge->add_key('blog_id', TRUE);
182 $this->dbforge->add_key('site_id', TRUE);
183 // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`)
Andrey Andreeve8bcc9e2016-02-02 14:01:50 +0200184
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400185 $this->dbforge->add_key('blog_name');
186 // gives KEY `blog_name` (`blog_name`)
Andrey Andreeve8bcc9e2016-02-02 14:01:50 +0200187
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400188 $this->dbforge->add_key(array('blog_name', 'blog_label'));
189 // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`)
Derek Jones8ede1a22011-10-05 13:34:52 -0500190
James L Parry4828f892014-11-25 12:53:53 -0800191
Derek Jones8ede1a22011-10-05 13:34:52 -0500192Creating a table
193================
194
195After fields and keys have been declared, you can create a new table
196with
197
198::
199
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400200 $this->dbforge->create_table('table_name');
201 // gives CREATE TABLE table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
203
204An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause
205into the definition
206
207::
208
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400209 $this->dbforge->create_table('table_name', TRUE);
210 // gives CREATE TABLE IF NOT EXISTS table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500211
Andrey Andreev27f798b2014-01-20 18:19:13 +0200212You could also pass optional table attributes, such as MySQL's ``ENGINE``::
213
214 $attributes = array('ENGINE' => 'InnoDB');
215 $this->dbforge->create_table('table_name', FALSE, $attributes);
216 // produces: CREATE TABLE `table_name` (...) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
217
218.. note:: Unless you specify the ``CHARACTER SET`` and/or ``COLLATE`` attributes,
219 ``create_table()`` will always add them with your configured *char_set*
220 and *dbcollat* values, as long as they are not empty (MySQL only).
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
James L Parry4828f892014-11-25 12:53:53 -0800222
Derek Jones8ede1a22011-10-05 13:34:52 -0500223Dropping a table
224================
225
Andrey Andreeva287a342012-11-05 23:19:59 +0200226Execute a DROP TABLE statement and optionally add an IF EXISTS clause.
Derek Jones8ede1a22011-10-05 13:34:52 -0500227
228::
229
Andrey Andreeva287a342012-11-05 23:19:59 +0200230 // Produces: DROP TABLE table_name
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400231 $this->dbforge->drop_table('table_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500232
Andrey Andreeva287a342012-11-05 23:19:59 +0200233 // Produces: DROP TABLE IF EXISTS table_name
LouisMilotte1db6da32015-04-04 03:22:12 -0700234 $this->dbforge->drop_table('table_name',TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500235
James L Parry4828f892014-11-25 12:53:53 -0800236
Derek Jones8ede1a22011-10-05 13:34:52 -0500237Renaming a table
238================
239
240Executes a TABLE rename
241
242::
243
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400244 $this->dbforge->rename_table('old_table_name', 'new_table_name');
245 // gives ALTER TABLE old_table_name RENAME TO new_table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500246
James L Parry4828f892014-11-25 12:53:53 -0800247
Derek Jones8ede1a22011-10-05 13:34:52 -0500248****************
249Modifying Tables
250****************
251
James L Parry4828f892014-11-25 12:53:53 -0800252Adding a Column to a Table
253==========================
254
255**$this->dbforge->add_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500256
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200257The ``add_column()`` method is used to modify an existing table. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500258accepts the same field array as above, and can be used for an unlimited
259number of additional fields.
260
261::
262
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400263 $fields = array(
264 'preferences' => array('type' => 'TEXT')
265 );
Andrey Andreeve8bcc9e2016-02-02 14:01:50 +0200266 $this->dbforge->add_column('table_name', $fields);
Andrey Andreevb67277b2012-11-12 12:51:14 +0200267 // Executes: ALTER TABLE table_name ADD preferences TEXT
268
269If you are using MySQL or CUBIRD, then you can take advantage of their
270AFTER and FIRST clauses to position the new column.
271
272Examples::
273
274 // Will place the new column after the `another_field` column:
275 $fields = array(
276 'preferences' => array('type' => 'TEXT', 'after' => 'another_field')
277 );
278
279 // Will place the new column at the start of the table definition:
280 $fields = array(
281 'preferences' => array('type' => 'TEXT', 'first' => TRUE)
282 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500283
James L Parry4828f892014-11-25 12:53:53 -0800284
285Dropping a Column From a Table
286==============================
287
288**$this->dbforge->drop_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500289
290Used to remove a column from a table.
291
292::
293
294 $this->dbforge->drop_column('table_name', 'column_to_drop');
295
296
James L Parry4828f892014-11-25 12:53:53 -0800297
298Modifying a Column in a Table
299=============================
300
301**$this->dbforge->modify_column()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500302
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200303The usage of this method is identical to ``add_column()``, except it
Derek Jones8ede1a22011-10-05 13:34:52 -0500304alters an existing column rather than adding a new one. In order to
305change the name you can add a "name" key into the field defining array.
306
307::
308
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400309 $fields = array(
310 'old_name' => array(
311 'name' => 'new_name',
312 'type' => 'TEXT',
313 ),
314 );
315 $this->dbforge->modify_column('table_name', $fields);
James L Parry4828f892014-11-25 12:53:53 -0800316 // gives ALTER TABLE table_name CHANGE old_name new_name TEXT
317
James L Parryd96616c2014-12-03 01:17:04 -0800318
319***************
320Class Reference
321***************
322
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200323.. php:class:: CI_DB_forge
James L Parryd96616c2014-12-03 01:17:04 -0800324
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200325 .. php:method:: add_column($table[, $field = array()[, $_after = NULL]])
James L Parryd96616c2014-12-03 01:17:04 -0800326
Andrey Andreevb9061492014-12-04 16:33:24 +0200327 :param string $table: Table name to add the column to
328 :param array $field: Column definition(s)
James L Parryd96616c2014-12-03 01:17:04 -0800329 :param string $_after: Column for AFTER clause (deprecated)
Andrey Andreevb9061492014-12-04 16:33:24 +0200330 :returns: TRUE on success, FALSE on failure
331 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800332
Andrey Andreevb9061492014-12-04 16:33:24 +0200333 Adds a column to a table. Usage: See `Adding a Column to a Table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800334
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200335 .. php:method:: add_field($field)
James L Parryd96616c2014-12-03 01:17:04 -0800336
Andrey Andreevb9061492014-12-04 16:33:24 +0200337 :param array $field: Field definition to add
338 :returns: CI_DB_forge instance (method chaining)
339 :rtype: CI_DB_forge
James L Parryd96616c2014-12-03 01:17:04 -0800340
Andrey Andreevb9061492014-12-04 16:33:24 +0200341 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 -0800342
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200343 .. php:method:: add_key($key[, $primary = FALSE])
James L Parryd96616c2014-12-03 01:17:04 -0800344
345 :param array $key: Name of a key field
Andrey Andreevb9061492014-12-04 16:33:24 +0200346 :param bool $primary: Set to TRUE if it should be a primary key or a regular one
347 :returns: CI_DB_forge instance (method chaining)
348 :rtype: CI_DB_forge
James L Parryd96616c2014-12-03 01:17:04 -0800349
Andrey Andreevb9061492014-12-04 16:33:24 +0200350 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 -0800351
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200352 .. php:method:: create_database($db_name)
James L Parryd96616c2014-12-03 01:17:04 -0800353
354 :param string $db_name: Name of the database to create
Andrey Andreevb9061492014-12-04 16:33:24 +0200355 :returns: TRUE on success, FALSE on failure
356 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800357
Andrey Andreevb9061492014-12-04 16:33:24 +0200358 Creates a new database. Usage: See `Creating and Dropping Databases`_.
James L Parryd96616c2014-12-03 01:17:04 -0800359
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200360 .. php:method:: create_table($table[, $if_not_exists = FALSE[, array $attributes = array()]])
James L Parryd96616c2014-12-03 01:17:04 -0800361
362 :param string $table: Name of the table to create
Andrey Andreevb9061492014-12-04 16:33:24 +0200363 :param string $if_not_exists: Set to TRUE to add an 'IF NOT EXISTS' clause
364 :param string $attributes: An associative array of table attributes
365 :returns: TRUE on success, FALSE on failure
366 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800367
Andrey Andreevb9061492014-12-04 16:33:24 +0200368 Creates a new table. Usage: See `Creating a table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800369
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200370 .. php:method:: drop_column($table, $column_name)
James L Parryd96616c2014-12-03 01:17:04 -0800371
372 :param string $table: Table name
Andrey Andreevb9061492014-12-04 16:33:24 +0200373 :param array $column_name: The column name to drop
374 :returns: TRUE on success, FALSE on failure
375 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800376
Andrey Andreevb9061492014-12-04 16:33:24 +0200377 Drops a column from a table. Usage: See `Dropping a Column From a Table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800378
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200379 .. php:method:: drop_database($db_name)
James L Parryd96616c2014-12-03 01:17:04 -0800380
381 :param string $db_name: Name of the database to drop
Andrey Andreevb9061492014-12-04 16:33:24 +0200382 :returns: TRUE on success, FALSE on failure
383 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800384
Andrey Andreevb9061492014-12-04 16:33:24 +0200385 Drops a database. Usage: See `Creating and Dropping Databases`_.
James L Parryd96616c2014-12-03 01:17:04 -0800386
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200387 .. php:method:: drop_table($table_name[, $if_exists = FALSE])
James L Parryd96616c2014-12-03 01:17:04 -0800388
Andrey Andreevb9061492014-12-04 16:33:24 +0200389 :param string $table: Name of the table to drop
390 :param string $if_exists: Set to TRUE to add an 'IF EXISTS' clause
391 :returns: TRUE on success, FALSE on failure
392 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800393
Andrey Andreevb9061492014-12-04 16:33:24 +0200394 Drops a table. Usage: See `Dropping a table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800395
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200396 .. php:method:: modify_column($table, $field)
James L Parryd96616c2014-12-03 01:17:04 -0800397
398 :param string $table: Table name
Andrey Andreevb9061492014-12-04 16:33:24 +0200399 :param array $field: Column definition(s)
400 :returns: TRUE on success, FALSE on failure
401 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800402
Andrey Andreevb9061492014-12-04 16:33:24 +0200403 Modifies a table column. Usage: See `Modifying a Column in a Table`_.
James L Parryd96616c2014-12-03 01:17:04 -0800404
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200405 .. php:method:: rename_table($table_name, $new_table_name)
James L Parryd96616c2014-12-03 01:17:04 -0800406
Andrey Andreevb9061492014-12-04 16:33:24 +0200407 :param string $table: Current of the table
James L Parryd96616c2014-12-03 01:17:04 -0800408 :param string $new_table_name: New name of the table
Andrey Andreevb9061492014-12-04 16:33:24 +0200409 :returns: TRUE on success, FALSE on failure
410 :rtype: bool
James L Parryd96616c2014-12-03 01:17:04 -0800411
LouisMilotte1db6da32015-04-04 03:22:12 -0700412 Renames a table. Usage: See `Renaming a table`_.