Create a table in oracle returning a constraint name conflict - oracle

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?

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

having error for missing left parenthesis?

CREATE TABLE Transaction
(Portfolio_Number NUMBER(7,0) CONSTRAINT NN_Portfolio_Number NOT NULL,
CONSTRAINT FK_Portfolio_Number FOREIGN KEY
(Portfolio_Number) REFERENCES Portfolio(Portfolio_Number),
CONSTRAINT PK_Portfolio_Number PRIMARY KEY,
Stock_Code VARCHAR2(50) CONSTRAINT NN_Stock_Code NOT NULL,
CONSTRAINT FK_Stock_Code FOREIGN KEY
(Stock_Code) REFERENCES Stock(Stock_Code),
CONSTRAINT PK_Stock_Code PRIMARY KEY,
Transaction_Date DATE DEFAULT SYSDATE
CONSTRAINT PK_Transaction_Date PRIMARY KEY
CONSTRAINT NN_Transaction_Date NOT NULL,
Exchange_Code VARCHAR2(4) CONSTRAINT NN_Exchange_Code NOT NULL,
CONSTRAINT FK_Exchange_Code FOREIGN KEY
(Exchange_Code) REFERENCES Exchange(Exchange_Code),
Broker_Number NUMBER(7,0) CONSTRAINT NN_Broker_Number NOT NULL,
CONSTRAINT FK_Broker_Number FOREIGN KEY
(Broker_Number) REFERENCES Broker(Broker_Number),
Buy_Sell CHAR(1) CONSTRAINT NN_Buy_Sell NOT NULL,
Quantity NUMBER(7,0) CONSTRAINT CK_Quantity CHECK (Quantity > 0)
CONSTRAINT NN_Quantity NOT NULL,
Price_Per_Share NUMBER(6,2) CONSTRAINT NN_Price_Per_Share NOT NULL);
It seems that you got lost in too many CONSTRAINTs scattered throughout that piece of code.
a table can have only one primary key constraint; you have 3 of them. If it is a composite primary key, all columns should be placed into the same CONSTRAINT clause (see my example)
you can't mix constraints with columns; if you use them, put all those outline constraints to the end of the script (like I did)
I can't run it as I miss all tables you're referencing, but - this code is formatted properly so I hope it should compile.
CREATE TABLE Transaction
(
Portfolio_Number NUMBER (7, 0) CONSTRAINT NN_Portfolio_Number NOT NULL,
Stock_Code NUMBER (7, 0) CONSTRAINT NN_Stock_Code NOT NULL,
Transaction_Date DATE
DEFAULT SYSDATE
CONSTRAINT NN_Transaction_Date NOT NULL,
Exchange_Code VARCHAR2 (4) CONSTRAINT NN_Exchange_Code NOT NULL,
Broker_Number NUMBER (7, 0) CONSTRAINT NN_Broker_Number NOT NULL,
Buy_Sell CHAR (1) CONSTRAINT NN_Buy_Sell NOT NULL,
Quantity NUMBER (7, 0)
CONSTRAINT CK_Quantity CHECK (Quantity > 0)
CONSTRAINT NN_Quantity NOT NULL,
Price_Per_Share NUMBER (6, 2) CONSTRAINT NN_Price_Per_Share NOT NULL,
--
CONSTRAINT PK_Transaction_Date PRIMARY KEY
(portfolio_number, transaction_date, stock_code),
CONSTRAINT FK_Portfolio_Number FOREIGN KEY
(Portfolio_Number)
REFERENCES Portfolio (Portfolio_Number),
CONSTRAINT FK_Stock_Code FOREIGN KEY
(Stock_Code)
REFERENCES Stock (Stock_Code),
CONSTRAINT FK_Exchange_Code FOREIGN KEY
(Exchange_Code)
REFERENCES Exchange (Exchange_Code),
CONSTRAINT FK_Broker_Number FOREIGN KEY
(Broker_Number)
REFERENCES Broker (Broker_Number)
);
You are trying to create a primary key without specifying column name
, CONSTRAINT PK_Portfolio_Number PRIMARY KEY,
which is wrong. The right would be something like that
, CONSTRAINT PK_Portfolio_Number PRIMARY KEY(Portfolio_Number),
Next thing to note is you're trying to create 2 primary keys for same table which is impossible. If you need a primary key to be for both columns, you need to specify them in the same statement
, CONSTRAINT PK_Portfolio_Number PRIMARY KEY(Portfolio_Number, Stock_Code),

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

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.

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 create a check constraint between two columns in SQL?

I am trying to create a Basic pay (BP) table with
CREATE TABLE bp (
bpid VARCHAR(5),
FOREIGN KEY (bpid) REFERENCES designation(desigid),
upperlimit DECIMAL(10,2) NOT NULL,
lowerlimit DECIMAL(10,2) NOT NULL,
increment DECIMAL(10,2) NOT NULL
CONSTRAINT llvalid CHECK (upperlimit > lowerlimit)
);
As you can see near the ending, I want to check if upperlimit is greater than lowerlimit, how can I do that?
It might (probably does) depend on the data base you use.
Comparing to the oracle syntax (e.g. here: http://www.techonthenet.com/oracle/check.php), what you are missing might be a ',' between NULL and CONSTRAINT
The problem is that you have defined it as a column level constraint but it references other columns. You must define a constraint at the table level.
ALTER TABLE bp
ADD CONSTRAINT CK_limit CHECK ( upperlimit > lowerlimit)
Here's proper the SQL query...
CREATE TABLE bp (bpid VARCHAR(5),
FOREIGN KEY (bpid) REFERENCES designation(desigid),
upperlimit DECIMAL(10,2) NOT NULL,
lowerlimit DECIMAL(10,2) NOT NULL,
increment DECIMAL(10,2) NOT NULL,
CONSTRAINT llvalid CHECK (upperlimit > lowerlimit));
Note the comma after NOT NULL and CONSTRAINT in the last line.

Resources