Home > Database > Mysql Tutorial > body text

Update column size in MySQL and increase its size?

王林
Release: 2023-08-31 14:21:02
forward
926 people have browsed it

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

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

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

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

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

Display records -

mysql> select *from DataTruncated;
Copy after login

Below is the output -

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

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!

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