how to add check constraints for date columns - sql

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

Related

check constraint that references multiple columns in the same table

I am attempting to add a constraint to a DB2 database that will check three columns. I am using a table that is an invoice table that includes start date end date quantity item price etc. for each line item on an invoice. I want to prevent allowing start and end date from being null when a column linestatus = RELELASED. Here is the alter statement that I have so far. My question is why won't this work? I have verified that this table does not have any current instances of all three of these checks.
alter table pluspgbtrans
add constraint start_end_notnull
Check (eip_linestatus = 'RELEASED' AND eip_endate is not null AND eip_startdate is not null)
Your SQL statement is valid.
However, your logic has an error: this check does not apply only if eip_linestatus = 'RELEASED'.
As written, your constraint is asserting that all rows must have eip_linestatus = 'RELEASED' AND eip_endate is not null AND eip_startdate is not null.
So, if any rows in your table have eip_linestatus with a value of anything other than RELEASED, you'll get the SQL0544N error when you try to add the constraint.
To create the constraint you're looking for, you need to handle the other state(s) for eip_linestatus. I can't guess what they are, so here's a potential generic option:
alter table pluspgbtrans
add constraint start_end_notnull check (
(eip_linestatus <> 'RELEASED')
OR
(
eip_linestatus = 'RELEASED'
AND eip_endate is not null
AND eip_startdate is not null
)
);

Constrains using DATE

I'm trying to create a constrain to check the record is no greater than 2016.
Here is the record in my database
Here is my query:
ALTER TABLE SIGHTINGS
ADD CONSTRAINT CK_SIGHTING_DATE
CHECK (SIGHTING_DATE <=TO_DATE('01-JAN-16'));
But I got an error says: ERROR at line 1:
ORA-02436: date or system variable wrongly specified in CHECK
constraint.
I've checked some similar questions on this website but there solutions doesn't solve my problem.
One option is to use the extract() function as you just want to check for the year:
ALTER TABLE SIGHTINGS
ADD CONSTRAINT CK_SIGHTING_DATE
CHECK (extract(year from SIGHTING_DATE) < 2016);
or use an ANSI date literal:
ALTER TABLE SIGHTINGS
ADD CONSTRAINT CK_SIGHTING_DATE
CHECK (SIGHTING_DATE < date '2016-01-01');
you have make in date specifcation
ALTER TABLE SIGHTINGS
ADD CONSTRAINT CK_SIGHTING_DATE
CHECK (SIGHTING_DATE <=
/*TO_DATE('01-JAN-16','DD-MON-YY') as I was pointed your should specify 4 digits for year*/
TO_DATE('01-JAN-2016','DD-MON-YYYY'));
or
ALTER TABLE SIGHTINGS
ADD CONSTRAINT CK_SIGHTING_DATE
CHECK (SIGHTING_DATE <=DATE'2016-01-01');
another one things its what do you mean when you say "no greater than 2016"
Your check alow dates in 01-jan-2016 but not allow 02-jan-2016.
If you would like to include whole 2016 year write
SIGHTING_DATE < DATE'2017-01-01'
or
trunc(SIGHTING_DATE,'yy') <=DATE'2016-01-01'

How to add a new column and a constraint in one go?

I want to add a new column to an existing table.
I need it to emulate the enum type (in the way possible in SQL Server; with value constraints, that is).
The following doesn't work:
ALTER TABLE orders ADD [sent_to_panel] NVARCHAR(16) NULL;
ALTER TABLE orders WITH CHECK ADD CONSTRAINT [CK_orders] CHECK (([sent_to_panel]='invalidated' OR [sent_to_panel]='not_sent' OR [sent_to_panel]='sent'));
ALTER TABLE orders ADD CONSTRAINT [DF_orders_sent_to_panel] DEFAULT (N'not_sent') FOR [sent_to_panel];
ALTER TABLE orders CHECK CONSTRAINT [CK_orders];
I'm getting an error:
Msg 207, Level 16, State 1, Line 2
Invalid column name 'sent_to_panel'.
If I execute the first command on its own, though:
ALTER TABLE orders ADD [sent_to_panel] NVARCHAR(16) NULL;
The rest goes through.
So I suppose the problem is that the column isn't actually added yet (and thus not recognized by ADD CONSTRAINT) when trying to get it all done in one go.
The question is: how to make the script work properly?
CREATE TABLE a (
b int
);
ALTER TABLE a
ADD c nvarchar(16) NULL
, CONSTRAINT check_this CHECK (c IN ('invalidated', 'not_sent', 'sent'))
, CONSTRAINT defaultify DEFAULT ('not_sent') FOR c
;
ALTER TABLE a
CHECK CONSTRAINT check_this
;
DROP TABLE a;

Future Dates SQL Developer

I'm trying to create a constraint that does not allow dates in future years. I have this:
ALTER TABLE PACIENTE ADD CONSTRAINT ck_FechaNacimiento
CHECK (FechaNacimiento<=current_date);
But i'm getting error 02436.
You cannot create a non-deterministic constraint. So you cannot create a constraint that references a function like current_date or sysdate that returns a different value every time you call it.
If you want to enforce this sort of thing, you'd need to create a trigger on the table that throws an error if the business rule is violated, i.e.
CREATE OR REPLACE TRIGGER trg_paciente
BEFORE INSERT OR UPDATE
ON paciente
FOR EACH ROW
BEGIN
IF( :new.FechaNacimiento > current_date )
THEN
RAISE_APPLICATION_ERROR( -20001, 'FechaNacimiento<=current_date must be in the past' );
END IF;
END;
I'd tried again with this and didn't show error, thanks by the way:
ALTER TABLE EXAMENPACIENTE ADD CONSTRAINT ExamenPaciente_FechaExamen_c1
CHECK (FechaExamen<='30-SEP-2013');
ALTER TABLE PACIENTE ADD CONSTRAINT ck_FechaNacimiento CHECK (FechaNacimiento<=SYSDATE);

Why does this 'modify table' statement fail?

I'm trying to add a 'not null' constraint to a column in Oracle 9.
ALTER TABLE user_roles modify group_id varchar2(36 char) set not null;
However, the operation fails with an odd error:
Error report:
SQL Error: ORA-12987: cannot combine drop column with other operations
12987. 00000 - "cannot combine drop column with other operations"
*Cause: An attempt was made to combine drop column with other
ALTER TABLE operations.
*Action: Ensure that drop column is the sole operation specified in
ALTER TABLE.
Any ideas why this is failing?
Remove set:
ALTER TABLE user_roles modify group_id varchar2(36 char) not null
And yes, Oracle's errors can be very misleading.
It turns out the syntax of the above statement is wrong. It should be:
ALTER TABLE user_roles modify group_id varchar2(36 char) not null;
Still, the presence of an erroneous 'set' leads to a very odd error!
I'm trying to add a 'not null'
constraint to a column in Oracle 9.
If you are really trying jsut to make the column NOT NULL (i.e. you don't want to change the datatype at the same time) you just need to
ALTER TABLE user_roles modify not null;