Add a constraint with a where statement - postgresql

I want to add a NOT NULL to a column that is conditional on another column.
ALTER TABLE mailers ALTER COLUMN original_id SET NOT NULL
WHERE offer_type = 'client cross'
In other words, if one column have 'client cross' do not let a different column be NULL.

You cannot create a NOT NULL column constraint with a condition. Either the column can contain NULL values or it can't.
However, you can create a normal CHECK constraint for the entire row:
ALTER TABLE mailers
ADD CONSTRAINT "non-null_original_id_for_client-cross"
CHECK (original_id IS NOT NULL OR offer_type <> 'client cross');

Related

Add a not null constraint to a table in which a column has a certain value in PostgreSQL

I'm trying to add a NOT NULL constraint in PostgreSQL, but can't find the right syntax.
Here is, essentially, what I'm trying to do:
ALTER TABLE olimpic.tb_athlete
ADD CONSTRAINT soloESP CHECK(country = 'ESP' AND substitute_id IS NOT NULL)
and the table I'm trying to modify:
CREATE TABLE olimpic.tb_athlete
(
athlete_id CHAR(7) PRIMARY KEY,
name VARCHAR(50) NOT NULL,
country CHAR(3) NOT NULL,
substitute_id CHAR(7),
FOREIGN KEY (substitute_id) REFERENCES olimpic.tb_athlete(athlete_id)
);
I have already deleted or set default values to the column country where the value is 'ESP', with this code being an example:
DELETE FROM olimpic.tb_athlete
where substitute_id is NULL and country = 'ESP';
but I'm still getting the following error:
ERROR: ERROR: check constraint 'soloESP' on table tb_athlete is violated by some row
SQL state: 23514
Any help you could give me as to how to proceed would be greatly appreciated.
Do you realize that the constraint you're trying to add does not allow any rows with the country field other than 'ESP'? You say you want two conditions simultaneously (because you use the AND operator): each row must have country = 'ESP' AND non-null substitute_id
I believe what you wanted is
ALTER TABLE olimpic.tb_athlete
ADD CONSTRAINT soloESP CHECK(country != 'ESP' OR substitute_id IS NOT NULL);
This constraint will ensure that if country = 'ESP' then substitute_id must be non-null. For other countries both null and non-null values of substitute_id are valid.
But the above is only a guess because you provided neither your database's schema, nor meanings of the fields, nor the error text in English, nor the data stored in your database so that we could analyze what is really happening in your case. Please, consider editing the question to add the above

Alter DB2 table with existent records with a new not null default '' column

what will happen with existent records with this new column, will they have default '' ?
or this is only applying for new records? is this a safe way? Appreciate your help, regards.
SQL code:
ALTER TABLE mytable ADD COLUMN newColumn VARCHAR(10) NOT NULL DEFAULT '';
Once you run below command -
ALTER TABLE mytable ADD COLUMN newColumn VARCHAR(10) NOT NULL DEFAULT '';
All the existing rows in the table will have the value as a single space in column newColumn, and for newly inserted rows, You can specify the value in insert statement.

Is it possible to add a column with unique constraint in one go?

I want to add an email column to an existing table with the UNIQUE constraint. Is it possible to do this with one statement?
When I try:
ALTER TABLE "Corporates"
ADD COLUMN "email" varchar(100) NOT NULL
CONSTRAINT "Corporates_email_key" UNIQUE ("email")
the query fails with the error:
ERROR: syntax error at or near "("
LINE 3: CONSTRAINT "Corporates_email_key" UNIQUE ("email")
^
SQL state: 42601
Character: 120
OTOH I could run the following two statements individually:
ALTER TABLE "Corporates"
ADD COLUMN "email" varchar(100) NOT NULL
and then:
ALTER TABLE "Corporates"
ADD CONSTRAINT "Corporates_email_key" UNIQUE ("email")
and it worked.
You need a second ADD option:
ALTER TABLE corporates
ADD COLUMN email varchar(100) NOT NULL,
ADD CONSTRAINT corporates_email_key UNIQUE (email);

db2 change column from null to not null

Have a Location column in XYZ table in db2, now I want to change to not null and using the below command
ALTER table xyz ALTER COLUMN LOCATIONID set not null
But asking to give default value. How to change the command for that
As you are making a previously optional column into a mandatory column, if there is already at least one row in the table that contains a NULL in LOCATIONID then Db2 may prevent the alteration (SQL0407N).
If the table has no rows, or if no rows have null in LOCATIONID column, then Db2-LUW will allow the alteration. You may need to REORG the table before/after the alteration in some cases.
If the table already has rows with LOCATIONID null, you must either set these rows LOCATIONID value to some not-null value before doing the alteration, or you must recreate the table.
When recreating the table, consider specifying a default value via 'NOT NULL WITH DEFAULT ...' if that makes sense for the data concerned.

T-SQL: Add NOT NULL constraint to multiple existing columns at once

I have a table like below:
CREATE TABLE dbo.MyTable (
Text int,
column2 varchar(50),
column3 varchar(50)
)
and now I would like using T-SQL to apply a NOT NULL constraints to the three columns at once (Note that I have a column called Text that matches with a reserved type in T-SQL), so I perform:
ALTER TABLE dbo.MyTable
ADD CONSTRAINT NOT_NULL NOT NULL (Text, column2, column3)
but it is not working:
Incorrect syntax near reserved word 'not'.
For one, your syntax is wrong.
ALTER TABLE dbo.MyTable
ALTER COLUMN column2 VARCHAR(50) NOT NULL
Note that NOT NULL doesn't quite behave as a constraint, but rather a feature of the column's type. As such, you don't use the ADD CONSTRAINT operation.
Secondly, you just have to use multiple statements, one for each column. There's really no benefit to adding the constraints all at once, so it doesn't matter.
The NOT NULL thing is not a constraint, but is part of the data type of the column, so the ADD CONSTRAINT don't applies at all to begin with. The correct way to do is to use an ALTER COLUMN, so the correct syntax would be
ALTER TABLE dbo.MyTable ALTER COLUMN [Text] INT NOT NULL,
[column2] VARCHAR(50) NOT NULL,
[column3] VARCHAR(50) NOT NULL ;
The thing of the column name colliding with the reserved keyword is deal with by enclosing the name in [], but that's not the original problem in your example.