简体   繁体   English

将数据库中的所有TinyInt(1)列更改为非空默认值0

[英]Alter All TinyInt(1) Columns In Database to Not Null Default 0

I have a database, and I need to alter every single 我有一个数据库,我需要更改每个数据库

tinyint(1) default null

column (and incidentally, every value in those columns set to null) in every single table in that database to: 该数据库中每个表中的列(顺便说一下,这些列中的每个值都设置为null)为:

tinyint(1) not null default 0

as soon as possible. 尽快地。

What is the fastest / most efficient way to achieve this, (programmatically or otherwise)? (以编程方式或其他方式)最快/最有效的方法是什么?

Query information_schema.COLUMNS view for a list of tables/columns, then use your programming language of choice to loop through the results and perform ALTER TABLE queries. information_schema.COLUMNS视图中查询表/列的列表,然后使用您选择的编程语言遍历结果并执行ALTER TABLE查询。

SELECT TABLE_NAME, COLUMN_NAME 
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'yourDatabase'
AND COLUMN_TYPE = 'tinyint(1)'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM