You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user_guide_src/source/dbmgmt/forge.rst
+47-12Lines changed: 47 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -96,34 +96,67 @@ mechanism for this.
96
96
Adding Fields
97
97
=============
98
98
99
+
$forge->addField()
100
+
------------------
101
+
99
102
Fields are normally created via an associative array. Within the array, you must
100
-
include a ``type`` key that relates to the datatype of the field. For
101
-
example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR)
102
-
also require a ``constraint`` key.
103
+
include a ``type`` key that relates to the datatype of the field.
104
+
105
+
For example, ``INT``, ``VARCHAR``, ``TEXT``, etc.
106
+
Many datatypes (for example ``VARCHAR``) also require a ``constraint`` key.
103
107
104
108
.. literalinclude:: forge/006.php
105
109
106
110
Additionally, the following key/values can be used:
107
111
108
-
- ``unsigned``/true : to generate "UNSIGNED" in the field definition.
109
-
- ``default``/value : to generate a default value in the field definition.
110
-
- ``null``/true : to generate "null" in the field definition. Without this,
111
-
the field will default to "NOT null".
112
+
- ``unsigned``/true : to generate ``UNSIGNED`` in the field definition.
113
+
- ``default``/value : to generate ``DEFAULT`` constraint in the field definition.
114
+
- ``null``/true : to generate ``NULL`` in the field definition. Without this,
115
+
the field will default to ``NOT NULL``.
112
116
- ``auto_increment``/true : generates an auto_increment flag on the
113
117
field. Note that the field type must be a type that supports this,
114
-
such as integer.
118
+
such as ``INTEGER``.
115
119
- ``unique``/true : to generate a unique key for the field definition.
116
120
117
121
.. literalinclude:: forge/007.php
118
122
119
123
After the fields have been defined, they can be added using
120
124
``$forge->addField($fields)`` followed by a call to the
121
-
``createTable()`` method.
125
+
:ref:`createTable()<creating-a-table>` method.
122
126
123
-
$forge->addField()
124
-
------------------
127
+
Notes on Data Types
128
+
-------------------
129
+
130
+
Floating-Point Types
131
+
^^^^^^^^^^^^^^^^^^^^
125
132
126
-
The ``addField()`` method will accept the above array.
133
+
Floating-Point types such as ``FLOAT`` and ``DOUBLE`` represent approximate values.
134
+
Therefore, they should not be used when exact values are needed.
135
+
136
+
::
137
+
138
+
mysql> CREATE TABLE t (f FLOAT, d DOUBLE);
139
+
mysql> INSERT INTO t VALUES(99.9, 99.9);
140
+
141
+
mysql> SELECT * FROM t WHERE f=99.9;
142
+
Empty set (0.00 sec)
143
+
144
+
mysql> SELECT * FROM t WHERE f > 99.89 AND f < 99.91;
145
+
+------+------+
146
+
| f | d |
147
+
+------+------+
148
+
| 99.9 | 99.9 |
149
+
+------+------+
150
+
1 row in set (0.01 sec)
151
+
152
+
When it is important to preserve exact precision, for example with monetary data,
153
+
``DECIMAL`` or ``NUMERIC`` should be used.
154
+
155
+
TEXT
156
+
^^^^
157
+
158
+
``TEXT`` should not be used on SQLSRV. It is deprecated.
159
+
See `ntext, text, and image (Transact-SQL) - SQL Server | Microsoft Learn <https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql?view=sql-server-ver16>`_.
127
160
128
161
.. _forge-addfield-default-value-rawsql:
129
162
@@ -209,6 +242,8 @@ You can specify the desired action for the "on update" and "on delete" propertie
209
242
210
243
.. note:: SQLite3 does not support the naming of foreign keys. CodeIgniter will refer to them by ``prefix_table_column_foreign``.
0 commit comments