How to add check constraint on time with a timestamp? - sql

I need to add a constraint that time entered in a column is only after 8:00 am. I've tried
alter table toy_store ADD CHECK (store_opening_time > TIMESTAMP '2013-05-05 08:00:00');
This only allows entry of timestamp greater than 5th May 8:00 am. I want to write a query that checks if the entry is after 8:00 am notwithstanding the date.

you can do it like this :
alter table toy_store ADD CHECK (EXTRACT(HOUR FROM store_opening_time) > 8 || NULL);

Related

Timezone aware uniqueness constraint

I am working with a timesheet app, used by users from multiple timezones. I am trying to introduce a unique constraint, that only allows users to clock_in or clock_out once per day in the local timezone.
Please refer to the following table declaration:
Table "public.entries"
---------------------------------------------
Column | Type |
---------------------------------------------
id | bigint |
user_id | bigint |
entry_type | string | enum(clock_in, clock_out)
created_at | timestamp(6) without time zone |
But little lost on how to handle the timezone-aware uniqueness.
Update:
I am considering 0:00 hrs to 23:55 hrs of local time zone as day.
User's timezone is stored in the users table but can move to the entries table if it helps with constraints.
I misread the question and wrote a bad answer, so here's a new one...
I assume this is a typical client-server-db setup. You need to obtain the local time zone from the client that's clocking in/out the user; Postgres doesn't know what it is. We'll figure out the user's local date from that and store it. Then we'll have a uniqueness index on <user, local date>.
I thought there'd be fancier ways to do this by storing the timestamptz with a separate time zone col and calculating the date within the uniqueness index, but Postgres doesn't allow us to use date_trunc within an index. So we're going to denormalize just a little and make things a lot easier with this additional date col.
CREATE TABLE clock_in (
user_id bigint NOT NULL,
created_at timestamptz NOT NULL, -- stores microseconds since epoch
local_date date NOT NULL, -- stores the <year, month, day> in whatever timezone the user clocked in from
-- optional for bookkeeping purposes: time_zone text NOT NULL,
UNIQUE(user_id, local_date)
);
Take a look at the official date/time type docs for further explanation of the above. IMO you shouldn't rely on DB constraints to reject bad user input. They're more of a second line of defense meant to ensure a self-consistent database. First your server should query the last clock-in and error out if it was in the same day, and also error if there was no clock-in that day. You'll be able to yield more useful error messages that way. Then you can insert...
INSERT INTO clock_in(user_id, created_at, local_date) (
SELECT 1, now(),
(date_trunc('day', now() AT TIME ZONE 'insert_users_timezone_here'))::date
);
Usage example for a client who has indicated it's in the PST timezone:
me=# CREATE TABLE clock_in ( user_id bigint NOT NULL, created_at timestamptz NOT NULL, local_date date NOT NULL, UNIQUE(user_id, local_date) );
CREATE TABLE
me=# INSERT INTO clock_in(user_id, created_at, local_date) ( SELECT 1, now(), (date_trunc('day', now() AT TIME ZONE 'PST'))::date );
INSERT 0 1
me=# INSERT INTO clock_in(user_id, created_at, local_date) ( SELECT 1, now(), (date_trunc('day', now() AT TIME ZONE 'PST'))::date );
ERROR: duplicate key value violates unique constraint "clock_in_user_id_local_date_key"
DETAIL: Key (user_id, local_date)=(1, 2022-04-13) already exists.
me=# INSERT INTO clock_in(user_id, created_at, local_date) ( SELECT 1, now(), (date_trunc('day', now() AT TIME ZONE 'PST' + interval '10' hour))::date );
INSERT 0 1
me=#
Then you'd do the same for clock-outs.
Using timestamptz instead of timestamp is deliberate. You should almost never use timestamp, for reasons other answers describe well.
Firstly, you'll probably want to use a native datetime datatype and a range one at that, e.g. tstzrange (with timezone) / tsrange (without timezone) – they allow you to natively store a start and end time – see https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-BUILTIN
You can optionally add an exclusion constraint to ensure no two shifts overlap – see: https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-CONSTRAINT if that's all you really want to ensure, then that might be enough.
If you definitely want to ensure there's only one shift starting or ending per day, you can use a function to derive a unique index:
create unique index INDEX_NAME on TABLE_NAME (date_trunc('day', lower(column_name)))
For your example specifically:
create unique index idx_unique_shift_start_time on entries (user_id, date_trunc('day', lower(active_during)))
create unique index idx_unique_shift_end_time on entries (user_id, date_trunc('day', upper(active_during)))
These two indexes take the lower or upper bounds of the range (i.e. the start time or end time), then truncate to just the day (i.e. drop the hours, minutes, seconds etc) and then combine with the user_id to give us a unique key.

