How to alter a table to add a constraint? - sql

So, the question I have is:
Due to various values entered by users for the status of the facility, this leads
to confusion. The database owner would like to limit the following values
“Open”, “Closed”, “Reserved”, and “Maintenance” to be used for the status of
facility.
My table FACILITY has the following columns:
FACILITYNAME
RATE
STATUS
I tried the following:
ALTER TABLE FACILITY ADD CONSTRAINT FACILITY_STATUS 'Open','Closed','Reserved','Maintenance' FOR STATUS;
I get an error, ORA-00904:invalid identifier
I then tried the following:
ALTER TABLE FACILITY ADD CONSTRAINT STATUS_CHECK CHECK (STATUS IN ('Open','Closed','Reserved','Maintenance'));
It said table altered, but when I tried updating the STATUS column, with 'abc' and 'Open', it says row updated, but nothing happened. I was expecting it to give me a constraint error for 'abc', and updating it to 'Open'.

Strange, for me it is working as expected:
CREATE TABLE FACILITY (
FACILITYNAME VARCHAR2(100),
RATE INTEGER,
STATUS VARCHAR2(20));
Table created.
INSERT INTO FACILITY VALUES ('f1', 1, 'Open');
1 row created.
ALTER TABLE FACILITY ADD CONSTRAINT STATUS_CHECK CHECK (STATUS IN ('Open','Closed','Reserved','Maintenance'));
Table altered.
UPDATE FACILITY SET STATUS = 'abc';
ORA-02290: check constraint (XXX.STATUS_CHECK) violated
Maybe verify the status of the constraint:
SELECT CONSTRAINT_NAME, STATUS, DEFERRABLE, DEFERRED
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'FACILITY';
+------------------------------------------------+
|CONSTRAINT_NAME|STATUS |DEFERRABLE |DEFERRED |
+------------------------------------------------+
|STATUS_CHECK |ENABLED|NOT DEFERRABLE|IMMEDIATE|
+------------------------------------------------+
Also note, unless you give clause VALIDATE in your ALTER TABLE the constraint does not check existing values! By default only new and updated values are affected by the constraint.

As #KaushikNayak suggested try adding check constraint:
ALTER TABLE FACILITY ADD CONSTRAINT check_status CHECK (FACILITY_STATUS IN ('Open','Closed','Reserved','Maintenance'));
Otherwise, Column level constraint must use another column:
a column-level constraint
Column-level constraints refer to a single column in the table and do not specify a column name (except check constraints). They refer to the column that they follow.
I suggest you create table FACILITY_STATUS with column STATUS as Primary key and insert the values.
Then add constraint to FACILITY table STATUS column using FACILITY_STATUS(STATUS) as foreign key

Related

Using a 'not in' query inside of a check constraint in SQL

I have two tables, one of student and one of staff that look as such:
create table student (
id int not null primary key
)
create table staff (
id int not null primary key
)
I want the id in each to be unique. I know this isn't how it should be in production but I'm just trying to see why my check constraint doesn't work and I'm using a simpler example to explain.
I then alter the tables to include the check as follows:
alter table student add constraint not_staff check (id not in (select id from staff))
alter table staff add constraint not_student check (id not in (select id from student))
These checks seem to be invalid.
My question is whether we're allowed to have these kinds of SQL statements inside of a check constraint. If so, why is the above constraint invalid and how would I go about fixing it.
Thanks!
You can't use queries in a check constraint in Db2. Refer to the description of the CREATE TABLE statement.
CHECK (check-condition)
Defines a check constraint. The search-condition must be true or unknown for every row of the table.
search-condition
The search-condition has the following restrictions:
...
The search-condition cannot contain any of the following (SQLSTATE 42621):
Subqueries
The easiest way to achieve your goal is not to create constraints, but create a sequence and use it in before triggers on both tables.

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.

PL/SQL constraint: Always one record in a table

