Обсуждение: Creating a column interger with default to not null
I am confused with what this is telling me.
I have a table and I am trying to add a new column with constraint not null.
What am I missing?
Chris
pgdb001=> alter table schema.table add COLUMN column_name integer not null;
ERROR: column "column_name" contains null values
On 24 September 2010 16:05, Chris Barnes <compuguruchrisbarnes@hotmail.com> wrote: > > I am confused with what this is telling me. > I have a table and I am trying to add a new column with constraint not null. > > > What am I missing? > Chris > > > pgdb001=> alter table schema.table add COLUMN column_name integer not null; > ERROR: column "column_name" contains null values What it's telling you is that the column can't contain null values, but you're adding a new column with no default, so it will naturally use null. Try adding a default like: ALTER TABLE schema.table ADD COLUMN column_name integer NOT NULL DEFAULT 0; -- Thom Brown Twitter: @darkixion IRC (freenode): dark_ixion Registered Linux user: #516935
On Friday 24 September 2010 8:05:43 am Chris Barnes wrote: > I am confused with what this is telling me. > I have a table and I am trying to add a new column with constraint not > null. > > > What am I missing? > Chris > > > pgdb001=> alter table schema.table add COLUMN column_name integer not null; > ERROR: column "column_name" contains null values The error message :) The column has NULLs in it so you can not make it NOT NULL until you get rid of them. -- Adrian Klaver adrian.klaver@gmail.com