blob: 48642ad7ed35f2f317410d1c904ffe1d10f3ab2a [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
9
10****************************
11Initializing the Forge Class
12****************************
13
14.. important:: In order to initialize the Forge class, your database
15 driver must already be running, since the forge class relies on it.
16
17Load the Forge Class as follows::
18
19 $this->load->dbforge()
20
Andrey Andreeveaa60c72012-11-06 01:11:22 +020021You can also pass another database object to the DB Forge loader, in case
22the database you want to manage isn't the default one::
23
24 $this->myforge = $this->load->dbforge($this->other_db, TRUE);
25
26In the above example, we're passing a custom database object as the first
27parameter and then tell it to return the dbforge object, instead of
28assigning it directly to ``$this->dbforge``.
29
30.. note:: Both of the parameters can be used individually, just pass an empty
31 value as the first one if you wish to skip it.
32
33Once initialized you will access the methods using the ``$this->dbforge``
Derek Jones8ede1a22011-10-05 13:34:52 -050034object::
35
Andrey Andreeveaa60c72012-11-06 01:11:22 +020036 $this->dbforge->some_method();
Derek Jones8ede1a22011-10-05 13:34:52 -050037
38$this->dbforge->create_database('db_name')
Andrey Andreeveaa60c72012-11-06 01:11:22 +020039==========================================
Derek Jones8ede1a22011-10-05 13:34:52 -050040
41Permits you to create the database specified in the first parameter.
42Returns TRUE/FALSE based on success or failure::
43
Joseph Wensleyf24f4042011-10-06 22:53:29 -040044 if ($this->dbforge->create_database('my_db'))
45 {
46 echo 'Database created!';
47 }
Derek Jones8ede1a22011-10-05 13:34:52 -050048
49$this->dbforge->drop_database('db_name')
50==========================================
51
52Permits you to drop the database specified in the first parameter.
53Returns TRUE/FALSE based on success or failure::
54
Joseph Wensleyf24f4042011-10-06 22:53:29 -040055 if ($this->dbforge->drop_database('my_db'))
56 {
57 echo 'Database deleted!';
58 }
Derek Jones8ede1a22011-10-05 13:34:52 -050059
60****************************
61Creating and Dropping Tables
62****************************
63
64There are several things you may wish to do when creating tables. Add
65fields, add keys to the table, alter columns. CodeIgniter provides a
66mechanism for this.
67
68Adding fields
69=============
70
71Fields are created via an associative array. Within the array you must
72include a 'type' key that relates to the datatype of the field. For
73example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR)
74also require a 'constraint' key.
75
76::
77
Joseph Wensleyf24f4042011-10-06 22:53:29 -040078 $fields = array(
79 'users' => array(
80 'type' => 'VARCHAR',
81 'constraint' => '100',
82 ),
83 );
84 // will translate to "users VARCHAR(100)" when the field is added.
Derek Jones8ede1a22011-10-05 13:34:52 -050085
86
87Additionally, the following key/values can be used:
88
89- unsigned/true : to generate "UNSIGNED" in the field definition.
90- default/value : to generate a default value in the field definition.
91- null/true : to generate "NULL" in the field definition. Without this,
92 the field will default to "NOT NULL".
93- auto_increment/true : generates an auto_increment flag on the
94 field. Note that the field type must be a type that supports this,
95 such as integer.
96
97::
98
Joseph Wensleyf24f4042011-10-06 22:53:29 -040099 $fields = array(
100 'blog_id' => array(
101 'type' => 'INT',
102 'constraint' => 5,
103 'unsigned' => TRUE,
104 'auto_increment' => TRUE
105 ),
106 'blog_title' => array(
107 'type' => 'VARCHAR',
108 'constraint' => '100',
109 ),
110 'blog_author' => array(
111 'type' =>'VARCHAR',
112 'constraint' => '100',
113 'default' => 'King of Town',
114 ),
115 'blog_description' => array(
116 'type' => 'TEXT',
117 'null' => TRUE,
118 ),
119 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
121
122After the fields have been defined, they can be added using
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200123``$this->dbforge->add_field($fields);`` followed by a call to the
124``create_table()`` method.
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
126$this->dbforge->add_field()
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200127---------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200129The add fields method will accept the above array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
131Passing strings as fields
132-------------------------
133
134If you know exactly how you want a field to be created, you can pass the
135string into the field definitions with add_field()
136
137::
138
139 $this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'");
140
141
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400142.. note:: Multiple calls to add_field() are cumulative.
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
144Creating an id field
145--------------------
146
147There is a special exception for creating id fields. A field with type
148id will automatically be assinged as an INT(9) auto_incrementing
149Primary Key.
150
151::
152
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400153 $this->dbforge->add_field('id');
154 // gives id INT(9) NOT NULL AUTO_INCREMENT
Derek Jones8ede1a22011-10-05 13:34:52 -0500155
156
157Adding Keys
158===========
159
160Generally speaking, you'll want your table to have Keys. This is
161accomplished with $this->dbforge->add_key('field'). An optional second
162parameter set to TRUE will make it a primary key. Note that add_key()
163must be followed by a call to create_table().
164
165Multiple column non-primary keys must be sent as an array. Sample output
166below is for MySQL.
167
168::
169
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400170 $this->dbforge->add_key('blog_id', TRUE);
171 // gives PRIMARY KEY `blog_id` (`blog_id`)
172
173 $this->dbforge->add_key('blog_id', TRUE);
174 $this->dbforge->add_key('site_id', TRUE);
175 // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`)
176
177 $this->dbforge->add_key('blog_name');
178 // gives KEY `blog_name` (`blog_name`)
179
180 $this->dbforge->add_key(array('blog_name', 'blog_label'));
181 // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`)
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
183
184Creating a table
185================
186
187After fields and keys have been declared, you can create a new table
188with
189
190::
191
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400192 $this->dbforge->create_table('table_name');
193 // gives CREATE TABLE table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500194
195
196An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause
197into the definition
198
199::
200
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400201 $this->dbforge->create_table('table_name', TRUE);
202 // gives CREATE TABLE IF NOT EXISTS table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500203
Andrey Andreev27f798b2014-01-20 18:19:13 +0200204You could also pass optional table attributes, such as MySQL's ``ENGINE``::
205
206 $attributes = array('ENGINE' => 'InnoDB');
207 $this->dbforge->create_table('table_name', FALSE, $attributes);
208 // produces: CREATE TABLE `table_name` (...) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
209
210.. note:: Unless you specify the ``CHARACTER SET`` and/or ``COLLATE`` attributes,
211 ``create_table()`` will always add them with your configured *char_set*
212 and *dbcollat* values, as long as they are not empty (MySQL only).
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
214Dropping a table
215================
216
Andrey Andreeva287a342012-11-05 23:19:59 +0200217Execute a DROP TABLE statement and optionally add an IF EXISTS clause.
Derek Jones8ede1a22011-10-05 13:34:52 -0500218
219::
220
Andrey Andreeva287a342012-11-05 23:19:59 +0200221 // Produces: DROP TABLE table_name
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400222 $this->dbforge->drop_table('table_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500223
Andrey Andreeva287a342012-11-05 23:19:59 +0200224 // Produces: DROP TABLE IF EXISTS table_name
225 $this->dbforge->drop_table('table_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500226
227Renaming a table
228================
229
230Executes a TABLE rename
231
232::
233
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400234 $this->dbforge->rename_table('old_table_name', 'new_table_name');
235 // gives ALTER TABLE old_table_name RENAME TO new_table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500236
237
238****************
239Modifying Tables
240****************
241
242$this->dbforge->add_column()
Andrey Andreevb67277b2012-11-12 12:51:14 +0200243============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500244
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200245The ``add_column()`` method is used to modify an existing table. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500246accepts the same field array as above, and can be used for an unlimited
247number of additional fields.
248
249::
250
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400251 $fields = array(
252 'preferences' => array('type' => 'TEXT')
253 );
254 $this->dbforge->add_column('table_name', $fields);
Andrey Andreevb67277b2012-11-12 12:51:14 +0200255 // Executes: ALTER TABLE table_name ADD preferences TEXT
256
257If you are using MySQL or CUBIRD, then you can take advantage of their
258AFTER and FIRST clauses to position the new column.
259
260Examples::
261
262 // Will place the new column after the `another_field` column:
263 $fields = array(
264 'preferences' => array('type' => 'TEXT', 'after' => 'another_field')
265 );
266
267 // Will place the new column at the start of the table definition:
268 $fields = array(
269 'preferences' => array('type' => 'TEXT', 'first' => TRUE)
270 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500271
Derek Jones8ede1a22011-10-05 13:34:52 -0500272$this->dbforge->drop_column()
Andrey Andreevb67277b2012-11-12 12:51:14 +0200273=============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500274
275Used to remove a column from a table.
276
277::
278
279 $this->dbforge->drop_column('table_name', 'column_to_drop');
280
281
282$this->dbforge->modify_column()
Andrey Andreevb67277b2012-11-12 12:51:14 +0200283===============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500284
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200285The usage of this method is identical to ``add_column()``, except it
Derek Jones8ede1a22011-10-05 13:34:52 -0500286alters an existing column rather than adding a new one. In order to
287change the name you can add a "name" key into the field defining array.
288
289::
290
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400291 $fields = array(
292 'old_name' => array(
293 'name' => 'new_name',
294 'type' => 'TEXT',
295 ),
296 );
297 $this->dbforge->modify_column('table_name', $fields);
298 // gives ALTER TABLE table_name CHANGE old_name new_name TEXT