Home  >  Article  >  Database  >  Update column size in MySQL and increase its size?

Update column size in MySQL and increase its size?

王林
王林forward
2023-08-31 14:21:02925browse

更新 MySQL 中的列大小并增加其大小?

To update the column size, you can use the alter command. The syntax is as follows -

alter table yourTableName change yourColumnName yourColumnName data type;

To understand the above syntax, let us create a table. Query to create table -

mysql> create table DataTruncated
   −> (
   −> id int,   
   −> Name varchar(5)
   −> );
Query OK, 0 rows affected (0.64 sec)

Look at the "Name" column above, the column size is 5. Whenever we give a size greater than 5, MySQL gives the following error -

mysql> insert into DataTruncated values(101,'JohnSmith');
ERROR 1406 (22001): Data too long for column 'Name' at row 1

Now update the column size of the "name" column. The query is as follows -

mysql> alter table DataTruncated change Name Name varchar(200);
Query OK, 0 rows affected (2.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

Insert the same records into the table. Now, since we updated the column size from 5 to 25, there are no visible errors -

mysql> insert into DataTruncated values(101,'JohnSmith');
Query OK, 1 row affected (0.11 sec)

Display records -

mysql> select *from DataTruncated;

Below is the output -

+------+-----------+
| id   | Name      |
+------+-----------+
| 101  | JohnSmith |
+------+-----------+
1 row in set (0.00 sec)

The above is the detailed content of Update column size in MySQL and increase its size?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete