Check constraint on existing column with PostgresQL - sql

I'm trying to put check constraint on existing column.
Is there any way to achieve this from PostgreSQL?

Use alter table to add a new constraint:
alter table foo
add constraint check_positive check (the_column > 0);
More details and examples are in the manual:
http://www.postgresql.org/docs/current/static/sql-altertable.html#AEN70043
Edit
Checking for specific values is done in the same way, by using an IN operator:
alter table foo
add constraint check_positive check (some_code in ('A','B'));

If you are okay with (or want) Postgres to generate a constraint name you can use the following shorthand syntax.
ALTER TABLE foo
ADD CHECK (column_1 > 2);

You can add a new constraint with with alter table command. From documentation this example:
ALTER TABLE distributors
ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;
You will have to replace constraint name as well as table name and content by your local requirements.

This should do it:
create table test (
id serial
);
alter table test
add constraint id_not_null
check (id is not null);

Related

Trouble Altering Table SQL

Hey guys just wondering what i'm doing wrong in this script.
ALTER TABLE SSV_TOURS (
ADD CRUISE_ID# CHAR(5),
ADD CONSTRAINT TOURS_CRUISEID#_FK FOREIGN KEY (CRUISE_ID#) REFERENCES SSV_CRUISES(CRUISE_ID#)
);
When i do the ADD commands individually the table alters, so I'm not really sure what I'm doing wrong. Thanks in advance
In Oracle, alter table only allows one modification at a time. You can see this in the syntax diagram in the documentation: There are no back arrows.
So:
ALTER TABLE SSV_TOURS ADD CRUISE_ID# CHAR(5);
ALTER TABLE SSV_TOURS ADD CONSTRAINT TOURS_CRUISEID#_FK FOREIGN KEY (CRUISE_ID#) REFERENCES SSV_CRUISES(CRUISE_ID#);
I think you can't mix column_clause and constraint_clause in one alter table sentence. However in your case you either need to split your alter table to two (one with add column and other with add constraint) or slightly rewrite your statement as
ALTER TABLE SSV_TOURS (
ADD CRUISE_ID# CHAR(5)
CONSTRAINT TOURS_CRUISEID#_FK FOREIGN KEY (CRUISE_ID#)
REFERENCES SSV_CRUISES(CRUISE_ID#)
);

How to add constraint to table in sql?

I create a table
create table CARS{
CAR_ID NUMBER(10), CONSTRAINT X_CAR_ID NOT NULL
}
and now I want to change the name of the constraint, so I drop the constraint:
ALTER TABLE CARS DROP CONSTRAINT X_CAR_ID;
This works correclty but, when I tried to add new constraint I have a problem,
my query:
ALTER TABLE CARS ADD CONSTRAINT XX_CAR_ID (CAR_ID) NOT NULL;
I thought that query, will be working correctly, but I get only error report:
Error report -
SQL Error: ORA-00904:
How to add correctly this constraint ?
While I couldn't test it I believe the statement below is what you want:
ALTER TABLE CARS MODIFY CAR_ID CONSTRAINT XX_CAR_ID NOT NULL;
Oracle uses the modify keyword in this context.
To rename it without dropping first you would use:
alter table cars rename constraint x_car_id to xx_car_id;
See the reference for more info.

SQL - Add Unique Constraint Failure

Trying to alter a table in SQL Server. I want to add a unique constraint to a column called Names in table ReportingItemNames:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([ReportingItemNames,Name])
But I am getting this error:
Column name 'ReportingItemNames,Name' does not exist in the target table or view
Where am I going wrong?
Use this:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames UNIQUE ([Name])
You can refer to ALTER TABLE (Transact-SQL) documentation for more information.
Shouldn't it be:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([Name])

add CHECK constraint to already populated table

I created a table called test with column called code:
create table test(
code char(3) not null);
I then populated the table with the following data:
insert into test values ('A12');
insert into test values ('B23');
insert into test values ('C45');
I then altered the column to make it char(4):
alter table test
alter column code char(4) not null;
I then added a 'X' to all existing data so that it becomes 4 characters long:
update test
set code='X'+code
where LEN(code)=3;
So far so good but then when I tried to add a check constraint:
alter table test
add constraint codeCheck check (code like 'A-Z''A-Z''0-9''0-9');
I got this error:
The ALTER TABLE statement conflicted with the CHECK constraint "codeCheck".
I understand that the error implies that the existing data violates the check constraint that I am trying to add into the table, but why?
and how do I do it such that the existing data and check constraint do not violate each other?
Your pattern syntax is wrong. It should be
alter table test
add constraint codeCheck check (code like '[A-Z][A-Z][0-9][0-9]');
Because your data doesn't match the like constraint.
Try
alter table test
add constraint codeCheck check (code like '[A-Z][A-Z][0-9][0-9]' );
I donĀ“t know how it works with SQL Server but your like clause looks odd.
Try using
'[A-Z]{2}\d{2}'

Command for adding a default constraint

There appears to be at least two ways to add a default constraint using straight T-SQL. Am I correct that the only difference between the two below is that the second method specifically creates a name for the constraint, and the first method has one generated by SQL Server?
ALTER TABLE [Common].[PropertySetting] ADD DEFAULT ((1)) FOR [Active];
ALTER TABLE [Common].[PropertySetting] ADD CONSTRAINT [DF_PropertySetting_Active) DEFAULT ((1)) FOR [Active];
Pretty much, yes for an ALTER TABLE
You can add a columnn with default in one step for CREATE or ALTER too.
ALTER TABLE dbo.TableName
ADD bar varchar(100) CONSTRAINT DF_Foo_Bar DEFAULT ('bicycle');
ALTER TABLE dbo.TableName
ADD bar varchar(100) DEFAULT ('bicycle');
As you noted, the system generates a name if one is not supplied. CONSTRAINT constraint_name is optional says MSDN. The same applies to any column or table CONSTRAINT
If the column was already created, and you only want to add a (named) DEFAULT constraint, then use:
ALTER TABLE dbo.TableName
ADD CONSTRAINT DF_Foo_Bar DEFAULT 'bicycle' FOR FieldName;
To have the system generate the DEFAULT constraint name (which will be of the form DF_{TableName}_{Column}_{8RandomChars}, e.g. DF_TableName_FieldName_12345678) then omit the CONSTRAINT <name> part, like so:
ALTER TABLE dbo.TableName
ADD DEFAULT 'bicycle' FOR FieldName;