0 votes
1 view
in SQL by (22.4k points)

I'm adding a new, "NOT NULL" column to my Postgresql database using the following query (sanitized for the Internet):

ALTER TABLE mytable ADD COLUMN mycolumn character varying(50) NOT NULL;

Each time I run this query, I receive the following error message:

ERROR:  column "mycolumn" contains null values

I'm stumped. Where am I going wrong?

NOTE: I'm using pgAdmin III (1.8.4) primarily, but I received the same error when I ran the SQL from within Terminal.

1 Answer

0 votes
by (40.3k points)

Set a default value and use this code:

ALTER TABLE mytable ADD COLUMN mycolumn character varying(50) NOT NULL DEFAULT 'foo';

/* you can set real values as well */

ALTER TABLE mytable ALTER COLUMN mycolumn DROP DEFAULT;

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...