批量设置主键(默认表格的第一个字段)
SELECT CONCAT(
'ALTER TABLE ', TABLE_NAME,
' ADD PRIMARY KEY (', COLUMN_NAME, ');'
) AS sql_statement
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '数据库名字'
AND ORDINAL_POSITION = 1
AND COLUMN_KEY <> 'PRI';
批量添加自增(默认表格的第一个字段)
SELECT CONCAT(
'ALTER TABLE ', TABLE_NAME,
' MODIFY COLUMN ', COLUMN_NAME, ' ', COLUMN_TYPE,
' AUTO_INCREMENT;'
) AS sql_statement
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = '数据库名字'
AND ORDINAL_POSITION = 1
AND EXTRA NOT LIKE '%auto_increment%'
AND COLUMN_TYPE IN ('int', 'bigint', 'mediumint', 'smallint', 'tinyint');
本站文章除注明转载/出处外,均为博主 spooking 原创或翻译,转载前请务必署名。