How do I add a not null column and a check constraint in one line in Oracle 11g? - oracle

I'm trying to add a new INTEGER column to a table. The column must be NOT NULL, with a default value of 1, and only accept values greater than zero.
I'm trying to do this at the moment:
ALTER TABLE FOO_AUTHORS
ADD PUBLICATION_PERIOD_DAYS INTEGER DEFAULT(1) NOT NULL
CONSTRAINT publicationPeriodDays
CHECK (PUBLICATION_PERIOD_DAYS>0);
Is there a way to do this in one line? I'm following this example, but it's not working because of the NOT NULL. Is the NOT NULL then necessary?
I'm getting the following error from the DB:
QL Error: ORA-02293: cannot validate (DBOWNER.PUBLICATIONPERIODDAYS) - check constraint violated
02293. 00000 - "cannot validate (%s.%s) - check constraint violated"
*Cause: an alter table operation tried to validate a check constraint to
populated table that had nocomplying values.
If I try it without the NOT NULL, it works fine.

Roll the NOT NULL constraint into the CHECK constraint:
ALTER TABLE FOO_AUTHORS
ADD PUBLICATION_PERIOD_DAYS INTEGER DEFAULT 1
CONSTRAINT publicationPeriodDays
CHECK ( PUBLICATION_PERIOD_DAYS IS NOT NULL AND PUBLICATION_PERIOD_DAYS > 0 );
The existing rows will have their PUBLICATION_PERIOD_DAYS set to the default value.

Related

How can i drop a check constraint without deleting and recreating the table in sqlplus

