blob: ca904ed005ed42797f0e6fb2029e6ffc1030b35d [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
204
205Dropping a table
206================
207
Andrey Andreeva287a342012-11-05 23:19:59 +0200208Execute a DROP TABLE statement and optionally add an IF EXISTS clause.
Derek Jones8ede1a22011-10-05 13:34:52 -0500209
210::
211
Andrey Andreeva287a342012-11-05 23:19:59 +0200212 // Produces: DROP TABLE table_name
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400213 $this->dbforge->drop_table('table_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500214
Andrey Andreeva287a342012-11-05 23:19:59 +0200215 // Produces: DROP TABLE IF EXISTS table_name
216 $this->dbforge->drop_table('table_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500217
218Renaming a table
219================
220
221Executes a TABLE rename
222
223::
224
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400225 $this->dbforge->rename_table('old_table_name', 'new_table_name');
226 // gives ALTER TABLE old_table_name RENAME TO new_table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500227
228
229****************
230Modifying Tables
231****************
232
233$this->dbforge->add_column()
Andrey Andreevb67277b2012-11-12 12:51:14 +0200234============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500235
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200236The ``add_column()`` method is used to modify an existing table. It
Derek Jones8ede1a22011-10-05 13:34:52 -0500237accepts the same field array as above, and can be used for an unlimited
238number of additional fields.
239
240::
241
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400242 $fields = array(
243 'preferences' => array('type' => 'TEXT')
244 );
245 $this->dbforge->add_column('table_name', $fields);
Andrey Andreevb67277b2012-11-12 12:51:14 +0200246 // Executes: ALTER TABLE table_name ADD preferences TEXT
247
248If you are using MySQL or CUBIRD, then you can take advantage of their
249AFTER and FIRST clauses to position the new column.
250
251Examples::
252
253 // Will place the new column after the `another_field` column:
254 $fields = array(
255 'preferences' => array('type' => 'TEXT', 'after' => 'another_field')
256 );
257
258 // Will place the new column at the start of the table definition:
259 $fields = array(
260 'preferences' => array('type' => 'TEXT', 'first' => TRUE)
261 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500262
Derek Jones8ede1a22011-10-05 13:34:52 -0500263$this->dbforge->drop_column()
Andrey Andreevb67277b2012-11-12 12:51:14 +0200264=============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500265
266Used to remove a column from a table.
267
268::
269
270 $this->dbforge->drop_column('table_name', 'column_to_drop');
271
272
273$this->dbforge->modify_column()
Andrey Andreevb67277b2012-11-12 12:51:14 +0200274===============================
Derek Jones8ede1a22011-10-05 13:34:52 -0500275
Andrey Andreeveaa60c72012-11-06 01:11:22 +0200276The usage of this method is identical to ``add_column()``, except it
Derek Jones8ede1a22011-10-05 13:34:52 -0500277alters an existing column rather than adding a new one. In order to
278change the name you can add a "name" key into the field defining array.
279
280::
281
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400282 $fields = array(
283 'old_name' => array(
284 'name' => 'new_name',
285 'type' => 'TEXT',
286 ),
287 );
288 $this->dbforge->modify_column('table_name', $fields);
289 // gives ALTER TABLE table_name CHANGE old_name new_name TEXT