blob: bf17e2918c438240068c702e2797e9d2c3bba0f0 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001####################
2Database Forge Class
3####################
4
5The Database Forge Class contains functions that help you manage your
6database.
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
21Once initialized you will access the functions using the $this->dbforge
22object::
23
24 $this->dbforge->some_function()
25
26$this->dbforge->create_database('db_name')
27============================================
28
29Permits you to create the database specified in the first parameter.
30Returns TRUE/FALSE based on success or failure::
31
Joseph Wensleyf24f4042011-10-06 22:53:29 -040032 if ($this->dbforge->create_database('my_db'))
33 {
34 echo 'Database created!';
35 }
Derek Jones8ede1a22011-10-05 13:34:52 -050036
37$this->dbforge->drop_database('db_name')
38==========================================
39
40Permits you to drop the database specified in the first parameter.
41Returns TRUE/FALSE based on success or failure::
42
Joseph Wensleyf24f4042011-10-06 22:53:29 -040043 if ($this->dbforge->drop_database('my_db'))
44 {
45 echo 'Database deleted!';
46 }
Derek Jones8ede1a22011-10-05 13:34:52 -050047
48****************************
49Creating and Dropping Tables
50****************************
51
52There are several things you may wish to do when creating tables. Add
53fields, add keys to the table, alter columns. CodeIgniter provides a
54mechanism for this.
55
56Adding fields
57=============
58
59Fields are created via an associative array. Within the array you must
60include a 'type' key that relates to the datatype of the field. For
61example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR)
62also require a 'constraint' key.
63
64::
65
Joseph Wensleyf24f4042011-10-06 22:53:29 -040066 $fields = array(
67 'users' => array(
68 'type' => 'VARCHAR',
69 'constraint' => '100',
70 ),
71 );
72 // will translate to "users VARCHAR(100)" when the field is added.
Derek Jones8ede1a22011-10-05 13:34:52 -050073
74
75Additionally, the following key/values can be used:
76
77- unsigned/true : to generate "UNSIGNED" in the field definition.
78- default/value : to generate a default value in the field definition.
79- null/true : to generate "NULL" in the field definition. Without this,
80 the field will default to "NOT NULL".
81- auto_increment/true : generates an auto_increment flag on the
82 field. Note that the field type must be a type that supports this,
83 such as integer.
84
85::
86
Joseph Wensleyf24f4042011-10-06 22:53:29 -040087 $fields = array(
88 'blog_id' => array(
89 'type' => 'INT',
90 'constraint' => 5,
91 'unsigned' => TRUE,
92 'auto_increment' => TRUE
93 ),
94 'blog_title' => array(
95 'type' => 'VARCHAR',
96 'constraint' => '100',
97 ),
98 'blog_author' => array(
99 'type' =>'VARCHAR',
100 'constraint' => '100',
101 'default' => 'King of Town',
102 ),
103 'blog_description' => array(
104 'type' => 'TEXT',
105 'null' => TRUE,
106 ),
107 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
109
110After the fields have been defined, they can be added using
111$this->dbforge->add_field($fields); followed by a call to the
112create_table() function.
113
114$this->dbforge->add_field()
115----------------------------
116
117The add fields function will accept the above array.
118
119Passing strings as fields
120-------------------------
121
122If you know exactly how you want a field to be created, you can pass the
123string into the field definitions with add_field()
124
125::
126
127 $this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'");
128
129
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400130.. note:: Multiple calls to add_field() are cumulative.
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
132Creating an id field
133--------------------
134
135There is a special exception for creating id fields. A field with type
136id will automatically be assinged as an INT(9) auto_incrementing
137Primary Key.
138
139::
140
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400141 $this->dbforge->add_field('id');
142 // gives id INT(9) NOT NULL AUTO_INCREMENT
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
144
145Adding Keys
146===========
147
148Generally speaking, you'll want your table to have Keys. This is
149accomplished with $this->dbforge->add_key('field'). An optional second
150parameter set to TRUE will make it a primary key. Note that add_key()
151must be followed by a call to create_table().
152
153Multiple column non-primary keys must be sent as an array. Sample output
154below is for MySQL.
155
156::
157
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400158 $this->dbforge->add_key('blog_id', TRUE);
159 // gives PRIMARY KEY `blog_id` (`blog_id`)
160
161 $this->dbforge->add_key('blog_id', TRUE);
162 $this->dbforge->add_key('site_id', TRUE);
163 // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`)
164
165 $this->dbforge->add_key('blog_name');
166 // gives KEY `blog_name` (`blog_name`)
167
168 $this->dbforge->add_key(array('blog_name', 'blog_label'));
169 // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`)
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
171
172Creating a table
173================
174
175After fields and keys have been declared, you can create a new table
176with
177
178::
179
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400180 $this->dbforge->create_table('table_name');
181 // gives CREATE TABLE table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500182
183
184An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause
185into the definition
186
187::
188
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400189 $this->dbforge->create_table('table_name', TRUE);
190 // gives CREATE TABLE IF NOT EXISTS table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500191
192
193Dropping a table
194================
195
196Executes a DROP TABLE sql
197
198::
199
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400200 $this->dbforge->drop_table('table_name');
201 // gives DROP TABLE IF EXISTS table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
203
204Renaming a table
205================
206
207Executes a TABLE rename
208
209::
210
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400211 $this->dbforge->rename_table('old_table_name', 'new_table_name');
212 // gives ALTER TABLE old_table_name RENAME TO new_table_name
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
214
215****************
216Modifying Tables
217****************
218
219$this->dbforge->add_column()
220=============================
221
222The add_column() function is used to modify an existing table. It
223accepts the same field array as above, and can be used for an unlimited
224number of additional fields.
225
226::
227
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400228 $fields = array(
229 'preferences' => array('type' => 'TEXT')
230 );
231 $this->dbforge->add_column('table_name', $fields);
232 // gives ALTER TABLE table_name ADD preferences TEXT
Derek Jones8ede1a22011-10-05 13:34:52 -0500233
234An optional third parameter can be used to specify which existing column
235to add the new column after.
236
237::
238
239 $this->dbforge->add_column('table_name', $fields, 'after_field');
240
241
242$this->dbforge->drop_column()
243==============================
244
245Used to remove a column from a table.
246
247::
248
249 $this->dbforge->drop_column('table_name', 'column_to_drop');
250
251
252$this->dbforge->modify_column()
253================================
254
255The usage of this function is identical to add_column(), except it
256alters an existing column rather than adding a new one. In order to
257change the name you can add a "name" key into the field defining array.
258
259::
260
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400261 $fields = array(
262 'old_name' => array(
263 'name' => 'new_name',
264 'type' => 'TEXT',
265 ),
266 );
267 $this->dbforge->modify_column('table_name', $fields);
268 // gives ALTER TABLE table_name CHANGE old_name new_name TEXT