ORA-02264: name already used by an existing constraint how to get this solved? - oracle

CREATE TABLE report(
report_id NUMBER(5),
description VARCHAR2(200) NOT NULL,
status VARCHAR2(200) NOT NULL,
bicycle_id NUMBER(5),
cust_id NUMBER(5),
staff_id NUMBER(5),
CONSTRAINT ad_reportid_pk PRIMARY KEY (report_id),
CONSTRAINT ad_bicycle_fk FOREIGN KEY (bicycle_id) REFERENCES bicycle(bicycle_id),
CONSTRAINT ad_custid_fk FOREIGN KEY (cust_id) REFERENCES customer(cust_id),
CONSTRAINT ad_staffid_fk FOREIGN KEY (staff_id) REFERENCES staff(staff_id)
);
Above is the SQL script when I'm creating a table and it thrown me error with ORA-02264. I am neither drop the table nor creating that table. Tried to run this script
SELECT * FROM user_constraints WHERE CONSTRAINT_NAME = 'ad_bicycleid_fk'
and it showed no data.

All object names are stored in uppercase unless you mention it in double quotes while creating an object.
Try this:
SELECT * FROM user_constraints
WHERE upper(CONSTRAINT_NAME) = upper('ad_bicycleid_fk');

Here's an example which shows one option you might do that.
First, "dummy" tables which are being referenced by columns in the report table (why do I need them? Because create table report would fail otherwise):
SQL> create table bicycle
2 (bicycle_id number(5) constraint pk_bic primary key);
Table created.
SQL> create table customer
2 (cust_id number(5) constraint pk_cus primary key);
Table created.
SQL> create table staff
2 (staff_id number(5) constraint pk_sta primary key);
Table created.
The report table:
SQL> create table report
2 (report_id number(5),
3 description varchar2(200) not null,
4 status varchar2(200) not null,
5 bicycle_id number(5),
6 cust_id number(5),
7 staff_id number(5),
8 --
9 constraint pk_rep primary key (report_id),
10 constraint fk_rep_bic foreign key (bicycle_id)
11 references bicycle (bicycle_id),
12 constraint fk_rep_cus foreign key (cust_id)
13 references customer (cust_id),
14 constraint fk_rep_sta foreign key (staff_id)
15 references staff (staff_id)
16 );
Table created.
SQL>
Everything is OK.
Note the way I named constraints, especially foreign keys whose names show - at least approximately - which table references which another table, e.g. FK_REP_BIC - it is a Foreign Key from table REPort and it references the BICycle table. I'm not saying that you must do it that way, but - Google a little bit, read about good practices and find the one you prefer the most.
As of the constraint you couldn't find:
SQL> select owner, constraint_type, table_name
2 from user_constraints
3 where constraint_name = 'FK_REP_BIC';
OWNER C TABLE_NAME
--------------- - ---------------
SCOTT R REPORT
SQL>

Related

I have a problem with an UPDATE BEFORE TRIGGER

