35.5. Adding and Editing Fields

New fields can be added to a table and existing ones changed or deleted. Adding a field poses no risk to existing data, but changing the type or size of one may, and deleting a field will cause the data that it contains to be lost.

To add a new field, follow these steps:

1.
On the module's main page, click on the icon for the database that contains the table, and then on the table icon. This will bring up the page shown in Figure 35.3, which lists the names, types, and other details of all existing fields.

Figure 35.3. The table editing page.


2.
Select the type for the new field from the menu next to the Add field of type button before clicking it. See Section 35.6 “Field Types” for a list of types and their purposes.

3.
On the field addition form that appears, enter a unique name for this field into the Field name text box. No two fields in the same table can have the same name, and only letters, numbers and _ can be used.

4.
If you are adding a char or varchar field, you must enter a maximum number of characters into the Type width text box.

If adding a float, double, or decimal field, you must enter two numbers into the Width and decimals text boxes. The first is the total number of digits that a value can contain and the second is the number of digits to the right of the decimal point. For negative numbers, the minus sign counts as a digit, so a field with Width and decimals set to 5 and 2 could store numbers from –99.99 to 999.99.

For date, datetime, time, blob, and text fields, there is no width input at all, as these types have fixed or unlimited sizes.

For enum and set fields, you must enter a list of possible values into the Enumerated values text box.

For all other field types (such as int) the Type width can be either set to Default to have the field use the default size for the chosen type, or a width can be entered. For int fields, this is the maximum number of digits that a value in this field can contain.

5.
For integer field types (such as int and smallint), the Type options radio buttons allow you to choose whether values in this field should be left-filled with zeros (the Fill with zeros option) or if they should be unsigned (the Unsigned option). If None is selected, values will be signed and no additional zeros will be added.

For float, double, and decimal fields, the same Type options are also displayed but without the Unsigned option. Fields of these types are always signed.

For char and varchar fields, Type options has two different choices—Case sensitive and Case insensitive. If insensitive is selected, SQL queries that match values in this field will ignore case differences.

6.
To prevent SQL NULL values being inserted into this field, change the Allow null? input to No. This can be useful if every record has a value for this field and must be selected if this field is going to be part of the primary key for the table.

7.
To have a default value inserted when a record is added to the table and no value is specified for this field, fill in the Default value text box. Naturally, the value must be of the correct type for the field.

If your table already contains some rows, their values for this field will be set to whatever you enter here when the new field is added.

8.
Change the Part of primary key? selection to Yes if this field is going to be the key for the table. More than one field can be part of the key, in which case the key is a combination of all of them.

9.
Finally, click Create. If there are no errors in your inputs, the field will be added to the table and you will be returned to the table editing page shown in Figure 35.4.

Figure 35.4. The MySQL user list.


Newly created or existing fields can be edited as well, by following the next set of steps. Making changes to the type of field or reducing its size, however, may result in data loss if the old values are not compatible with the new type. For example, converting a varchar to an int will cause all nonnumeric values to be lost—however, converting an int to a varchar is generally safe as long as the new size is large enough.

1.
On the module's main page, click on the icon for the database that contains the table, and then on the table icon. This will bring up the page shown in Figure 35.3, which lists the names, types, and other details of all existing fields.

2.
Click on the name of the field that you want to modify to go to the field editing form.

3.
To rename the field, edit the Field name text box.

4.
To change the field's type, select a new one from the Data type menu. As explained above, this should be done with care.

5.
Depending on the current type, different inputs will be displayed for editing its size. These are the same ones as explained in Step 4 of the preceding field creation instructions.

Increasing the size of a field will not harm any data that it contains, but decreasing it will cause values to be truncated if they are longer than the new size.

6.
The Type options, Allow nulls?, Default value and Part of primary key? inputs have the same meanings here as in the field creation steps. Change them if you want to adjust these options for the existing field.

7.
When you are done, hit the Save button at the bottom of the form. The field will be immediately updated, and any data that it contains will be modified or truncated as appropriate.

An existing field can be removed by clicking the Delete button on the field editing form instead of Save. Any data that it contains will be immediately deleted forever. Naturally, you cannot delete the last field in a table.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.181.145