How to alter new not null column in Postgresql? - sql

I have a table that I want to add new not null varchar(255) column
My query is:
alter table poll_management.DASHLETS add column name varchar(255) not null;
update poll_management.DASHLETS as dashlet set name = report.name
from poll_management.REPORTS as report
WHERE dashlet.id = report.reportdashletid
But I have an error:
ERROR: column "name" contains null values
********** Error **********
ERROR: column "name" contains null values
SQL state: 23502

To avoid your error, two solutions come at once:
BEGIN;
alter table poll_management.DASHLETS add column name varchar(255);
update poll_management.DASHLETS as dashlet set name = report.name
from poll_management.REPORTS as report;
--mind I removed where, cos you need to update ALL rows to have some avlue
alter table poll_management.DASHLETS alter column "name" set not null;
END;
and the other:
alter table poll_management.DASHLETS add column name varchar(255) NOT NULL default 'not set';

i found that my query must be change to :
alter table poll_management.DASHLETS add column name varchar(255) not null DEFAULT 'value';

Related

Alter a table column into smallint column in postgres

I have a postgres table called update_profile with a column that is a table:
And I want to alter this column to a smallint containing the value of update_type_id.
Initially I tried:
ALTER TABLE update_profile ALTER COLUMN update_type TYPE SMALLINT USING update_type.update_type_id;
But I had the following error: missing FROM-clause entry for table "update_type"
Then I tried:
ALTER TABLE update_profile AS u ALTER COLUMN u.update_type TYPE SMALLINT USING u.update_type.update_type_id;
Which is not allowed.
Note: update_type_id is also a smallint
Is there a way to do this operation?
Don't repeat the table name when you reference the other column. You can't assign any alias for the table (or column) either.
ALTER TABLE update_profile
ALTER COLUMN update_type TYPE SMALLINT
USING update_type_id;
This is what I ended up doing:
ALTER TABLE update_profile ADD COLUMN update_type_id SMALLINT;
UPDATE update_profile up SET update_type_id =
(
SELECT ut.update_type_id
FROM n3rgy_update_type ut
WHERE ut = up.update_type
)
WHERE up.update_type IS NOT NULL;
ALTER TABLE update_profile DROP COLUMN update_type;
Because I didn't find a way to alter the column update_type, I created a new column called update_type_id, passed the values of update_profile.update_type.update_type_id, and then dropped update_type.
So now I have the values of update_profile.update_type.update_type_id in update_profile.update_type_id

How to add NOT NULL constraint along with default value for the column?

I have a column 'name' in student table. I need to add NOT NULL constraint on this column. But I get SQL error saying cannot add null constraint since the existing rows in the table has null values in the column. How would I add a null constraint along with default value in a single alter statement. Below is my query.
alter table Student alter column name nvarchar NOT NULL;
SQL Server does not make this easy. I think the only way is to set the existing values to the default that you want. Then change the column to have a default value and not null:
-- Get rid of the existing `NULL` values
update students set name = '' where name is null;
-- Add the NOT NULL constraint
alter table students
alter column name varchar(255) not null;
-- Add a default value
alter table students
add constraint df_t_name default '' for name ;
Here is what it looks like in practice.
But I get SQL error saying cannot add null constraint since the existing rows in the table has null values in the column.
Have you tried overwriting the existing NULL values?
You can't have a constraint on a column when existing values would violate that constraint. You need to make your table compliant first.

insert values in the last column for my table from another table

Hi I'm trying to insert values in the last column for my table from another table but am getting error ERROR:
null value in column "name" violates not-null constraint DETAIL:
Failing row contains (ddf1caf0-26c2-49e1-8a73-64227eae1f50, null,
null, null, null, null, 2532).
I suspect that you want to update the column subsystem of the table software_instances with values of the column sub of the table temp_subsystem:
update software_instances si
set subsystem = ts.sub
from temp_subsystem ts
where ts.module = si.module
there are two solutions to this issue
make sure the value you are selecting have the name value populated.
Add where name!=null in the select query
or
alter the software_instance table , to accept null values for name column.
ALTER TABLE SOFTWARE_INSTANCES ALTER COLUMN NAME DROP NOT NULL

Cannot Alter Column in Squirell SQL Client

I am trying to alter a column in my ingres DB to expand the size of the column.
The query i'm running is
ALTER TABLE test_table ALTER COLUMN address varchar(100) NOT NULL
Which gives error
Error: ALTER TABLE: invalid change of attributes on an ALTER COLUMN
SQLState: 42000 ErrorCode: 3859
Anyone any idea why I'm getting this error? I've checked the syntax for altering tables.
Probably you have NULL in data. Update your table first (set to empty string or any value you want):
LiveDemo
UPDATE test_table
SET address = '' -- or another value indicating missing addres like 'none'
WHERE address IS NULL;
And then try:
ALTER TABLE test_table ALTER COLUMN address varchar(100) NOT NULL;
EDIT:
If you don't want to enforce NOT NULL use just:
LiveDemo2
ALTER TABLE test_table ALTER COLUMN address varchar(100);

Invalid Identifier in ALTER TABLE ADD COLUMN

I'm getting the error "Invalid Identifier" whilst running this ALTER TABLE statement:
ALTER TABLE TRUCK ADD COLUMN WEIGHT INTEGER NOT NULL;
The syntax is like this, so I don't understand the error:
ALTER TABLE table_name ADD COLUMN column_name data_type[NOT NULL][UNIQUE]
The attribute WEIGHT doesn't need unique.
How do I include the "not greater than 1000" for the Integer data type into the Alter statement?
SQL Server:
ALTER TABLE TRUCK
ADD WEIGHT INT NOT NULL
This query will be helpful.
alter table truck
add column weight int not null default 0 check (weight < 1000)