I have the following tables:
CREATE TABLE Categorie
(categ_id NUMBER(5,0),
tip VARCHAR2(20));
CREATE TABLE Reteta
(reteta_id NUMBER(3),
nume VARCHAR2(100),
descriere VARCHAR2(200),
categ_id NUMBER(3,0),
vegetariana VARCHAR2(1) CHECK (vegetariana IN ('D','N')),
timp_preparare NUMBER(20,1),
portii NUMBER(3,1));
CREATE TABLE Set_ingrediente
(reteta_id NUMBER(3,0),
ingred_id NUMBER(3,0),
cantitate NUMBER(5,2),
um VARCHAR2(10) NOT NULL CHECK (um IN ('gr','ml','buc','lingurita','cana')),
comentarii VARCHAR2(100)
);
CREATE TABLE Ingredient
(ingred_id NUMBER(3,0),
ingredient VARCHAR2(30));
And the following primary/foreign keys:
ALTER TABLE Categorie
MODIFY (categ_id CONSTRAINT categ_id_pk PRIMARY KEY NOT NULL);
ALTER TABLE Reteta
MODIFY(CONSTRAINT reteta_id_pk PRIMARY KEY (reteta_id));
ALTER TABLE Ingredient
MODIFY(CONSTRAINT ingred_id_pk PRIMARY KEY (ingred_id));
ALTER TABLE Set_ingrediente
MODIFY( CONSTRAINT reteta_id_fk FOREIGN KEY (reteta_id) REFERENCES Reteta(reteta_id) ON DELETE CASCADE);
ALTER TABLE Set_ingrediente
MODIFY( CONSTRAINT ingred_id_fk FOREIGN KEY (ingred_id) REFERENCES Ingredient(ingred_id) ON DELETE CASCADE);
I have to write a trigger which :
Ensure that the vegetarian column cannot be changed if the recipe uses the ingredient 'oil'.
This is the trigger which I tried and it's not working:
CREATE OR REPLACE TRIGGER tr_oil BEFORE UPDATE
ON Reteta
FOR EACH ROW BEGIN
DECLARE
ingredient_var ingredient.ingredient%type;
SELECT ingredient
INTO ingredient_var
FROM Ingredint
WHERE ingred_id=
(SELECT ingred_id
FROM Set_ingrediente
WHERE reteta_id=NEW.reteta_id);
IF ingredient_var='oil' THEN
SET NEW.vegetariana = OLD.vegetariana;
END IF;
END;
That's so strange; up to trigger code, everything you posted worked perfectly so I just can't imagine what went wrong when it came to the trigger which is full of errors (misplaced begin, invalid table name, missing colon with the :new pseudorecord, ...). As if two different people wrote two pieces of code you posted.
Anyway:
SQL> CREATE OR REPLACE TRIGGER tr_oil
2 BEFORE UPDATE ON Reteta
3 FOR EACH ROW
4 DECLARE
5 ingredient_var ingredient.ingredient%type;
6 BEGIN
7 SELECT ingredient
8 INTO ingredient_var
9 FROM Ingredient
10 WHERE ingred_id = (SELECT ingred_id
11 FROM Set_ingrediente
12 WHERE reteta_id = :NEW.reteta_id);
13
14 IF ingredient_var='oil' THEN
15 :NEW.vegetariana := :OLD.vegetariana;
16 END IF;
17 END;
18 /
Trigger created.
SQL>

add composite/ foreign key in sqlplus

I have a very basic question regarding the Keys in SQL. I am trying to write an SQL statement so I can add the foreign key to my tables, however, it would always "table not found or missing". And the reason why I can't create that table is that the next table also has foreign keys from another table that I need to reference. Is there a way around it?
create table table_name (id char (3) primary key, name varchar (8));
SQL > table created.
create table table_name_2 (table2ID char (3) primary key, CID char (3),
name varchar (8), title varchar (8), foreign key (CID) references table_name_3);
SQL > missing table (table_name_3);
create table table_name_3 (CID char (3) primary key, tTitle varchar (8),
foreign key (phone) references table_name_4);
So I was only able to create table 3. What do I need to do so that I can create the table and add the foreign keys referencing while I write the SQL statements?
I am not ALLOWED to use ALTER table.
Create foreign keys separately, using the ALTER TABLE statement. Like this:
SQL> CREATE TABLE table_name(
2 id CHAR(3)PRIMARY KEY,
3 name VARCHAR(8)
4 );
Table created.
SQL> CREATE TABLE table_name_2(
2 table2id CHAR(3)PRIMARY KEY,
3 cid CHAR(3),
4 name VARCHAR(8),
5 title VARCHAR(8)
6 );
Table created.
SQL> CREATE TABLE table_name_3(
2 cid CHAR(3)PRIMARY KEY,
3 ttitle VARCHAR(8)
4 );
Table created.
Now the foreign key:
SQL> ALTER TABLE table_name_2 ADD CONSTRAINT fk_2_3 FOREIGN KEY(cid)
2 REFERENCES table_name_3;
Table altered.
SQL>
one approach you can od is to create the table with the Primary key and then do
and add the foreign key.
ALTER TABLE child_table
ADD CONSTRAINT fk_name
FOREIGN KEY (col1) REFERENCES parent_table(col1);

