Alter table to add a check condition in SQL - sql

Given:
CREATE TABLE SHOW (
ID VARCHAR2(10),
GENRE VARCHAR2(25) NOT NULL,
CONSTRAINT CH_GENRE CHECK (GENRE IN ('war','western')),
)
How can I add 'drama' to ch_genre using an alter table?
I supposed that is something like:
ALTER TABLE TV_SHOW
ADD CONSTRAINT ADD_ROMANTIC
CHECK (GENRE IN ('romantic'));
But it doesn't work.

Technically, you need to drop and recreate the constraint:
alter table show drop constraint ch_genre;
alter table show
add constraint ch_genre
check (genre in ('war', 'western', 'romantic'))
enable
;
If you have a lot of rows, validating the constraint may be expensive. On the other hand, since we are just adding another possible value, we know that all values are valid already, so we can skip the validation as follows:
alter table show
add constraint ch_genre
check (genre in ('war', 'western', 'romantic'))
enable novalidate
;
If you find yourself modifying the list of values on a regular basis, that's an indication that you should have a separate table to store that list, say genres, along with a foreign key that relates show to genres (probably through an integer, surrogate primary key rather than a string).

Simply>>
Drop and Recreate Your Constraint
ALTER TABLE SHOW Drop CONSTRAINT CH_GENRE; ALTER TABLE SHOW ADD
CONSTRAINT CH_GENRE CHECK (GENRE IN ('war','western','Drama'));

Drop and recreate the check constraint like this:
CREATE TABLE SHOW (
ID VARCHAR2(10),
GENRE VARCHAR2(25) NOT NULL,
CONSTRAINT CH_GENRE CHECK (GENRE IN ('war','western'))
);
Table SHOW created.
alter table SHOW drop constraint CH_GENRE ;
Table SHOW altered.
alter table SHOW add constraint CH_GENRE check (GENRE IN ('war','western','romantic'));
Table SHOW altered.

Related

Postgresql: Changing Action For Foreign Key Constraint

I have a simple table like below.
create table chemlab.rule_header (
id serial PRIMARY KEY,
name varchar(50),
grade varchar(20),
class_tag varchar(20), --tag added to sammple if match
parent_id int REFERENCES chemlab.rule_header(id) DEFAULT NULL,
unique( grade, class_tag )
)
But afterwards, I found that I need to add ON DELETE action, the default is NO ACTION. I couldn't figure out how to change the action.
Now I have to DROP & ADD
ALTER table chemlab.rule_header
DROP CONSTRAINT rule_header_parent_id_fkey ;
ALTER TABLE rule_header
ADD CONSTRAINT rule_header_parent_id_fkey
FOREIGN KEY (parent_id) REFERENCES chemlab.rule_header(id) ON DELETE RESTRICT;
So what is the correct syntax to alter an action on foreign key constraint ?
Well, this not directly altering FOREIGN KEY constraint, and there are DROP and ADD still, though this is only one statement:
ALTER table chemlab.rule_header
DROP CONSTRAINT rule_header_parent_id_fkey,
ADD CONSTRAINT rule_header_parent_id_fkey
FOREIGN KEY (parent_id) REFERENCES chemlab.rule_header(id) ON DELETE RESTRICT;
Take a look at the documentation at https://www.postgresql.org/docs/current/sql-altertable.html. There are options to alter a few things about a constraint (like DEFERRABLE) but not for changing the action, as I understand you need.

ALTER COLUMN Command doesn't work SQL Server

i want to add to a primary key in one table a references to the primary key of another table.
my code:
CREATE TABLE[payment]
(ID int Primary key)
CREATE TABLE [tab]
(ID int Primary key references tab2(ID))
Alter Table payment
alter column ID
ADD constraint fk_payment
references tab(ID)
i get the error that the syntax near constraint is wrong, but i don't know what to change
because of the not changeable order of the table Alter table is the only option. to reference from one table to the other doesn't work cause I've references from that table to another one already.
i need two one-to-one-relations from one table to another
If you want to add a FK constraint, just use this code:
ALTER TABLE dbo.payment
ADD CONSTRAINT fk_payment
FOREIGN KEY(ID) REFERENCES dbo.tab(ID)
You don't need to alter the column or table - just add the constraint

query to modify the constraint in oracle

