blob: ee033248cd8b7e3bb1d69f19269ffd18e7692ccb [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
32 if ($this->dbforge->create_database('my_db')) {     echo 'Database created!'; }
33
34$this->dbforge->drop_database('db_name')
35==========================================
36
37Permits you to drop the database specified in the first parameter.
38Returns TRUE/FALSE based on success or failure::
39
40 if ($this->dbforge->drop_database('my_db')) {     echo 'Database deleted!'; }
41
42****************************
43Creating and Dropping Tables
44****************************
45
46There are several things you may wish to do when creating tables. Add
47fields, add keys to the table, alter columns. CodeIgniter provides a
48mechanism for this.
49
50Adding fields
51=============
52
53Fields are created via an associative array. Within the array you must
54include a 'type' key that relates to the datatype of the field. For
55example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR)
56also require a 'constraint' key.
57
58::
59
60 $fields = array(                         'users' => array(                                                  'type' => 'VARCHAR',                                                  'constraint' => '100',                                           ),                 ); // will translate to "users VARCHAR(100)" when the field is added.
61
62
63Additionally, the following key/values can be used:
64
65- unsigned/true : to generate "UNSIGNED" in the field definition.
66- default/value : to generate a default value in the field definition.
67- null/true : to generate "NULL" in the field definition. Without this,
68 the field will default to "NOT NULL".
69- auto_increment/true : generates an auto_increment flag on the
70 field. Note that the field type must be a type that supports this,
71 such as integer.
72
73::
74
75 $fields = array(                         'blog_id' => array(                                                  'type' => 'INT',                                                  'constraint' => 5,                                                  'unsigned' => TRUE,                                                  'auto_increment' => TRUE                                           ),                         'blog_title' => array(                                                  'type' => 'VARCHAR',                                                  'constraint' => '100',                                           ),                         'blog_author' => array(                                                  'type' =>'VARCHAR',                                                  'constraint' => '100',                                                  'default' => 'King of Town',                                           ),                         'blog_description' => array(                                                  'type' => 'TEXT',                                                  'null' => TRUE,                                           ),                 );
76
77
78After the fields have been defined, they can be added using
79$this->dbforge->add_field($fields); followed by a call to the
80create_table() function.
81
82$this->dbforge->add_field()
83----------------------------
84
85The add fields function will accept the above array.
86
87Passing strings as fields
88-------------------------
89
90If you know exactly how you want a field to be created, you can pass the
91string into the field definitions with add_field()
92
93::
94
95 $this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'");
96
97
98Note: Multiple calls to add_field() are cumulative.
99
100Creating an id field
101--------------------
102
103There is a special exception for creating id fields. A field with type
104id will automatically be assinged as an INT(9) auto_incrementing
105Primary Key.
106
107::
108
109 $this->dbforge->add_field('id'); // gives id INT(9) NOT NULL AUTO_INCREMENT
110
111
112Adding Keys
113===========
114
115Generally speaking, you'll want your table to have Keys. This is
116accomplished with $this->dbforge->add_key('field'). An optional second
117parameter set to TRUE will make it a primary key. Note that add_key()
118must be followed by a call to create_table().
119
120Multiple column non-primary keys must be sent as an array. Sample output
121below is for MySQL.
122
123::
124
125 $this->dbforge->add_key('blog_id', TRUE); // gives PRIMARY KEY `blog_id` (`blog_id`) $this->dbforge->add_key('blog_id', TRUE); $this->dbforge->add_key('site_id', TRUE); // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`) $this->dbforge->add_key('blog_name'); // gives KEY `blog_name` (`blog_name`) $this->dbforge->add_key(array('blog_name', 'blog_label')); // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`)
126
127
128Creating a table
129================
130
131After fields and keys have been declared, you can create a new table
132with
133
134::
135
136 $this->dbforge->create_table('table_name'); // gives CREATE TABLE table_name
137
138
139An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause
140into the definition
141
142::
143
144 $this->dbforge->create_table('table_name', TRUE); // gives CREATE TABLE IF NOT EXISTS table_name
145
146
147Dropping a table
148================
149
150Executes a DROP TABLE sql
151
152::
153
154 $this->dbforge->drop_table('table_name'); // gives DROP TABLE IF EXISTS table_name
155
156
157Renaming a table
158================
159
160Executes a TABLE rename
161
162::
163
164 $this->dbforge->rename_table('old_table_name', 'new_table_name'); // gives ALTER TABLE old_table_name RENAME TO new_table_name
165
166
167****************
168Modifying Tables
169****************
170
171$this->dbforge->add_column()
172=============================
173
174The add_column() function is used to modify an existing table. It
175accepts the same field array as above, and can be used for an unlimited
176number of additional fields.
177
178::
179
180 $fields = array(                         'preferences' => array('type' => 'TEXT') ); $this->dbforge->add_column('table_name', $fields); // gives ALTER TABLE table_name ADD preferences TEXT
181
182An optional third parameter can be used to specify which existing column
183to add the new column after.
184
185::
186
187 $this->dbforge->add_column('table_name', $fields, 'after_field');
188
189
190$this->dbforge->drop_column()
191==============================
192
193Used to remove a column from a table.
194
195::
196
197 $this->dbforge->drop_column('table_name', 'column_to_drop');
198
199
200$this->dbforge->modify_column()
201================================
202
203The usage of this function is identical to add_column(), except it
204alters an existing column rather than adding a new one. In order to
205change the name you can add a "name" key into the field defining array.
206
207::
208
209 $fields = array(                         'old_name' => array(                                                          'name' => 'new_name',                                                          'type' => 'TEXT',                                                 ), ); $this->dbforge->modify_column('table_name', $fields); // gives ALTER TABLE table_name CHANGE old_name new_name TEXT
210
211
212