How to assign two foreign keys in child table ORACLE SQL?

How can I inherit from two(or more) parent classes in ORACLE SQL? I've tried something like this:
ID(13),
OTHER_ID(9),
CONSTRAINT FK_ID FOREIGN
KEY(ID) REFERENCES TABLE_ONE(ID),
CONSTRAINT FK_OTHER_GROUP FOREIGN
KEY(OTHER_ID) REFERENCES TABLE_TWO(OTHER_ID)
I've read the documentation and the code I've found is this one:
INDEX (product_category, product_id),
INDEX (customer_id),
FOREIGN KEY (product_category, product_id)
REFERENCES product(category, id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (customer_id)
REFERENCES customer(id)
It doesn't quite work for me. Is there any special way to define these indexes? I haven't met them before. The first example had a problem that data types weren't specified. But I'd like someone to explain me how could I do it the second(oracle documentation) way.
This is how I understood the problem:
there are two tables, each having primary key constraint
in my example, those are t_emp and t_dept
there's also the third table whose columns are supposed to reference primary keys from previous two tables
those are fk_th_emp and fk_th_dep foreign key constraints
If that's so, here's how to do that:
SQL> create table t_emp
2 (empno number constraint pk_temp primary key,
3 ename varchar2(20)
4 );
Table created.
SQL> create table t_dept
2 (deptno number constraint pk_dept primary key,
3 dname varchar2(20)
4 );
Table created.
SQL> create table third
2 (id number constraint pk_third primary key,
3 other_id number,
4 --
5 constraint fk_th_emp foreign key (id) references t_emp (empno),
6 constraint fk_th_dep foreign key (other_id) references t_dept (deptno)
7 );
Table created.
SQL>

Alter Table , adding a Foreign key constraint at a column ORA-02253

Hello there i am studying for the Oracle Certification of SQL Associate .
And trying to do some Examples .
I have an issue where i cannot find easily a reference on this .
create table employees
(employee_id number NOT NULL,
first_name varchar(20),
last_name varchar(30),
constraint employee_pk primary key (employee_id));
create table employee_notes
(employee_notes_id number,
employee_notes varchar(500),
constraint pk_employee_notes primary key (employee_notes_id));
create sequence employee_notes_seq start with 1 increment by 1
Now i want to add a new column at employee_notes table with a foreign key constraint .
I can't find out in syntax where is the problem .
****alter table employee_notes
add employee_id number
constraint fk_employee_notes foreign key (employee_id) references employees (employee_id);****
i get this error
ORA-02253: constraint specification not allowed her
I also tried to alter the table and add column and then the constraint but cannot
alter table employee_notes
add employee_id number;
--
alter table employee notes
add constraint fk_employee_notes foreign key (employee_id) references employees (employee_id);
ORA-02253: constraint specification not allowed here
I would like to know how i can do this
and why this syntax is wrong :)
You did something wrong because - it works OK:
SQL> CREATE TABLE employees
2 (
3 employee_id NUMBER NOT NULL,
4 first_name VARCHAR (20),
5 last_name VARCHAR (30),
6 CONSTRAINT employee_pk PRIMARY KEY (employee_id)
7 );
Table created.
SQL>
SQL> CREATE TABLE employee_notes
2 (
3 employee_notes_id NUMBER,
4 employee_notes VARCHAR (500),
5 CONSTRAINT pk_employee_notes PRIMARY KEY (employee_notes_id)
6 );
Table created.
SQL> ALTER TABLE employee_notes ADD employee_id NUMBER;
Table altered.
SQL> ALTER TABLE employee_notes ADD CONSTRAINT fk_employee_notes
2 FOREIGN KEY (employee_id)
3 REFERENCES employees (employee_id);
Table altered.
SQL>
When you use ALTER TABLE ... ADD in order to add a column and a constraint in one statement, do the following:
-- notice the () and the comma!
alter table employee_notes
add (
employee_id number
, constraint fk_employee_notes
foreign key (employee_id) references employees (employee_id)
) ;
That should do the trick. See dbfiddle. The syntax is similar to CREATE TABLE, where you'd also write all column names, data types etc in (), separated by commas.

