SQL Server 2014 issue. I created a table which has two DATE columns with DATE datatypes. One is populated the other is a future event. I created a constraint that date1 < date2. I inserted into the table putting NULL into date2 because it is future. I cannot update anything in the table. When trying to update date2 with date, I get Operand type clash: int is incompatible with date. I tried entering date with and without apostrophes in ever way I could think of. I cannot drop or alter the column because of the constraint I added. HELP. Please be specific about the SQL syntax needed for altering or updating to fix.
Change the constraint! It is never true -- either false or NULL. So, remove the constraint you have and do something like:
alter table t add constraint chk_t_date1_date2 check ( (date1 < date2) or date2 is null);
To drop the constraint, you do:
alter table t drop constraint <constraint name>;
You need to know the name of the constraint, which you can get from information_schema.table_constraints.
You should use single quotes '2019-04-18' with the ' before and after.
Related
Is it possible to add an autogenerated primary key column ( timestamp) to the existing table with alter table?
Something like this but it doesn't compile
ALTER TABLE DB2ADMIN.xxxyyyy ADD COLUMN ID TIMESTAMP NOT NULL WITH DEFAULT timestamp(generate_unique())#
Error during Prepare
42601(-104)[IBM][CLI Driver][DB2/AIX64] SQL0104N An unexpected token "timestamp" was found following "OT NULL WITH DEFAULT". Expected tokens may include: "CHECK". SQLSTATE=42601
It is unwise to use a fake (from generate_unique) timestamp datatype as a primary key because it makes setting values for pre-existing rows more awkward, and makes date arithmetic impossible.
The datatype TIMESTAMP is better suited for real dates/times because then you can use date arithmetic, which is practical for business. If the values in your fake timestamp-column are from generate-unique then you cannot sensibly use date arithmetic.
If you try to use a real timestamp value , (instead of generate_unique) , such as current timestamp then you are likely to get collisions, depending on the insert-rate. Usually that's a bad idea. Also this makes setting values for any pre-existing rows more difficult.
It is usually much easier and faster to use an autogenerated identity column as a surrogate primary key, especially if the table already has existing data.
Here is a typical way to do this which works with Db2-LUW and also on older versions of Db2. Other ways are possible with later versions of Db2.
Firs you need to verify that the table does not already have a primary key, as there can only be at most one of these per table.
Next, check if the table already has a unique index on a NOT NULL column, because if such a column exists then it can be promoted to be the primary key column.
If neither of the above exist, then you can use logic like this below to add an autgenerated column, set unique values in any existing rows, and ensure that any future inserts automatically get a unique value in the column without application intervention.
alter table myschema.mytab add column id bigint not null default 0 ;
alter table myschema.mytab alter column id drop default ;
alter table myschema.mytab alter column id set generated always as identity ;
update myschema.mytab set id = default ;
alter table myschema.mytab add constraint pkey primary key(id) ;
reorg table myschema.mytab ;
runstats on table myschema.mytab with distribution and detailed indexes all;
You can use CURRENT_TIMESTAMP instead of timestamp(generate_unique())
ALTER TABLE sellers ADD COLUMN ID TIMESTAMP NOT NULL WITH DEFAULT CURRENT_TIMESTAMP
You can test here
I have an appointments table that has (among others) a [personid] and an [appdate] column of datetime(2) datatype. I want to add a constraint that will prevent any personid from getting more than one appointment on any single day.
I tried this but it did not work
ALTER TABLE dbo.tblappointments
ADD CONSTRAINT SingleApp UNIQUE (PersonID, CAST(Appdate AS DATE))
Thank you.
I imagine you can create a computed column and use it as the index.
ALTER TABLE dbo.tblappointments ADD AppointmentDay as CAST(Appdate AS DATE) PERSISTED
If you don't use PERSISTED the value will be calculated every time the column is accessed
SQL Server: I am looking for a way to force at database level a date column to be last day of the month (note: not the last day of current month). Is there a way to alter the table to implement this constraint?
Thank you
You could implement the check constraint via eomonth() function to validate the date (i.e. end-of-month)
ALTER table t
ADD CONSTRAINT CHK_Date CHECK(<date> = eomonth(<date>))
You can, however, do any of the following:
Do your validation in a trigger. Should be after insert and update.
Create another table with all possible end of month dates and use a foreign key for that column.
Yogesh's answer is the correct one.
I created a constraint on the date column of the table X. When I created a check constraint on date as date > '01-jan-2000' there is an error message ("Check constraint violated"). When I tried again to check the date if it comes after 1996 (date > '01-jan-1996'), it worked well.
Is there a reason for it?
I used this code:
ALTER TABLE X
ADD CONSTRAINT DATE_CONST CHECK(DATE>'01-JAN-2000') -- ERROR
ALTER TABLE X
ADD CONSTRAINT DATE_CONST CHECK(DATE>'01-JAN-1996') -- WORKED
there are two things you are done wrong.
a) You can not assing name to column keyword like "DATE".
b) you must to convert date when you add CONSTRAINT like this :
ALTER TABLE test ADD CONSTRAINT DATE_CONST CHECK(testdate>to_date('01-JAN-2000', 'dd-mon-yyyy'));
http://sqlfiddle.com/#!4/98198/9 is example of this question.
Edit : If your table has record before specified date, Check constraint will be compiled but it will be invalid. You must be update before specied date records to newly date.
I have a table with 2 date columns, open_date and close_date. And when I insert a new row the close_date can be null. But in the future I will want to update the close_date, and I want to ensure that the close_date is bigger than the open_date?
Thanx
I'm using oracle...
You can add a check constraint to make sure Close_date is bigger than the Open_Date,
ALTER TABLE Table_Name
ADD CONSTRAINT ck_Con_Name CHECK (Close_date > Open_Date)
To make sure a value is provided for Close_Date column and its not left null, make the column NON-NULABLE.
For this you will need to make sure first there isnt any NULL Values in that column. UPDATE the column to some defualt value. Then Alter table definition something like this...
ALTER TABLE Table_Name
ALTER COLUMN Close_date DATETIME NOT NULL;
A check constraint like this will do ask you ask:
ALTER TABLE YourTable ADD CONSTRAINT CK_open_date_before_close_date
CHECK (open_date < close_date);
This will reject any transactions that update the table to a state that violates the condition. If, for example, you updated 5 rows but only one of them violated the constraint, the entire update will fail.
Additionally, I would recommend that instead of NULL for an open-ended close date, you use a sentinel value of 99991231. There are many reasons for this, not the least of which is performance of queries (so you can do simple inequality statements without needing an OR IS NULL clause). You can additionally then make the column NOT NULL and simplify the above check condition. This also affects front-end application code positively.
UPDATE
The check constraint does not require OR close_date IS NULL in it, since if close_date is NULL, the whole expression will be evaluated to NULL, and this will not violate the CHECK constraint.
You may be needing the CHECK Constraints.
PLease refer to the link