MySQL批量删除表字段注释

616 阅读1分钟

1.运行以下命令以生成删除注释的脚本,把your_database_name替换为你的数据库:

SELECT
	CONCAT(
		'alter table ',
		table_schema,
		'.',
		table_name,
		' modify column ',
		column_name,
		' ',
		column_type,
	IF
		( is_nullable = 'YES', '', ' not null' ),
	IF
		(
			column_default IS NULL,
			'',
		IF
			(
				data_type IN ( 'char', 'varchar' ) 
				OR data_type IN ( 'date', 'datetime', 'timestamp' ) 
				AND column_default != 'CURRENT_TIMESTAMP',
				CONCAT( ' default ', '\'',column_default, '\'' ),
				CONCAT( ' default ', column_default ) 
			) 
		),
	IF
		(
			extra IS NULL 
			OR extra = '',
			'',
		concat( ' ', extra )),
		' comment '''';' 
) s 
FROM
	information_schema.COLUMNS 
WHERE
	table_schema = 'your_database_name';

这个查询将为数据库中的每个表和每个列生成一条 ALTER TABLE 语句。这些语句将删除表和列的注释。请注意,此查询不会立即删除注释,而是生成用于删除注释的脚本

2.执行生成的删除脚本:

alter table your_database_name.admin_log modify column id int(11) not null auto_increment comment '';
alter table your_database_name.admin_log modify column admin_user_id int(10) not null comment '';
...