SQL oracle add check constraint to an existing table - sql

Im using SQL/PL developer and I have a table named Appeal, that has 2 attributs OpenDate and CloseDate. And I want to add a constraint to ensure that open date will be smaller than close date. I have a lot of records in this table.
this is my code:
alter table appeal
add constraint Check_Dates
check (OpenDate < CloseDate)
and I get en error saying:
ORA-02293: cannot validate (STSTEM.CHECK_DATES) - check constraint violated
any ieads?
Thanx

Your constraint looks right, I have tested it:
create table appeal ( OpenDate date, CloseDate date);
alter table appeal
add constraint Check_Dates
check (OpenDate < CloseDate);
insert into appeal values ( sysdate, sysdate - 1 );
And here the result:
Schema Creation Failed: ORA-02290: check constraint
(USER_4_44096.CHECK_DATES) violated
Problem is than you have already rows with OpenDate < CloseDate values in your database. Fix it before create constraint. Look behavior changing sentences order:
create table appeal ( OpenDate date, CloseDate date);
insert into appeal values ( sysdate, sysdate - 1 );
alter table appeal
add constraint Check_Dates
check (OpenDate < CloseDate);
And here your issue:
Schema Creation Failed: ORA-02293: cannot validate
(USER_4_E4450.CHECK_DATES) - check constraint violated

Try this
alter table appeal
add constraint Check_Dates
check (OpenDate < CloseDate) ENABLE NOVALIDATE;
You will have check the previous data for errors but any new Data will fall under the CHECK

Related

SQL_Add constraint and check the string using LIKE

I am using Oracle SQL.
Here is the table rental, and the CC_Type column means the credit card type. After creating the table, I want to add a constraint to make sure the credit card is either 'credit' or 'debit'
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
Therefore, I try to write:
ALTER TABLE RENTAL
2 ADD CONSTRAINT CC_TYPE_CK
3 CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
But SQL yield: cannot validate (SYSTEM.CC_TYPE_CK) - check constraint violated
I don't understand it violated what? And how to fix it?
Thanks!!
You apparently have data in the table that violates the constraint. Your code basically works; here is a db<>fiddle (this fixes the extra comma before the closing paren in the create table statement).
So, look for the bad data:
select r.*
from rental r
where r.cc_type not in ('Credit', 'Debit');
I would also write the constraint using in . . . it is simpler:
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit'));
just remove , after Member_ID VARCHAR2(5) other wise your query working fine
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
demo link
I agree with Gordon Linoff, Check for the data in cc_type.
select Distinct r.cc_type
from rental r;
If you still want to create the constraint even when you have the other CC_TYPEs(in the other words, without validating the existing data) then try this
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit')) NOVALIDATE;

how to add check constraints for date columns

I am getting this error
ORA-02438: Column check constraint cannot reference other columns
when I am performing this query
alter table Issue
modify Issue_Date not null check (Issue_Date <= sys_date);
as well as I have to add this condition also (issue_date<return_date);
and when I tried this
alter table Issue
add constraint ck_Issue_Date not null check (Issue_Date <= sys_date);
ERROR ORA-00904: : invalid identifier
I suspect you are wanting to reference the Oracle SYSDATE function, not a column named sys_date.
Unfortunately, the conditions in a CHECK CONSTRAINT cannot reference the SYSDATE function.
To get the database enforce this type of restriction on the value of a column, that would require a TRIGGER.
For example, something like this:
CREATE OR REPLACE TRIGGER trg_issue_issue_date_biu
BEFORE INSERT OR UPDATE ON Issue
FOR EACH ROW
BEGIN
IF (NEW.Issue_Date <= SYSDATE) THEN
NULL;
ELSE
RAISE_APPLICATION_ERROR(-20000, 'Invalid: Issue_Date is NULL or >SYSDATE');
END IF;
END;
You tried to mix up inline column level constraint and table level constraint (for more than 1 column). Please simply split them on 2 statements:
alter table Issue
modify Issue_Date not null;
alter table Issue
add constraint ck_Issue_Date check (Issue_Date <= sys_date);
alter table Issue
add constraint ck_Issue_Date2 check (issue_date<return_date);

Need to add constraint: date plus 10 days

I'm trying to add a constraint to a table so that it displays one of the columns as the current date plus 10 days. Here's what I've tried so far (I'm very new to SQL):
ALTER TABLE orders
ADD CONSTRAINT default_date
DEFAULT DATEADD (DAY,10,required_date) FOR required_date
Halp!
Edit: I've also tried this now:
ALTER TABLE orders
ALTER COLUMN required_date
ADD CONSTRAINT required_date_plus_ten
DEFAULT DATEADD (DAY,10,required_date)
Edit: Thanks to ypercube & my classmate. The final code is:
ALTER TABLE orders
ADD CONSTRAINT default_date
DEFAULT (DATEADD (DAY,10,'required_date')) FOR required_date;
The syntax in SQL-Server, for adding a DEFAULT value to an existing column is:
ALTER TABLE orders
ADD CONSTRAINT required_date_plus_ten
DEFAULT DATEADD(day, 10, GETDATE())
FOR required_date ;
Tested in SQL-Fiddle

Using a check constraint on a date

I want to make sure that a person's Date of Birth must be less than the current date.
So I declared in a table:
staff_dob SMALLDATETIME NOT NULL CHECK (GETDATE() < staff_dob)
But when I keep getting conflicts with the check constraint. How do I fix this? Do I need to formate GETDATE() into a proper format that I use? I'm unsure on how to do it.
Try this code:
drop table test
create table test
(staff_dob datetime check (staff_dob < getdate()))
--this insert will fail
insert test
(staff_dob)
values
('1/1/2013')
--this insert will succeed
insert test
(staff_dob)
values
('1/1/2011')
I think your check comparison was in the wrong direction.
take a look at Creating and Modifying CHECK Constraints
CREATE TABLE [dbo].[Staff](
[staffid] [int] NULL,
[dob] [date] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Staff] WITH CHECK ADD CONSTRAINT [CK_Staff] CHECK (([dob]<getdate()))
GO
ALTER TABLE [dbo].[Staff] CHECK CONSTRAINT [CK_Staff]
GO
hope this helps

SQL Date option

I have a little doubt, I want to create a table that had a date that can't be bigger the 2012/12/31, i searched on google but only had exemples on SELECT. I'm gonna put an example:
CREATE TABLE example(
IDExample number (8) primary key,
DateExample date // Here i want to put that condition, is it possible?
);
If you're using SQL Server you can add check contraint on the column in the following way.
ALTER TABLE dbo.example ADD CONSTRAINT CK_DateExample
CHECK (DateExample < '20130101')
If you're using Oracle, the syntax is very similar:
ALTER TABLE dbo.example ADD CONSTRAINT CK_DateExample
CHECK (DateExample < DATE '2013-01-01')