Im trying to add a constraint to a table so that there can only be one record in the table.
This is the code I already have:
ALTER TABLE CONFIG
ADD CONSTRAINT always_one
CHECK (count(*)= 1);
And this is the error I'm getting
ALTER TABLE CONFIG
ADD CONSTRAINT always_one CHECK (count(*)= 1)
Error report -
SQL Error: ORA-00934: group function is not allowed here
00934. 00000 - "group function is not allowed here"
*Cause:
*Action:
How does this work?
You can use already proposed solution with adding unique constraint on column
alter table config add constraint always_one check (pk_col=1);
this however allows inserting more than one row in case pk_col is null in second inserted row. So you need to handle this by adding a NOT NULL constraint as well
ALTER TABLE config
ADD CONSTRAINT notnulc CHECK (pk_col IS NOT NULL) ;
To prevent deleting this row, you should probably create before delete trigger as follows
create or replace trigger trg_ONLYONE before delete ON CONFIG
DECLARE
C NUMBER;
BEGIN
SELECT COUNT(*) INTO C FROM CONFIG;
if (C=1) THEN
RAISE_APPLICATION_ERROR (-20011, 'TOO FEW ROWS');
END IF;
END;
Futher options are: instead of check constraints mentioned above is CREATE BEFORE INSERT trigger, or instead of NOT NULL and UNIQUE CONSTRAINT make pk_col PRIMARY KEY
Just create a unique index on a column in the table, and add a constraint that the value of this column must be a certain value.
eg.
CREATE UNIQUE INDEX one_val ON config(pk_col);
ALTER TABLE CONFIG
ADD CONSTRAINT always_one
CHECK (pk_col = 1);
If all of your other columns could be any value, you may need to just add this additional column, and give it a default value.

Alter table & Add UNIQUE key results in an error

I have a table called Animal. AnimalId is the primary key & I wanted to set the column AnimalType_id as UNIQUE (I have an AnimalType table and need to set a foreign key here)
ALTER TABLE Animal
ADD UNIQUE Animal.AnimalType_id int
There already is data in both tables, because of that I can't drop the table.
This however results in an error:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '.'.
See the documentation for how to add a table constraint.
ALTER TABLE Animal ADD CONSTRAINT UQ_Animal_AnimalTypeId UNIQUE (AnimalType_id)
It sounds like AnimalType_id is a foreign key so I just wanted to check you understood that by making this column unique, you're making the relationship one-one - you'll only be able to have one animal of each type.
Since you're getting an error adding the unique constraint, I'm going to suggest that you actually want a foreign key instead of a unique constraint:
ALTER TABLE Animal
ADD CONSTRAINT FK_Animal_AnimalType
FOREIGN KEY
(
AnimalType_id
)
REFERENCES AnimalType
(
id
)
I've had to guess at the name of the AnimalType table name and it's primary key column name - please change these if they are incorrect.
If you get into the habit of giving names to all objects (even constraints) that you create, you will have easier time later when you need to disable, drop, or alter the constraint:
ALTER TABLE Animal ADD CONSTRAINT UQ_Animal_Type UNIQUE (AnimalType_id)
It is also possible to get a more flexible constraint-like effect from creating a unique index.
I think you are trying to do this:
ALTER TABLE Animal
ADD COLUMN AnimalType_id int;
It seems the data in the column is not unique.
when you create a unique constraint on a column, you can not have any duplicate entries (you can have at most one null)
try this
ALTER TABLE table_name ADD CONSTRAINT UNIQUE (column_name)

SQL: Create new column with default, unique value

I have added a new column, called Ordinal, to a table called Activity. The problem is that I gave it a UNIQUE constraint, set it to allow NULL (though this I won't want in the end.. I just needed to set it to that to get a little farther with the script), and did not give it a default value. I'm now running a RedGate SQL Compare script that was generated by comparing this table to a version of the Activity table that does not have the column. But I'm getting the following error:
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'iwt.Activity' and the index name 'IX_Activity'. The duplicate key value is (1).
So based on my research, it's trying to create a unique key constraint on the Ordinal column, but NULL is not unique. So my next step was to give it a unique value of 1 just to let the script pass. But 1 isn't going to be unique either. So, finally, my question:
Preferably in SQL Server Management Studio, how do I set a column as having a unique default value? Isn't that what I would need to create this constraint?
Thanks.
try this:
NULL will be the first constraint when you create the column.
UNIQUE will be as add constraint, you should add the second constraint.
they can run on this order with no problem (tested):
--first constraint
alter table Table_Name
add Column_Name int null
--second constraint
alter table Table_Name
add constraint Constraint_Name unique (Column_Name)
In my example :
PaymentGatewayHash is column
Cart is a table
--first query
alter table Cart
add PaymentGatewayHash NVARCHAR(20) null
--second query
CREATE UNIQUE INDEX PaymentGatewayHashUnique
ON Cart (PaymentGatewayHash)
WHERE PaymentGatewayHash IS NOT NULL
I just tested that :D