SQL*Plus ORA-00904 invalid identifier & ORA-00905 missing keyword (foreign key)

I've tried to debug this code to the best of my ability to eliminate the possibility of little mistakes being the reason these errors are occurring but I keep getting two different errors on three create table statements.
The CREATE TABLE SECTION statement gives an invalid identifier error on line 7 pointing to course#, here's my code:
CREATE TABLE SECTION
(SECTION# VARCHAR2(8) constraint pk_section# primary key,
TIME CHAR(5),
MAXST NUMBER(2),
ROOM VARCHAR2(14),
constraint chk_maxst check(maxst<=35),
constraint fk_crs foreign key(course#)
REFERENCES course(course#),
constraint fk_pro foreign key(empid)
REFERENCES professor(empid));
The CREATE TABLE TAKES statement gives an invalid identifier error on line 4 pointing to section#, here's my code:
CREATE TABLE TAKES
(GRADE CHAR(5) constraint nn_grade not null,
constraint chk_grade check(grade IN ('A','B','C')),
constraint fk_sec foreign key(section#)
REFERENCES section (section#),
constraint fk_stu foreign key(sid)
REFERENCES student(sid));
Full Context:
drop table professor cascade constraints;
drop table course cascade constraints;
drop table student cascade constraints;
drop table section cascade constraints;
drop table takes cascade constraints;
CREATE TABLE PROFESSOR
(NAME CHAR(15) constraint nn_name not null,
EMPID VARCHAR2(8) constraint pk_empid primary key,
PHONE NUMBER(10),
DATEHIRED DATE,
SALARY NUMBER);
CREATE TABLE COURSE
(NAME CHAR(24) constraint nn_names not null,
COURSE# CHAR(10) constraint pk_course# primary key,
CREDIT CHAR(6) constraint nn_credit not null,
COLLEGE CHAR(20),
HRS NUMBER(1),
constraint chk_credit check(credit IN('U','G')),
constraint chk_college check(college IN ('Arts and Sciences','Education','Engineering','Business')),
constraint chk_course check((credit='U' AND hrs<=4) OR (credit = 'G' AND hrs=3)),
constraint unq_course unique(name, college));
CREATE TABLE STUDENT
(SID VARCHAR2(7) constraint pk_sid primary key,
NAME CHAR(14),
ADDRESS CHAR(22),
BIRTHDATE DATE,
GRADELEVEL CHAR(2) constraint nn_glvl not null);
CREATE TABLE SECTION
(SECTION# VARCHAR2(8) constraint pk_section# primary key,
TIME CHAR(5),
MAXST NUMBER(2),
ROOM VARCHAR2(14),
constraint chk_maxst check(maxst<=35),
constraint fk_crs foreign key(course#)
REFERENCES course(course#),
constraint fk_pro foreign key(empid)
REFERENCES professor(empid));
CREATE TABLE TAKES
(GRADE CHAR(5) constraint nn_grade not null,
constraint chk_grade check(grade IN ('A','B','C')),
constraint fk_sec foreign key(section#)
REFERENCES section (section#),
constraint fk_stu foreign key(sid)
REFERENCES student(sid));
Textbook references:
[https://drive.google.com/open?id=1eDdBShzgnSjISqxByJ7FKgbkLCEwXzpd][1]
[https://drive.google.com/open?id=1WhDsgQy2xSwjxVMqDzaGOcBh7zSokneT][2]
[https://drive.google.com/open?id=12N51OCEucRn_unagqHYsqufEGK3tKJH_][3]
Did you follow documentation that describes how you are supposed to do what you are doing? Because, it seems that you didn't pay much attention at classes, nor read documentation and tend to make up things, doing something that is either wrong or doesn't ever exist.
Consider deleting all that mess and starting over.
Here are some guidelines; try to fix those mistakes, come back if it still doesn't work.
In the COURSE table:
don't use CHAR but VARCHAR2 data type
you can't create constraints on non-existent columns (for example, a check constraint on CRS_CREDIT column, while you named the column as CREDIT)
In the SECTION table:
don't use CHAR data type
you can't use columns (in FOREIGN KEY constraints) that don't exist in the SECTION table (such as SEC_CRS_COURSE#), nor in the table you reference (such as CRS_COURSE# in the COURSE table)
there are no ON UPDATE CASCADE nor ON DELETE RESTRICT in Oracle
The same goes for the TAKES table.
[EDIT, after you almost made it work]
Congratulations! You're now so close! SECTION and TAKES table need some adjustment (missing columns - have a look, I marked them with a comment) and then tables are successfully created.
Once again (as you won't listen): get rid of CHAR data type columns - use VARCHAR2 instead.
SQL> CREATE TABLE PROFESSOR
2 (
3 NAME CHAR (15) CONSTRAINT nn_name NOT NULL,
4 EMPID VARCHAR2 (8) CONSTRAINT pk_empid PRIMARY KEY,
5 PHONE NUMBER (10),
6 DATEHIRED DATE,
7 SALARY NUMBER
8 );
Table created.
SQL>
SQL> CREATE TABLE COURSE
2 (
3 NAME CHAR (24) CONSTRAINT nn_names NOT NULL,
4 COURSE# CHAR (10) CONSTRAINT pk_course# PRIMARY KEY,
5 CREDIT CHAR (6) CONSTRAINT nn_credit NOT NULL,
6 COLLEGE CHAR (20),
7 HRS NUMBER (1),
8 CONSTRAINT chk_credit CHECK (credit IN ('U', 'G')),
9 CONSTRAINT chk_college CHECK
10 (college IN ('Arts and Sciences',
11 'Education',
12 'Engineering',
13 'Business')),
14 CONSTRAINT chk_course CHECK
15 ( (credit = 'U' AND hrs <= 4) OR (credit = 'G' AND hrs = 3)),
16 CONSTRAINT unq_course UNIQUE (name, college)
17 );
Table created.
SQL>
SQL> CREATE TABLE STUDENT
2 (
3 SID VARCHAR2 (7) CONSTRAINT pk_sid PRIMARY KEY,
4 NAME CHAR (14),
5 ADDRESS CHAR (22),
6 BIRTHDATE DATE,
7 GRADELEVEL CHAR (2) CONSTRAINT nn_glvl NOT NULL
8 );
Table created.
SQL>
SQL> CREATE TABLE SECTION
2 (
3 SECTION# VARCHAR2 (8) CONSTRAINT pk_section# PRIMARY KEY,
4 TIME CHAR (5),
5 MAXST NUMBER (2),
6 ROOM VARCHAR2 (14),
7 course# CHAR (10), -- added by LF
8 empid VARCHAR2 (8), -- added by LF
9 CONSTRAINT chk_maxst CHECK (maxst <= 35),
10 CONSTRAINT fk_crs FOREIGN KEY (course#) REFERENCES course (course#),
11 CONSTRAINT fk_pro FOREIGN KEY (empid) REFERENCES professor (empid)
12 );
Table created.
SQL>
SQL> CREATE TABLE TAKES
2 (
3 GRADE CHAR (5) CONSTRAINT nn_grade NOT NULL,
4 section# VARCHAR2 (8), -- added by LF
5 sid VARCHAR2 (7), -- added by LF
6 CONSTRAINT chk_grade CHECK (grade IN ('A', 'B', 'C')),
7 CONSTRAINT fk_sec FOREIGN KEY (section#) REFERENCES section (section#),
8 CONSTRAINT fk_stu FOREIGN KEY (sid) REFERENCES student (sid)
9 );
Table created.
SQL>

Resources