Future Dates SQL Developer - sql

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

Related

Date constraint

I need a check constraint for date so no data can be entered in the past. I'm using oracle live SQL for a school project and cant find a solution.
(BookingID Varchar2(6) PRIMARY KEY NOT NULL,
App_Date Date NOT NULL,
App_Time varchar2(8) NOT NULL,
Location Varchar2(15) NOT NULL,
Query Varchar(50) NOT NULL);
Oracle interprets check constraints as forever constraints on the data. That is, the check constraint is valid not only when the data changes but forever thereafter.
For this reason, Oracle does not allow volatile functions in check constraints. A volatile function is one whose values can change over time. A very good example are the date/time functions, such as sysdate (which is really a function without parentheses).
So, you cannot do what you want with a check constraint. You need to set this up using an insert trigger.
You can't use sysdate in a check constraint, because, as explained by Gordon Linoff, it is a non-deterministic function (ie the value it gives is not constant over time).
Instead, you can implement the validation with a trigger:
create or replace trigger app_date_not_in_past
before insert or update on mytable
for each row
begin
if (:new.app_date < sysdate)
then
raise_application_error( -20001, 'app_date date must not be in the past' );
end if;
end;
/

Trigger to disallow inserting negative number to tables' column

In my mssql database I need to create a trigger that will disallow inserting negative budget to projects table. I don't really know how can I do it. All kind of help will be appreciated!
CREATE TRIGGER budget on PROJ FOR INSERT
For all answers ommiting triggers - unfortunately I need to do this with trigger
You do not need a trigger for this. SQL Server supports check constraints:
alter table projects add constraint chk_projects_budget check (budget >= 0);
There is no reason for not using a CHECK constraint. However you can certainly implement that constraint with a trigger by checking if there is any new row that has negative budget and raising an error on that case.
CREATE TRIGGER projects_positive_budget ON projects
FOR INSERT, UPDATE
IF EXISTS (SELECT * FROM inserted WHERE budget < 0)
BEGIN
RAISERROR('No negative budget is allowed', 16, -1);
ROLLBACK;
RETURN
END
GO
If you want to check multiple columns you can also
ALTER TABLE YourTableName
ADD CONSTRAINT CH_CheckForNegative CHECK(Col1 >=0 AND Col2 >= 0);
You can try this to add constraint for checking acceptable integer values either positive or negative.
CREATE TABLE [dbo].[Tbl](
[F1] [int] NULL,
[F2] [int] NULL
) ON [PRIMARY]
GO
--To accept only positive numbers and 0
ALTER TABLE [dbo].[Tbl] WITH CHECK ADD CONSTRAINT [CK_Tbl] CHECK (([F1]>=(0)))
GO
--To check constraint on the table
ALTER TABLE [dbo].[Tbl] CHECK CONSTRAINT [CK_Tbl]
GO
--To accept only negative numbers and 0
ALTER TABLE [dbo].[Tbl] WITH CHECK ADD CONSTRAINT [CK_Tbl_1] CHECK (([F2]<=(0)))
For more information you can check the link Integer Constraint

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

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;