How to limit a date constraint to only allow a user to enter dates that fall on a specific day of the week in Oracle SQL

I've been stuck with this problem for a few days now and I've been through many different questions that i've found via google/stack overflow but I've been unable to solve it.
I need to create a table that allows a user to enter a date for an appointment, one of the constraints is that the date that is entered can only be a date that is a monday or a friday.
Here are several of my attempts, some worked in that they allow me to create the table, but then when it comes to entering data, it says invalid month, invalid datatype or a plethora of other errors. (I've also removed other columns from the below code just because they're not relevant to the question).
CREATE TABLE t_appointments
(appointment_id NUMBER(10,0) CONSTRAINT appointments_appoint_id_pk PRIMARY KEY,
appoint_date DATE CONSTRAINT appointments_app_date_nn NOT NULL
CONSTRAINT appointments_app_date_ck CHECK (to_char(appoint_date,'Day') IN ('Monday','Friday'));
I also tried
CREATE TABLE t_appointments
(appointment_id NUMBER(10,0) CONSTRAINT appointments_appoint_id_pk PRIMARY KEY,
appoint_date DATE CONSTRAINT appointments_app_date_nn NOT NULL
CONSTRAINT appointments_app_date_ck CHECK (to_char(to_date(appoint_date,'DD-MM-YYYY'),'Day') IN ('Monday','Friday'));
Any help would be greatly appreciated, thank you.
This is the issue in Oracle:
Name of day, padded with blanks to display width of the widest name of day in the date language used for this element.
So, try this:
CONSTRAINT appointments_app_date_ck CHECK (to_char(appoint_date, 'Day') IN ('Monday ', 'Friday '))
Or, you can do:
CONSTRAINT appointments_app_date_ck CHECK (to_char(appoint_date, 'D') IN ('2', '6'))

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'

Checking timestamps in a constraint in Oracle

I have created a simple table:
CREATE TABLE Messages
( msgID number(10) PRIMARY KEY,
sender_ID number(10),
time_sent TIMESTAMP,
);
Now I want to add a constraint to it that ensures that time sent will be after the year 2014. I wrote:
alter table Messages
add constraint year_check
check (time_sent > to_timestamp('2014-12-31 23:59:59'));
However I get the following error:
ORA-30075: TIME/TIMESTAMP WITH TIME ZONE literal must be specified in CHECK constraint
I don't want to have a TIME ZONE in my timestamp and have inserted values like this:
INSERT INTO Messages VALUES(1, 1, TIMESTAMP '2014-12-24 07:15:57');
How can I fix my constraint to make this error go away?
When you lookup the error message in the manual you will see the recommendation:
Action: Use time or timestamp with time zone literals only.
to_timestamp('2014-12-31 23:59:59') returns a timestamp (without a time zone), but Oracle requires a timezone with time zone in a check constraint (although I have to admit I don't understand why)
You can either use an ANSI timestamp literal which is resolved to a timestamp with time zone:
alter table Messages
add constraint year_check
check (time_sent > timestamp '2014-12-31 23:59:59');
or use to_timestamp_tz with an explicit time zone:
alter table Messages
add constraint year_check
check (time_sent > to_timestamp_tz('2014-12-31 23:59:59 +00:00', 'YYYY-MM-DD HH:MI:SS TZH:TZM'));
Btw: I would rather change the condition to use >= on the first of January:
alter table Messages
add constraint year_check
check (time_sent >= timestamp '2015-01-01 00:00:00');
Otherwise you could add a row with 2014-12-31 23:59:59.567
Try using a format mask:
select to_timestamp('2014-12-31 23:59:59') timest from dual
2 /
select to_timestamp('2014-12-31 23:59:59') timest from dual
*
ERROR at line 1:
ORA-01843: not a valid month
select to_timestamp('2014-12-31 23:59:59', 'YYYY-MM-DD HH24:MI:SS') timest from dual
2 /
TIMEST
---------------------------------------------------------------------------
31-DEC-14 11.59.59.000000000 PM

I need help writing a check constraint on a smalldatetime column

I need to add a Check constraint to a Column named Departure. This column has the smalldatetime data type.
The Check Constraint should state that:
The date and time entered in the Departure column must be at least 6 hours from whatever the current time is when the date is being entered.
Can anyone help with the code.
Thank you
This should do it:
ALTER TABLE [YourTableName]
ADD CONSTRAINT DepartureLaterThan6Hours CHECK ([Departure] > dateadd(HOUR, 6, GetDate()));