Oracle check constraint1 [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a column Hire_date in table x with data
01-jun-98
16-aug-99
02-feb-09
01-mar-06
-
01-dec-08
17-mar-99
16-feb-07
I was asked to create a check constraint on that Hire_date column. Check that the hire_date comes after 2000. I see an error
I tried again to check if the hire_date comes after 1996. The constraint was successfully created.
why did i see an error when i created a check constraint for hire_date>2000 ?
Code:
alter table x
add constraint check_hire_date check(hire_Date>'01-jan-2000')
got error
alter table x
add constraint check_hire_date check(hire_Date>'01-jan-1996')
successful

The reason you got error is obvious. Some of your data violate the constraint. When you add a constraint to a table, it will validate the all data to the constraint.
You can use novalidate option to skip the validation. If you apply novalidate option when you create the constraint, you can add the constraint while you don't validate the old(=current) date with the constraint. The constraint will affect to the data which is inserted after the creation of the constraint.
sql> create table x (hire_date date);
sql> insert into x values ('1998-01-01');
sql> insert into x values ('2000-01-05');
sql> select * from x;
HIRE_DAT
--------
98/01/01
00/01/05
sql> alter table x add constraint d_check check
2 (hire_date >= to_date('2000-01-01', 'yyyy-mm-dd'))
3 novalidate;
sql> insert into x values ('1999-01-01');
insert into x values ('1999-01-01')
*
error at line 1:
check constraint (d_check check) violated

Related

DEFAULT constraint not working in SQL Oracle? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to get a constraint (this is just test code) but when I add the data (complete the whole table) and run it with leaving out Apptime data in the values it doesn't default to hello in the table it's just '-'. I'm using Oracle Live SQL. Any clue as to how this is done? Would be good if I could do it in the schema and not a constraint but if it has to be a constraint outside that's okay.
Thank you and apologies if I did anything wrong in this question, I'm new haha.
DROP TABLE Bok;
CREATE TABLE Bok (
BokID number(3) NOT NULL PRIMARY KEY,
Appdate varchar2(4),
Apptime varchar2(5) DEFAULT 'hello'
);
In order to default to work the insert query has to use DEFAULT keyword or skip the column
INSERT INTO Bok(BokID, Appdate, appTime)
VALUES (1, 'a', DEFAULT);
INSERT INTO Bok(BokID, Appdate)
VALUES (1, 'a');
One more option when DEFAULT ON NULL is defined:
CREATE TABLE Bok (
BokID number(3) NOT NULL PRIMARY KEY,
Appdate varchar2(4),
Apptime varchar2(5) DEFAULT ON NULL 'hello'
);
INSERT INTO Bok(BokID, Appdate, appTime)
VALUES (1, 'a', NULL);

Is adding a column and a constraint at the same time while modifying an Oracle table possible?

I have an assignment asking me to add a column to a table and give it a constraint.
"Add a column named Base_Salary with a datatype of NUMBER(7,2) to the
store_reps table. Ensure that the amount entered is above 0."
I thought it was pretty straight forward, but maybe I have to ALTER the table twice, once for the new column, and again for adding the constraints? That seems redundant and inefficient.
I've tried taking off the second 'ADD' and moving around the parentheses, and moving the commas, it seems there is a similar error for everything I do.
This is my attempt:
ALTER TABLE store_reps;
ADD (Base_Salary NUMBER(7, 2)),
ADD CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0);
I get an error
ADD CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0)
ERROR report -
UNKNOWN COMMAND
and another:
Error starting at line : 74 in command -
ALTER TABLE store_reps
ADD (Base_Salary NUMBER(7, 2)),
CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0)
Error report -
Any help is greatly appreciated. I just started SQL a few weeks ago, and am very much noob.
Your query contains two syntax problems that need to be fixed:
get rid of the comma before CONSTRAINT
get rid of the extra pair of braces around Base_Salary NUMBER(7, 2)
Working example:
create table store_reps (id number);
ALTER TABLE store_reps
ADD Base_Salary NUMBER(7, 2)
CONSTRAINT store_reps_Base_Salary CHECK (Base_Salary>0);
create table tr (col1 number);
alter table tr add col2 varchar2(100) constraint tr_cons CHECK (col2 = 'a');
DB Fiddle demo
Cheers!!