I have the below query from oracle query point of view is that I have created a constraint on table BOA_INVOICE as shown below
ALTER TABLE BOA_INVOICE ADD CONSTRAINT CK_INVOICE_SOURCE_SYSTEM CHECK ( SOURCE_SYSTEM IN ('PCE','PDS'));
Now this constraint is added successfully , but later on i want to modify same constraint add two values as shown below
ALTER TABLE BOA_INVOICE ADD CONSTRAINT CK_INVOICE_SOURCE_SYSTEM CHECK ( SOURCE_SYSTEM IN ('PCE','PDS','PER','AWE'));
Please advise what will be the query to achieve the same
You need to drop the constraint first and then create it again.
ALTER TABLE BOA_INVOICE DROP CONSTRAINT CK_INVOICE_SOURCE_SYSTEM;
Then Create it again:
ALTER TABLE BOA_INVOICE ADD CONSTRAINT
CK_INVOICE_SOURCE_SYSTEM CHECK ( SOURCE_SYSTEM IN ('PCE','PDS','PER','AWE'));

How to drop more than one constraint at once (Oracle, SQL)

I'm changing constraints in my database and I need to drop some of them. I know that for a single constraint, the command is following:
ALTER TABLE tblApplication DROP CONSTRAINT constraint1_name;
However, when I try
ALTER TABLE tblApplication DROP (
CONSTRAINT constraint1_name,
CONSTRAINT constraint2_name,
CONSTRAINT constraint3_name
);
it doesn't work and I need to do:
ALTER TABLE tblApplication DROP CONSTRAINT constraint1_name;
ALTER TABLE tblApplication DROP CONSTRAINT constraint2_name;
ALTER TABLE tblApplication DROP CONSTRAINT constraint3_name;
Is there a way to remove more than one constraint in a single command? I'd like to avoid repeating ALTER TABLE tblApplication, just like with the ADD command:
ALTER TABLE tblApplication ADD (
CONSTRAINT contraint1_name FOREIGN KEY ... ENABLE,
CONSTRAINT contraint2_name FOREIGN KEY ... ENABLE,
CONSTRAINT contraint3_name FOREIGN KEY ... ENABLE
);
Yes you can. You just need to repeat 'drop constraint' per constraint. e.g.
alter table t1
drop constraint fk1
drop constraint fk2
/
Edit: I tested this against Oracle 11, and it worked fine. Don't know about older versions.
There is an alternative form to drop constraints related to a column in a table, also dropping the column with CASCADE:
ALTER TABLE table1 DROP (columnName) CASCADE CONSTRAINTS;
It is tested on Oracle 11g
example: we can drop constraints in MySQL by creating constraints to the variables like this way.
create table sample(id int, name varchar(30), marks int, constraint uid unique(id), constraint un unique(name));
alter table sample drop constraint uid, drop constraint un;
Yes, we can drop multiple at once:
ALTER TABLE TABLE NAME
DROP CONSTRAINTS CONSTRAINT VALUE
DROP CONSTRAINTS CONSTRAINT VALUE;

Dropping then adding a constraint fails in oracle

I'm trying to move a primary key constraint to a different column in oracle. I tried this:
ALTER TABLE MY_TABLE
DROP CONSTRAINT c_name;
ALTER TABLE MY_TABLE
ADD CONSTRAINT c_name PRIMARY KEY
(
"COLUMN_NAME"
) ENABLE;
This fails on the add constraint with an error saying the the constraint already exists even though i just dropped it. Any ideas why this is happening
If the original constraint was a primary key constraint, Oracle creates an index to enforce the constraint. This index has the same name as the constraint (C_NAME in your example). You need to drop the index separately from the constraint. So you will need to do a :
ALTER TABLE <table1> DROP CONSTRAINT C_NAME;
DROP INDEX C_NAME;
ALTER TABLE <table1> ADD CONSTRAINT C_NAME PRIMARY KEY
( COLUMN_2 ) ENABLE;
The safest way is to first add a unique index. Try this:
create unique index new_pk on <tabname> (column_2);
Then drop the old PK:
alter table <tabname> drop primary key drop index;
(Optional) Rename the index:
alter index new_pk rename to c_name;
And then add the PK again indicating the index to be used:
alter table <tabname> add constraint c_name
primary key (column_2) using index c_name;
I don't know if this is a similar problem but, in DB2, you have to actually commit following the alter table statement. Otherwise it's still hanging around.