add CHECK constraint to already populated table - sql

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}'

Related

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.

Check constraint on existing column with PostgresQL

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);

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])

How to add a PK to an existing table in DB2?

I am facing one problem. I have a table already create in DB2.
CREATE TABLE "DDL12"
(
"D4_1" decimal(10,0),
"D4_2" decimal(10,0),
);
I am trying to create a PK on this table as :-
ALTER TABLE "DDL12" ADD CONSTRAINT "Key4" PRIMARY KEY ("D4_1");
But while running the command, I am getting the error saying D4_1 is NULLABLE.
Now, how can I create a PK on this table?
Thanks
Yes, this is due the fact, that your database "could have" rows having NULL value in that non PK column right now.
So first set the column to NOT NULL (+ make sure having a unique value in all rows) and then set the primary key with the command above.
You can change a column to not NULL like this:
ALTER TABLE "DDL12"
MODIFY "D4_1" decimal(10,0) NOT NULL;

How to add unique column in sybase?

I have a sybase db table in which i need to add a new column.
The conditions: The column must not allow nulls and be unique.
what is the alter table sql to achieve this?
EDIT:
It is a varchar type column.Yes the table as of now is empty, but when filled it is ensured that unique values would be filled in.
I tired executing
alter table Test add Name varchar not null unique
i get error saying default value must be specified as not null is given.
but i want to add unique constraint so do i really need to specify default?
thanks
Unique values are specified as part of an index on the column, not in the column definition itself.
Try:
alter table Test add Name varchar not null
create unique index index_name_unique on Test (Name)
The ASE reference manual can help with more detail.
Once a table has been created Sybase ASE does not allow addition of NOT NULL column directly unless a default is specified for the column. However, if the table is still empty you can do the following -
First add the new column as a NULL column to the table using alter table command -
alter table Test add Name varchar(100) null
Once this has been done, try modifying the same column Name in the table Test using the alter table script -
alter table Test modify Name varchar(100) NOT NULL
and you will see that you are able to modify the Name column to a NOT NULL column using these steps. This is because at this time Sybase server checks as there is no data in the table hence the NOT NULL constraint is not checked and the column is made NOT NULL. Hence, we are able to skip the default constraint.
In case there would have been some data already present in the table Test, then we need to add one more step in between steps 1 and 2 which will add default values to the existing rows in the table. This can be done via a script for previous data and then following the step 2.
To make the column only allow unique values for the column you need to add a unique key constraint using the following syntax -
alter table Test add constraint UK1 unique(Name)