We can modify the column size with the help of the ALTER command. Let's see how to modify Column size. Suppose we define any column with a certain size. When inserting if we If the size given is larger than what we defined, an error will occur.
Modifying the size can reduce the above problems. For more understanding we can Create a table with the help of the CREATE command -
mysql> CREATE table ModifyColumnNameDemo -> ( -> id int, -> StudentName varchar(10) -> ); Query OK, 0 rows affected (0.45 sec)
After the table is successfully created, we can insert records into the table through INSERT Order.
mysql> INSERT into ModifyColumnNameDemo values(1,'CarolTaylor'); ERROR 1406 (22001): Data too long for column 'StudentName' at row 1
From the above query, we get error 1406. This error can be solved when modifying Pillar. For this we can use the ALTER command. Following is the syntax -
ALTER table yourTableName modify column_name;
Apply the above query to modify the size of the column to a certain size -
mysql> ALTER table ModifyColumnNameDemo modify StudentName varchar(200); Query OK, 0 rows affected (1.54 sec) Records: 0 Duplicates: 0 Warnings: 0
After that we can check that the size of the column name "StudentName" is 200. The query is as follows As follows -
mysql> DESC ModifyColumnNameDemo;
The following is the output -
+-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+ |id | int(11) | YES | | NULL| | | StudentName | varchar(200) | YES | | NULL | | +-------------+--------------+------+-----+---------+-------+ 2 rows in set (0.04 sec)
Look at the StudentName column above, the size has been changed to 200. Now we can insert Writing a record to the table we don't get any errors. Let us check -
mysql> INSERT into ModifyColumnNameDemo values(1,'CarolTaylor'); Query OK, 1 row affected (0.14 sec)
The above records have been successfully inserted into the table. We can show the record Insert the above with the help of SELECT command -
mysql> SELECT * from ModifyColumnNameDemo;
The following is the output -
| id | StudentName | +------+-------------+ | 1 | CarolTaylor| +------+-------------+ 1 row in set (0.00 sec)
Look at the above output, the record has been inserted successfully after modifying the size column.
The above is the detailed content of How to modify the size of columns in a MySQL table?. For more information, please follow other related articles on the PHP Chinese website!