简体   繁体   English

我如何通过PostgreSQL 9.1的一个查询将TABLE的所有列从非null更改为null

[英]How can i Alter TABLE all column from not null to null by one query of PostgreSQL 9.1

I migrate my data from MySQL Database to PostgreSQL Database and by mistaken i have all my column set not-null in PostgreSQL database. 我将数据从MySQL数据库迁移到PostgreSQL数据库,并且错误地将所有列设置为PostgreSQL数据库中的非空值。

After this i am facing issue for inserting data and have to uncheck not null manually, so is there any way to do for all column in table ( except id(PRIMARY KEY) ). 在此之后,我面临插入数据的问题,必须手动取消选中不为null的问题,因此表中的所有列都可以进行任何处理(id(PRIMARY KEY)除外)。

i have this query also for single column but its also time consuming, 我对单列也有此查询,但也很耗时,

ALTER TABLE <table name> ALTER COLUMN <column name> DROP NOT NULL;

I don't think there is built in functionality for this. 我认为没有内置功能。 But it's easy to write a function to do this. 但是编写一个函数来做到这一点很容易。 For example: 例如:

CREATE OR REPLACE FUNCTION set_nullable(relation TEXT)
RETURNS VOID AS
$$
DECLARE
    rec RECORD;
BEGIN
    FOR rec IN (SELECT * FROM pg_attribute
                WHERE attnotnull = TRUE AND attrelid=relation::regclass::oid AND attnum > 0 AND attname != 'id')
    LOOP
        EXECUTE format('ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL', relation, rec.attname);
        RAISE NOTICE 'Set %.% nullable', relation, rec.attname;
    END LOOP;
END
$$
LANGUAGE plpgsql;

Use it like this: 像这样使用它:

SELECT set_nullable('my_table');

Just perform a new CREATE TABLE noNULL using the same structure as your current table and modify the NULLable field you need. 只需使用与当前表相同的结构执行新的CREATE TABLE noNULL ,然后修改所需的NULLable字段即可。

Then insert from old table 然后从旧表插入

INSERT INTO noNULL 
      SELECT *
      FROM oldTable

then delete oldTable and rename noNull -> oldTable 然后删除oldTable并重命名noNull -> oldTable

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

相关问题 ALTER TABLE,在非空列中设置null,PostgreSQL 9.1 - ALTER TABLE, set null in not null column, PostgreSQL 9.1 Postgresql 将表列类型更改为唯一非空 - Postgresql alter table column type to unique not null 如何一次性从PostgreSQL表中删除所有NOT NULL约束 - How to drop all NOT NULL constraints from a PostgreSQL table in one go 如何在Postgresql中更改新的非null列? - How to alter new not null column in Postgresql? PostgreSQL-如果所有列值均为&#39;A&#39;或NULL,如何返回ID - PostgreSQL - How can I return ids if all column values are 'A' or NULL 如何确保 postgresql 表中只有一列不是 null - How to make sure only one column is not null in postgresql table 我们如何在 postgresql 中构建查询以显示时间数据类型为 null 的列值,当从其他列中提取它具有 0 或 null - How can we build a query in postgresql to display column value with time datatype as null when extracting it from other column has either 0 or null 只有在Postgresql中列不为null时,如何强制执行约束? - How can I enforce a constraint only if a column is not null in Postgresql? 如何将诸如“ ALTER table […] ADD column”之类的更改记录到PostgreSQL表中? - How can I log changes like “ALTER table […] ADD column” to my PostgreSQL table? 将表列更改为int(9)UNSIGNED ZEROFILL NOT NULL - Alter table column to int(9) UNSIGNED ZEROFILL NOT NULL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM