Skip to content

Commit 47f71fa

Browse files
authored
Merge pull request #8448 from kenjis/docs-forge-data-types
docs: add about field data types on Forge
2 parents ca7d92c + 9bda43a commit 47f71fa

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

user_guide_src/source/dbmgmt/forge.rst

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,34 +96,67 @@ mechanism for this.
9696
Adding Fields
9797
=============
9898

99+
$forge->addField()
100+
------------------
101+
99102
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.
103107

104108
.. literalinclude:: forge/006.php
105109

106110
Additionally, the following key/values can be used:
107111

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``.
112116
- ``auto_increment``/true : generates an auto_increment flag on the
113117
field. Note that the field type must be a type that supports this,
114-
such as integer.
118+
such as ``INTEGER``.
115119
- ``unique``/true : to generate a unique key for the field definition.
116120

117121
.. literalinclude:: forge/007.php
118122

119123
After the fields have been defined, they can be added using
120124
``$forge->addField($fields)`` followed by a call to the
121-
``createTable()`` method.
125+
:ref:`createTable() <creating-a-table>` method.
122126

123-
$forge->addField()
124-
------------------
127+
Notes on Data Types
128+
-------------------
129+
130+
Floating-Point Types
131+
^^^^^^^^^^^^^^^^^^^^
125132

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>`_.
127160

128161
.. _forge-addfield-default-value-rawsql:
129162

@@ -209,6 +242,8 @@ You can specify the desired action for the "on update" and "on delete" propertie
209242

210243
.. note:: SQLite3 does not support the naming of foreign keys. CodeIgniter will refer to them by ``prefix_table_column_foreign``.
211244

245+
.. _creating-a-table:
246+
212247
Creating a Table
213248
================
214249

user_guide_src/source/dbmgmt/forge/007.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
'default' => 'pending',
2828
],
2929
];
30+
$forge->addField($fields);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

3-
$forge->dropColumn('table_name', 'column_1,column_2'); // by proving comma separated column names
3+
$forge->dropColumn('table_name', 'column_1,column_2'); // by proving comma separated column names
44
$forge->dropColumn('table_name', ['column_1', 'column_2']); // by proving array of column names
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

3-
// MySqli Produces: ALTER TABLE `tablename` DROP PRIMARY KEY
3+
// MySQLi Produces: ALTER TABLE `tablename` DROP PRIMARY KEY
44
// Others Produces: ALTER TABLE `tablename` DROP CONSTRAINT `pk_tablename`
55
$forge->dropPrimaryKey('tablename');

0 commit comments

Comments
 (0)