i want to apply check constraint with date for eg delivery date is greate than order date how to apply this constraint any can help me out
alter table sales_order modify dely_date check (dely_date>order_Date);
its show error
This is how to add CHECK constraint in an existing column :
alter table sales_order ADD CONSTRAINT CHK_dates check (dely_date>order_Date);
Related
I create a table
create table CARS{
CAR_ID NUMBER(10), CONSTRAINT X_CAR_ID NOT NULL
}
and now I want to change the name of the constraint, so I drop the constraint:
ALTER TABLE CARS DROP CONSTRAINT X_CAR_ID;
This works correclty but, when I tried to add new constraint I have a problem,
my query:
ALTER TABLE CARS ADD CONSTRAINT XX_CAR_ID (CAR_ID) NOT NULL;
I thought that query, will be working correctly, but I get only error report:
Error report -
SQL Error: ORA-00904:
How to add correctly this constraint ?
While I couldn't test it I believe the statement below is what you want:
ALTER TABLE CARS MODIFY CAR_ID CONSTRAINT XX_CAR_ID NOT NULL;
Oracle uses the modify keyword in this context.
To rename it without dropping first you would use:
alter table cars rename constraint x_car_id to xx_car_id;
See the reference for more info.
I'm trying this code but I'm getting this error message: An expression of non-boolean type specified in a context where a condition is expected, near ','.
ALTER TABLE customers
ADD active int
DEFAULT (1)
CONSTRAINT chk_active
CHECK (0,1);
Thanks in advance!
When defining a check constraint you have to actually refer to the column name, i.e. CHECK (Active IN (0,1) instead of just CHECK (0, 1). So your syntax would be
ALTER TABLE Customers ADD
active INT DEFAULT (1)
CONSTRAINT chk_active CHECK (Active IN (0,1));
It is also probably a good idea to name your default constraint, and include the table name in the constraint name so that you don't conflict with similar constraints on other tables:
ALTER TABLE Customers ADD
Active INT CONSTRAINT DF_Customers__Active DEFAULT (1)
CONSTRAINT CHK_Customers__Active CHECK (Active IN (0,1));
However, it would seem more appropriate to have a not null bit column so the check constraint is not required:
ALTER TABLE Customers ADD Active BIT NOT NULL CONSTRAINT DF_Customers__Active DEFAULT(1);
ALTER TABLE customers
ADD active int
GO
ALTER TABLE customers
ADD CONSTRAINT [DF_active ] DEFAULT (1) FOR active
GO
ALTER TABLE customers
ADD CONSTRAINT [chk_active] CHECK (active>=0 AND active <=1)
I would suggest you do it like this. Also always conside adding a name to your constraint.
As a facut, why don't u use active as bit column so you won't need the CHECK constraint.
try this
ALTER TABLE Customers ADD
active INT DEFAULT (1)
CONSTRAINT chk_active CHECK (Active IN (0,1));
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.
Trying to alter a table in SQL Server. I want to add a unique constraint to a column called Names in table ReportingItemNames:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([ReportingItemNames,Name])
But I am getting this error:
Column name 'ReportingItemNames,Name' does not exist in the target table or view
Where am I going wrong?
Use this:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames UNIQUE ([Name])
You can refer to ALTER TABLE (Transact-SQL) documentation for more information.
Shouldn't it be:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([Name])
In my SSIS/DW project, I have a DIM.DATE dimension which is linked to my FACT table by a surrogate key as follows:
ALTER TABLE FACT.SALES ADD date_id INT NOT NULL
ALTER TABLE FACT.SALES WITH CHECK ADD CONSTRAINT FK_dim_date FOREIGN KEY (date_id) REFERENCES DIM.DATE(date_id)
This creates a "date_id" in my fact table, now during my SSIS import process I have a date column being passed (shipped_date), I use this to look up the DIM.DATE table and pass in the surrogate key in my dimension.
This works great, but now I need to have a few different date dimensions for invoice date, received date, etc.
I am confused as to how to make use of the existing DIM.DATE to do this?
I could then add more columns into my fact table..
-- add column into fact table
ALTER TABLE FACT.SALES ADD shipped_date_id INT NOT NULL
ALTER TABLE FACT.SALES ADD invoice_date_id INT NOT NULL
-- add foreign key
ALTER TABLE FACT.SALES WITH CHECK ADD CONSTRAINT FK_shipped_date FOREIGN KEY (shipped_date_id) REFERENCES DIM.DATE(date_id)
ALTER TABLE FACT.SALES WITH CHECK ADD CONSTRAINT FK_invoice_date FOREIGN KEY (invoice_date_id) REFERENCES DIM.DATE(date_id)
But when I do my lookup, I can only pass in the "date_id" column.. I am confused how to make this work all together.
Anyone able to clear this up for me?
You have to use multiple LookUp transforms... 1 for each DateKey field in the fact table.