Home > Database > Mysql Tutorial > body text

How to modify the size of columns in a MySQL table?

WBOY
Release: 2023-08-28 12:25:06
forward
1580 people have browsed it

How to modify the size of columns in a MySQL table?

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)
Copy after login

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
Copy after login

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;
Copy after login

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
Copy after login

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;
Copy after login

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)
Copy after login

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)
Copy after login

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;
Copy after login

The following is the output -

| id | StudentName |
+------+-------------+
| 1 | CarolTaylor|
+------+-------------+
1 row in set (0.00 sec)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template