I have this table and i added a check to the column duree
with this condition alter table operation modify Duree integer check(Duree>=2);
but in the homework it's specified that it needs to be like (duree>=44) isntead
how this be fixed.
create table Operation (
CodeOP varchar(20) not null ,
Duree integer ,
Chef integer ,
DateDeb Date ,
Budget float ,
CodeS varchar (20),
constraint pk_operation primary key (CodeOP),
constraint fk_operation foreign key(CodeS)REFERENCES Service(CodeS) on delete set null
i tried alter table operation modify Duree integer check(Duree>=44);
but i'm getting this message :
ERROR at line 1: ORA-02293: cannot validate (TPBDD2.SYS_C008292) - check constraint violated

Create a table in oracle returning a constraint name conflict

I am trying to create a table in Oracle I am using the following SQL:
CREATE TABLE SALES_TARGET(
SLS_REP_SK NUMBER(38, 0) NOT NULL,
MARKET_PRODUCT_HRCHY_SK NUMBER(38, 0) NOT NULL,
FISCAL_PERIOD VARCHAR2(6) NOT NULL
CONSTRAINT VALID_FISCAL_PERIOD CHECK (LENGTH(FISCAL_PERIOD) = 6 AND REGEXP_LIKE(FISCAL_PERIOD, '^\d*$') AND (SUBSTR(FISCAL_PERIOD, 1, 4) BETWEEN '2010' AND '2050') AND (TO_NUMBER(SUBSTR(FISCAL_PERIOD, 5, 2)) BETWEEN 1 and 12)),
CURR_SK NUMBER(38, 0) NOT NULL,
SALES_TARGET_AMT NUMBER(18, 5) NOT NULL
CONSTRAINT POSITIVE_SALES_TARGET CHECK (SALES_TARGET_AMT >= 0),
ETL_BATCH_ID NUMBER(38, 0) NOT NULL,
ETL_CREATED_LOAD_DT DATE NOT NULL,
ETL_MODIFIED_LOAD_DT DATE NOT NULL,
CONSTRAINT PK_SALES_TARGET PRIMARY KEY (SLS_REP_SK, MARKET_PRODUCT_HRCHY_SK, FISCAL_PERIOD)
USING INDEX
TABLESPACE DWH_NRM_INDEX
)
TABLESPACE DWH_NRM_DATA
;
It is returning the following error due to a constraint name conflict
Error report -
SQL Error: ORA-02264: name already used by an existing constraint
02264. 00000 - "name already used by an existing constraint"
*Cause: The specified constraint name has to be unique.
*Action: Specify a unique constraint name for the constraint.
As other questions have suggested use a query such as:
select * from ALL_constraints where constraint_name = 'PK_SALES_TARGET';
This is returning no results ( I have tried the above with other constraint names and they are returned no problem).
Can anyone help me find my missing constraint?
YOu spesify 3 constraints
CONSTRAINT VALID_FISCAL_PERIOD
CONSTRAINT POSITIVE_SALES_TARGET
CONSTRAINT PK_SALES_TARGET
Please check are any of them already exists?

Oracle SQL Check Error

Why will my command not work when i use the check constraint? The table can be added when the check is not included.
create table Car (
CarID number(32,0) NOT NULL ,
PurchaseDate date,
Colour varchar2(10) NOT NULL CHECK (Colour IN ("Red", "Blue", "Green")),
CONSTRAINT CAR_PK PRIMARY KEY (CarID),
FOREIGN KEY (CarID) REFERENCES Vehicle(ID)
);
Error report -
SQL Error: ORA-02438: Column check constraint cannot reference other columns
02438. 00000 - "Column check constraint cannot reference other columns"
*Cause: attempted to define a column check constraint that references
another column.
*Action: define it as a table check constriant.
create table Car (
CarID number(32,0) NOT NULL ,
PurchaseDate date,
Colour varchar2(10) NOT NULL CHECK (Colour IN ('Red', 'Blue', 'Green')),
CONSTRAINT CAR_PK PRIMARY KEY (CarID),
FOREIGN KEY (CarID) REFERENCES Vehicle(ID)
);
No double quotes are allowed in oracle SQL

How to add composite primary keys?

I have a table with three columns, [Id,QTY,Date]. out of these three, two columns [id and date], should be set as primary keys, because I need to fetch the record one by one, from this table, into a reference.
the data to be inserted into this table is
101,10,NULL
101,20,201220
101,7,201440
102,5,null
102,8,201352
date is in yyyyww format
How do I define two columns as composite primary keys when they have null values, duplicates?
alter table abc add constraint pk primary key (ID, DATE);
if I try to alter the table the error appears
Error report:
SQL Error: ORA-01449: column contains NULL values; cannot alter to NOT NULL
01449. 00000 - "column contains NULL values; cannot alter to NOT NULL"
*Cause:
*Action:
Using table level constraint, you can use this query
alter table your_table add constraint pkc_Name primary key (column1, column2)
but first you need to declare the columns NOT NULL. All parts of a primary key need to be NOT NULL.
The column name of your table is ID and it is still null and non-unique, how is it possible. If it is primary key of other table try adding a surrogate key column for this table and make it primary key.
In case of composite primary key, it should have atleast one not null value(For each row) in the combination of columns. And the combination of column must be unique at all case.
For further details check, http://docs.oracle.com/cd/B10500_01/server.920/a96524/c22integ.htm
Correction - If composite primary key is made up of 3 columns, then no column (among 3) can hold NULL value. And the combination of those 3 columns must be unique.
E.g. (1,2,2)
(1,2,1)
(2,2,1)
(1,2,2) - not valid

date Not null , error ora_01758

why I am getting this error ?
In the table DDL I only have 2 columns , id (number) and name (varchar)
ALTER TABLE mytable ADD SUSPEND date NOT NULL
ORA-01758: table must be empty to add mandatory (NOT NULL) column
ORA-06512: at line 7
ORA-01758: table must be empty to add mandatory (NOT NULL) column ORA-06512: at line 7
And is your table empty? I think not.
There's probably a way around this involving adding the column as nullable, then populating every row with a non-NULL value, the altering the column to be not null.
Alternatively, since the problem is that these current rows will be given NULL as a default value, and the column is not allowed to be NULL, you can also get around it with a default value. From the Oracle docs:
However, a column with a NOT NULL constraint can be added to an existing table if you give a default value; otherwise, an exception is thrown when the ALTER TABLE statement is executed.
Here is a fiddle, how you could do it
Would a date in the future be acceptable as a temporary default? If so, this would work:
ALTER TABLE MYTABLE ADD (SUSPEND_DATE DATE DEFAULT(TO_DATE('21000101', 'YYYYMMDD'))
CONSTRAINT SUSPEND_DATE_NOT_NULL NOT NULL);
If table already contain the records then table won't allowes to add "Not null" column.
If you need same then set default value for the column or truncate the table then try.

Resources