I am trying to extract only date value from Oracle's SYSDATE but my query is giving me ORA-02290: check constraint violated [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I want to insert current date into my record, First I executed this query successfully.
insert into Member values (1, 'Richa Sharma', 'Pune', TO_DATE('10-Dec-05', 'DD-MM-YY'), 'Lifetime', '25000', 5, 50);
Then while executing the following query I'm getting the above error code.
insert into Member values (2, 'Garima Sen', 'Pune', SYSDATE, 'Annual', 100, 3, NULL);
EDIT: This is the query I used to create table.
create table Member (Member_Id number(5),
Member_Name varchar2(30),
Member_Address varchar2(50),
Acc_Open_Date date,
Membership_Type varchar2(20),
Fees_Paid number(6),
Max_Books_Allowed number(2),
Penalty_Amount number(7,2),
PRIMARY KEY(Member_Id),
CHECK (Membership_Type IN ('Lifetime',' Annual', 'Half Yearly',' Quarterly')));
Your check constraint has a leading space in ' Annual' change to 'Annual'

Oracle SQL ALTER TABLE error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have code like this:
ALTER TABLE "VMA_CSDD" ADD CONSTRAINT "VMA_CSDD_PK" PRIMARY KEY ("ID") ENABLE
ALTER TABLE "VMA_CSDD" MODIFY ("ID" NOT NULL ENABLE)
ALTER TABLE "VMA_CSDD" MODIFY ("CSDD_NAME" NOT NULL ENABLE)
It gives me an error:
ALTER TABLE "VMA_CSDD" MODIFY ("ID" NOT NULL ENABLE)
ORA-01735: invalid ALTER TABLE option
How do I fix this?
Option ENABLE is not correct with column definition you have to also add types
ALTER TABLE "VMA_CSDD" MODIFY ("ID" int NOT NULL )
ALTER TABLE "VMA_CSDD" MODIFY ("CSDD_NAME" varchar(100) NOT NULL )

SQL Error: ORA-00947: not enough values [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Learning Oracle SQL. I have created a table that looks like this:
CREATE TABLE Kocury
(imie VARCHAR2(15) NOT NULL,
plec VARCHAR2(1) CONSTRAINT allowedValues CHECK (plec in ('M', 'D')),
pseudo VARCHAR2(15) Constraint PK_KOCURY PRIMARY KEY,
funkcja VARCHAR2(10) CONSTRAINT fk_checkF REFERENCES Funkcje (funkcja),
szef VARCHAR2(15) CONSTRAINT fk_checkS references Kocury (pseudo),
w_stadku_od DATE default sysdate,
przydzial_myszy NUMBER(3),
myszy_extra NUMBER(3),
nr_bandy NUMBER(2) CONSTRAINT fk_checkN REFERENCES Bandy(nr_bandy)
);`
and then I tried to insert some data:
INSERT INTO Kocury(imie, plec, pseudo, funkcja, szef, w_stadku_od, przydzial_myszy ,myszy_extra, nr_bandy)
VALUES ('JACEK', 'M', 'PLACEK', 'LOWCZY', 'LYSY', '2008-12-01, 67',NULL , 2);
as far as I am concerned the data types all match. But in Oracle SQL Developer I get this:
Error starting at line : 41 in command -
INSERT INTO Kocury(imie, plec, pseudo, funkcja, szef, w_stadku_od, przydzial_myszy ,myszy_extra, nr_bandy)
VALUES ('JACEK', 'M', 'PLACEK', 'LOWCZY', 'LYSY', '2008-12-01, 67',NULL , 2)
Error at Command Line : 42 Column : 1
Error report -
SQL Error: ORA-00947: not enough values
00947. 00000 - "not enough values"
*Cause:
*Action:
I am not sure what's happening and how to get my data inserted. What could I be doing wrong? These topics didn't help me:
ORA-00947 : Not Enough Values
ORA-00947: not enough values
You're missing some apostrophes in between '2008-12-01, 67':
INSERT INTO Kocury(imie, plec, pseudo, funkcja, szef, w_stadku_od, przydzial_myszy ,myszy_extra, nr_bandy)
VALUES ('JACEK', 'M', 'PLACEK', 'LOWCZY', 'LYSY', '2008-12-01', 67, NULL , 2)