blob: f3b6c2dd037fc6c8300be22755045940c32aeb5d [file] [log] [blame]
Derek Allard39b622d2008-01-16 21:10:09 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
Derek Allard39b622d2008-01-16 21:10:09 +00003<head>
4
Derek Jonesfd93d222008-05-06 15:18:50 +00005<title>Database Forge Class : CodeIgniter User Guide</title>
Derek Allard39b622d2008-01-16 21:10:09 +00006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
10<script type="text/javascript" src="../nav/nav.js"></script>
11<script type="text/javascript" src="../nav/prototype.lite.js"></script>
12<script type="text/javascript" src="../nav/moo.fx.js"></script>
13<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
14
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
Derek Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allard39b622d2008-01-16 21:10:09 +000020<meta name='description' content='CodeIgniter User Guide' />
21
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Jones6b7b11f2008-05-06 16:07:27 +000031<td><h1>CodeIgniter User Guide Version 1.6.2</h1></td>
Derek Allard39b622d2008-01-16 21:10:09 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
Derek Jones7a9193a2008-01-21 18:39:20 +000043<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
Derek Allard39b622d2008-01-16 21:10:09 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45<a href="index.html">Database Library</a> &nbsp;&#8250;&nbsp;
Derek Allarddfd027d2008-02-01 18:29:42 +000046Database Forge Class
Derek Allard39b622d2008-01-16 21:10:09 +000047</td>
48<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
49</tr>
50</table>
51<!-- END BREADCRUMB -->
52
53
54<br clear="all" />
55
56
57<!-- START CONTENT -->
58<div id="content">
59
60<h1>Database Forge Class</h1>
61
62<p>The Database Forge Class contains functions that help you manage your database.</p>
63
64<h3>Table of Contents</h3>
65
66<ul>
67<li><a href="#init">Initializing the Forge Class</a></li>
68<li><a href="#create">Creating a Database</a></li>
69<li><a href="#drop">Dropping a Database</a></li>
70<li><a href="#add_field">Adding Fields</a></li>
71<li><a href="#add_key">Adding Keys</a></li>
72<li><a href="#create_table">Creating a Table</a></li>
73<li><a href="#drop_table">Dropping a Table</a></li>
Derek Allard2385d302008-04-07 14:01:01 +000074<li><a href="#rename_table">Renaming a Table</a></li>
Derek Allard39b622d2008-01-16 21:10:09 +000075<li><a href="#modifying_tables">Modifying a Table</a></li>
76</ul>
77
78
79<h2><a name="init"></a>Initializing the Forge Class</h2>
80
81<p class="important"><strong>Important:</strong>&nbsp; In order to initialize the Forge class, your database driver must
82already be running, since the forge class relies on it.</p>
83
84<p>Load the Forge Class as follows:</p>
85
86<code>$this->load->dbforge()</code>
87
88<p>Once initialized you will access the functions using the <dfn>$this->dbforge</dfn> object:</p>
89
90<code>$this->dbforge->some_function()</code>
91<h2><a name="create"></a>$this->dbforge->create_database('db_name')</h2>
92
93<p>Permits you to create the database specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p>
94
Derek Allardb6cd79c2008-02-01 00:39:51 +000095<code>if ($this->dbforge->create_database('my_db'))<br />
Derek Allard39b622d2008-01-16 21:10:09 +000096{<br />
97&nbsp;&nbsp;&nbsp; echo 'Database created!';<br />
98}</code>
99
100
101
102
103<h2><a name="drop"></a>$this->dbforge->drop_database('db_name')</h2>
104
105<p>Permits you to drop the database specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p>
106
Derek Allardb6cd79c2008-02-01 00:39:51 +0000107<code>if ($this->dbforge->drop_database('my_db'))<br />
Derek Allard39b622d2008-01-16 21:10:09 +0000108{<br />
109&nbsp;&nbsp;&nbsp; echo 'Database deleted!';<br />
110}</code>
111
112
113<h1>Creating and Dropping Tables</h1>
114<p>There are several things you may wish to do when creating tables. Add fields, add keys to the table, alter columns. CodeIgniter provides a mechanism for this.</p>
115<h2><a name="add_field" id="add_field"></a>Adding fields</h2>
116<p>Fields are created via an associative array. Within the array you must include a 'type' key that relates to the datatype of the field. For example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR) also require a 'constraint' key.</p>
117<p><code>$fields = array(<br />
118&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'users' =&gt; array(<br />
119 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'type' =&gt; 'varchar',<br />
120 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'constraint' =&gt; '100',<br />
121 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />
122 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
123 <br />
124// will translate to &quot;users VARCHAR(100)&quot; when the field is added.</code></p>
125<p>Additionally, the following key/values can be used:</p>
126<ul>
Derek Allard75eedf52008-04-13 12:49:54 +0000127 <li>unsigned/true : to generate &quot;UNSIGNED&quot; in the field definition.</li>
128 <li>default/value : to generate a default value in the field definition.</li>
129 <li>null/true : to generate &quot;NULL&quot; in the field definition. Without this, the field will default to &quot;NOT NULL&quot;.</li>
130 <li>auto_increment/true : generates an auto_increment flag on the field. Note that the field type must be a type that supports this, such as integer.</li>
Derek Allard39b622d2008-01-16 21:10:09 +0000131 </ul>
132<p><code>$fields = array(<br />
133 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'blog_id' =&gt; array(<br />
134 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'type' =&gt; 'INT',<br />
135 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'constraint' =&gt; 5, <br />
136 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />
Derek Allard0bb0e502008-03-27 12:08:34 +0000152&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
153</code></p>
Derek Allard39b622d2008-01-16 21:10:09 +0000154<p>After the fields have been defined, they can be added using <dfn>$this-&gt;dbforge-&gt;add_field($fields);</dfn> followed by a call to the <dfn>create_table()</dfn> function.</p>
155<h3>$this-&gt;dbforge-&gt;add_field()</h3>
156<p>The add fields function will accept the above array.</p>
157<h3>Passing strings as fields</h3>
158<p>If you know exactly how you want a field to be created, you can pass the string into the field definitions with add_field()</p>
159<p><code>$this-&gt;dbforge-&gt;add_field(&quot;label varchar(100) NOT NULL DEFAULT 'default label'&quot;);</code></p>
160<p class="important">Note: Multiple calls to <dfn>add_field()</dfn> are cumulative.</p>
161<h3>Creating an id field</h3>
162<p>There is a special exception for creating id fields. A field with type id will automatically be assinged as an INT(9) auto_incrementing Primary Key.</p>
163<p><code>$this-&gt;dbforge-&gt;add_field('id');<br />
164 // gives id INT(9) NOT NULL AUTO_INCREMENT</code></p>
165<h2><a name="add_key" id="add_key"></a>Adding Keys</h2>
166<p>Generally speaking, you'll want your table to have Keys. This is accomplished with <dfn>$this-&gt;dbforge-&gt;add_key('field')</dfn>. An optional second parameter set to TRUE will make it a primary key. Note that <dfn>add_key()</dfn> must be followed by a call to <dfn>create_table()</dfn>.</p>
167<p><code>$this-&gt;dbforge-&gt;add_key('blog_id', TRUE);<br />
168 // gives PRIMARY KEY (blog_id)<br />
169 <br />
170 $this-&gt;dbforge-&gt;add_key('blog_name');<br />
171 // gives KEY (blog_name)</code></p>
Derek Allard2385d302008-04-07 14:01:01 +0000172<h2><a name="create_table" id="create_table"></a>Creating a table</h2>
Derek Allard39b622d2008-01-16 21:10:09 +0000173<p>After fields and keys have been declared, you can create a new table with</p>
Derek Allard3faee4a2008-01-29 01:09:48 +0000174<p><code>$this-&gt;dbforge-&gt;create_table('table_name');<br />
Derek Allard39b622d2008-01-16 21:10:09 +0000175// gives CREATE TABLE table_name</code></p>
176<p>An optional second parameter set to TRUE adds an &quot;IF NOT EXISTS&quot; clause into the definition</p>
Derek Allard3faee4a2008-01-29 01:09:48 +0000177<p><code>$this-&gt;dbforge-&gt;create_table('table_name', TRUE);<br />
Derek Allard39b622d2008-01-16 21:10:09 +0000178// gives CREATE TABLE IF NOT EXISTS table_name</code></p>
179<h2><a name="drop_table" id="drop_table"></a>Dropping a table</h2>
180<p>Executes a DROP TABLE sql</p>
Derek Allard3faee4a2008-01-29 01:09:48 +0000181<p><code>$this-&gt;dbforge-&gt;drop_table('table_name');<br />
Derek Allard2385d302008-04-07 14:01:01 +0000182 // gives DROP TABLE IF EXISTS table_name</code></p>
183<h2><a name="rename_table" id="rename_table"></a>Renaming a table</h2>
184<p>Executes a TABLE rename</p>
185<p><code>$this-&gt;dbforge-&gt;rename_table('old_table_name', 'new_table_name');<br />
186 // gives ALTER TABLE old_table_name RENAME TO new_table_name</code></p>
187<h1><a name="modifying_tables" id="modifying_tables"></a>Modifying Tables</h1>
Derek Allard39b622d2008-01-16 21:10:09 +0000188<h2>$this-&gt;dbforge-&gt;add_column()</h2>
189<p>The add_column() function is used to modify an existing table. It accepts the same field array as above, and can be used for an unlimited number of additional fields.</p>
190<p><code>$fields = array(<br />
191 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'preferences' =&gt; array('type' =&gt; 'TEXT')<br />
192);<br />
193$this-&gt;dbforge-&gt;add_column('table_name', $fields);<br />
194<br />
Derek Allard44bd10f2008-03-25 22:27:37 +0000195// gives ALTER TABLE table_name ADD preferences TEXT</code></p>
Derek Allard39b622d2008-01-16 21:10:09 +0000196<h2>$this-&gt;dbforge-&gt;drop_column()</h2>
197<p>Used to remove a column from a table. </p>
198<p><code>$this-&gt;dbforge-&gt;drop_column('table_name', 'column_to_drop');</code></p>
199<h2>$this-&gt;dbforge-&gt;modify_column()</h2>
Derek Allard44bd10f2008-03-25 22:27:37 +0000200<p>The usage of this function is identical to add_column(), except it alters an existing column rather than adding a new one. In order to use it you must add a &quot;name&quot; key into the field defining array.</p>
Derek Allard39b622d2008-01-16 21:10:09 +0000201<p><code>$fields = array(<br />
202&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'old_name' =&gt; array(<br />
203&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name' =&gt; 'new_name',<br />
204&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'type' =&gt; 'TEXT',<br />
205&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />
206);<br />
Derek Allard75eedf52008-04-13 12:49:54 +0000207$this-&gt;dbforge-&gt;modify_column('table_name', $fields);<br />
Derek Allard39b622d2008-01-16 21:10:09 +0000208 <br />
Derek Allard75eedf52008-04-13 12:49:54 +0000209 // gives ALTER TABLE table_name CHANGE old_name new_name TEXT </code></p>
Derek Allard39b622d2008-01-16 21:10:09 +0000210<p>&nbsp;</p>
211</div>
212<!-- END CONTENT -->
213
214
215<div id="footer">
216<p>
217Previous Topic:&nbsp;&nbsp;<a href="caching.html">DB Caching Class</a>
218&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
219<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
220<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
221Next Topic:&nbsp;&nbsp;<a href="utilities.html">Database Utilities Class</a><a href="../libraries/email.html"></a></p>
Derek Jones07870432008-02-13 03:49:26 +0000222<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
Derek Allard39b622d2008-01-16 21:10:09 +0000223</div>
224
225</body>
226</html>