MySql 创建一个包含 600 列的表

问题描述 投票:0回答:1

试图找到在 mysql 中的表中创建 600 列的最简单方法,而无需输入每一列。

最终,我还必须找到一种方法来更新记录,而且无需输入每个列名称来插入数据。任何提示将不胜感激!

各列名称如下:

check_1
check_1_status
check_1_comment
check_2
check_2_status
check_2_comment
...
etc.
...
check_200
check_200_status
check_200_comment
mysql mysqli
1个回答
0
投票

对于单个表拥有 600 列来说这绝对不是一个好主意,更不用说行大小有限制。以下是我们如何在存储过程中执行此操作的演示。

delimiter //

create procedure create_table(in num int)
begin
declare id int default 2;

drop table if exists test;
create table test(check_1 char(1), check_1_status char(1), check_1_comment char(1));

while id<=num do
set @stmt=concat('alter table test add check_',id,' char(1), add check_',
id,'_status char(1), add check_',id,'_comment char(1);');

prepare stmt from @stmt;
execute stmt;


set id=id+1;

end while;
deallocate prepare stmt;

end//

delimiter ;

desc test;
-- result with the majority omitted:
| check_195         | char(1) | YES  |     | NULL    |       |
| check_195_status  | char(1) | YES  |     | NULL    |       |
| check_195_comment | char(1) | YES  |     | NULL    |       |
| check_196         | char(1) | YES  |     | NULL    |       |
| check_196_status  | char(1) | YES  |     | NULL    |       |
| check_196_comment | char(1) | YES  |     | NULL    |       |
| check_197         | char(1) | YES  |     | NULL    |       |
| check_197_status  | char(1) | YES  |     | NULL    |       |
| check_197_comment | char(1) | YES  |     | NULL    |       |
| check_198         | char(1) | YES  |     | NULL    |       |
| check_198_status  | char(1) | YES  |     | NULL    |       |
| check_198_comment | char(1) | YES  |     | NULL    |       |
| check_199         | char(1) | YES  |     | NULL    |       |
| check_199_status  | char(1) | YES  |     | NULL    |       |
| check_199_comment | char(1) | YES  |     | NULL    |       |
| check_200         | char(1) | YES  |     | NULL    |       |
| check_200_status  | char(1) | YES  |     | NULL    |       |
| check_200_comment | char(1) | YES  |     | NULL    |       |
+-------------------+---------+------+-----+---------+-------+
600 rows in set (0.07 sec)
© www.soinside.com 2019 - 2024. All rights reserved.