Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: TiDB支持在一个alter table add column增加多个字段么
[Test Environment for TiDB] Testing
[TiDB Version] v7.5.1
[Reproduction Path] What operations were performed to encounter the issue
mysql> alter table test322 add column d varchar(10),e varchar(10);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 46 near “e varchar(10)”
mysql> alter table test322 add column d varchar(10) e varchar(10);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 46 near “e varchar(10)”
[Encountered Issue: Issue Phenomenon and Impact]
[Resource Configuration] Enter TiDB Dashboard - Cluster Info - Hosts and take a screenshot of this page
[Attachments: Screenshots/Logs/Monitoring]
Try running: ALTER TABLE test322 ADD COLUMN (d VARCHAR(10), e VARCHAR(10));
I referred to MySQL, and the following syntax can be executed:
alter table test322 add column d varchar(10), add column e varchar(10);
Well, I tried it, and your syntax works too.
alter table test322 add column (f varchar(10), g varchar(10));
Another way is:
ALTER TABLE test322 ADD COLUMN d VARCHAR(10), ADD COLUMN e VARCHAR(10);
Yes, thank you very much.
Are there any libraries that are not supported…?
Version 7.5.1 supports adding multiple columns in a single statement, but version 5 does not.
Usually, to save time, I use Navicat to add and control the position of new fields directly in the interface.
Your syntax is incorrect. MySQL doesn’t support this usage either. The new version of TiDB supports adding multiple columns in a single statement. Using the general syntax alter table ... add xxx, add xxx
will definitely work.
There will be an error when adding 2 fields simultaneously in Navicat. It seems that there is a compatibility issue between the tool and the database.
The official TiDB documentation should also include the syntax for adding multiple columns.
The syntax diagram in the documentation is very clear. AddColumnSpec can be repeated, separated by commas.
In Navicat, adding two fields at the same time will result in an error, mainly because of the use of “after”. Multiple “after” statements depend on columns that have not yet been added. Modifying the automatically generated SQL will allow it to execute.
The syntax of MySQL is almost supported.