How to fix "ERROR: FK name length exceeds maximum allowed length(30)" in SQL Developer Data Modeller - sql

I'm trying to turn the Logical Model of my Database into a DDL script, but I don't know how to fix this error in the DDL script or Data Modeller:-- ERROR: FK name length exceeds maximum allowed length(30)
It seems to be based on my junction-table's Primary Key, which is made up from two Foreign Keys from the two neighboring tables.
I have tried changing the names of the Primary Keys in the neighboring tables, but when I try to generate a NEW DDL script with Data Modeler, it still generates the old script.
Here's the sections of code that create the 3 tables and link them together:
CREATE TABLE items (
item_no NUMBER (8) NOT NULL,
"year" DATE,
price NUMBER (20,2)
);
ALTER TABLE items ADD CONSTRAINT items_pk PRIMARY KEY ( item_no );
CREATE TABLE purchase_order (
order_no NUMBER(8) NOT NULL,
quantity INTEGER,
item_description VARCHAR2(200),
unit_price NUMBER(20,2),
total NUMBER(20,2),
order_date DATE,
sales_person_code VARCHAR2(5) NOT NULL,
supplier_id NUMBER(3) NOT NULL
);
ALTER TABLE purchase_order ADD CONSTRAINT purchase_order_pk PRIMARY KEY ( order_no );
CREATE TABLE purchase_order_items (
purchase_order_order_no NUMBER(8) NOT NULL,
items_item_no NUMBER(8) NOT NULL
);
ALTER TABLE purchase_order_items ADD CONSTRAINT purchase_order_items_pk PRIMARY KEY ( items_item_no,
purchase_order_order_no );
ALTER TABLE purchase_order_items
ADD CONSTRAINT purchase_order_items_items_fk FOREIGN KEY ( items_item_no )
REFERENCES items ( item_no );
-- ERROR: FK name length exceeds maximum allowed length(30)
ALTER TABLE purchase_order_items
ADD CONSTRAINT purchase_order_items_purchase_order_fk FOREIGN KEY ( purchase_order_order_no )
REFERENCES purchase_order ( order_no );
ALTER TABLE purchase_order
ADD CONSTRAINT purchase_order_sales_person_fk FOREIGN KEY ( sales_person_code )
REFERENCES sales_person ( code );
ALTER TABLE purchase_order
ADD CONSTRAINT purchase_order_supplier_fk FOREIGN KEY ( supplier_id )
REFERENCES supplier ( id );
So I'm not sure exactly what FK name length is too long and what I need to change in the script to fix this error.

Oracle limits identifiers to 30 characters, so
purchase_order_items_purchase_order_fk needs to be shorted. Perhaps to something like poi_purchase_porder_fk.

Related

"no matching unique or primary key for this column-list" Error in SQL

Was creating some tables in SQL and got stuck when I had to design the following tables:
As you can see it is impossible to create Room details and services details Table without customer receipt table as they contain Receipt_no as the primary key.
Similarly, it is impossible to create a Customer Receipt table without Room_charges and Service_charges attributes without first creating the two details tables.
Hence, I first created a customer receipt table but without FK constraints on Room_Charges and Service_charges and then I created Services Details and Room Details tables.
Later, using ALTER command I tried to add FK Constraints on Customer Receipt table but it gives me this error
ORA-02270: no matching unique or primary key for this column-list
Now, after researching a bit about it on StackOverflow, there might be three possible cases as mentioned in the approved answer (# Oracle (ORA-02270) : no matching unique or primary key for this column-list error )
I think my case is number 3 as I have ensured the first two cases to be implemented.
Can anyone help me resolve it?
I am attaching SQL Code as a reference:
CREATE TABLE Customer_Receipt
(
Receipt_no VARCHAR2(12) PRIMARY KEY,
Booking_no NUMBER NOT NULL,
Total_charges NUMBER(12,2),
CONSTRAINT bookingnocustrec
FOREIGN KEY(Booking_no) REFERENCES Room_booking (Booking_no)
);
CREATE TABLE Services_Details
(
Receipt_no VARCHAR2(12) NOT NULL,
Service_offered VARCHAR2(8) NOT NULL,
Service_charges NUMBER(12,2),
PRIMARY KEY(Receipt_no, Service_offered),
CONSTRAINT recno
FOREIGN KEY(Receipt_no) REFERENCES Customer_receipt (Receipt_no)
);
ALTER TABLE Services_Details
MODIFY Service_charges NOT NULL;
CREATE TABLE Room_Details
(
Receipt_no VARCHAR2(12) NOT NULL,
Category_name VARCHAR2(9) NOT NULL,
Days_stayed INT,
Room_charges NUMBER(12,2),
PRIMARY KEY(Receipt_no, Category_name),
CONSTRAINT recno1
FOREIGN KEY(Receipt_no) REFERENCES Customer_receipt (Receipt_no),
CONSTRAINT catname1
FOREIGN KEY(Category_name) REFERENCES Room_category (Category_name)
);
ALTER TABLE Customer_receipt
ADD Room_charges NUMBER(12,2) NOT NULL;
ALTER TABLE Customer_receipt
ADD CONSTRAINT FK_RC
FOREIGN KEY (Room_charges) REFERENCES Room_Details (Room_charges);
As a frame challenge.
Can anyone help me resolve it?
Yes, do not violate Third Normal Form and do not duplicate data by storing Total_Charges, Service_Charges or Room_Charges in the Customer_Receipt table when the data is already stored in the Service_Details and Room_Details tables.
If you are storing the same data in two locations then you are likely get into the situation where the data is inconsistent between those two location; just store each piece of data in a single location so there is a single source of truth in your database.
CREATE TABLE Customer_Receipt
(
Receipt_no VARCHAR2(12)
CONSTRAINT custreceipt__recno__pk PRIMARY KEY,
Booking_no CONSTRAINT custreceipt__bookingno__fk REFERENCES Room_booking
NOT NULL
);
CREATE TABLE Services_Details
(
Receipt_no CONSTRAINT servicedetails__recno__fk REFERENCES Customer_receipt
NOT NULL,
Service_offered VARCHAR2(8)
NOT NULL,
Service_charges NUMBER(12,2),
CONSTRAINT servicedetails__recno_servoff__pk PRIMARY KEY(Receipt_no, Service_offered)
);
CREATE TABLE Room_Details
(
Receipt_no CONSTRAINT roomdetails__recno__fk REFERENCES Customer_receipt
NOT NULL,
Category_name CONSTRAINT roomdetails__catname__fk REFERENCES Room_category
NOT NULL,
Days_stayed INT,
Room_charges NUMBER(12,2),
CONSTRAINT roomdetails__recno_catname__pk PRIMARY KEY(Receipt_no, Category_name)
);
If you want to display the Total_Charges, Service_Charges and Room_Charges then use a JOIN and get the data from the related tables. Something like:
SELECT cr.*,
COALESCE(s.service_charges, 0) AS service_charges,
COALESCE(r.room_charges, 0) AS room_charges,
COALESCE(s.service_charges, 0) + COALESCE(r.room_charges, 0)
AS total_charges
FROM customer_receipt cr
LEFT OUTER JOIN (
SELECT receipt_no,
SUM(service_charges) AS service_charges
FROM services_details
GROUP BY receipt_no
) s
ON cr.receipt_no = s.receipt_no
LEFT OUTER JOIN (
SELECT receipt_no,
SUM(days_stayed * room_charges) AS room_charges
FROM room_details
GROUP BY receipt_no
) r
ON cr.receipt_no = r.receipt_no;
Or create a view (or a materialized view).

Creating triggers and selecting the sum of shipped order with two variables

I currently have this assignment where we are supposed to create triggers to block the insert of a shipment depending on whether they exceed the total or not.
I have to sum all the shipped quantities and group them by order and product number.
Here are my tables to give an outlook:
My variables are in French, I'm sorry
CREATE TABLE LigneLivraison
(noLivraison NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
noCommande NUMBER(19) NOT NULL,
quantiteLivree NUMBER(19) NOT NULL,
PRIMARY KEY (noLivraison),
FOREIGN KEY (noLivraison) REFERENCES Livraison,
FOREIGN KEY (noProduit) REFERENCES Produit,
FOREIGN KEY (noCommande) REFERENCES Commande
)
/
CREATE TABLE LigneCommande
(noCommande NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
quantite NUMBER(19) NOT NULL,
CHECK (quantite > 0),
PRIMARY KEY (noCommande, noProduit),
FOREIGN KEY (noCommande) REFERENCES Commande,
FOREIGN KEY (noProduit) REFERENCES Produit
)
/
When I try to execute it, Oracle mentions that the SQL command does not end correctly
Here is what I have so far for the first trigger.
CREATE OR REPLACE TRIGGER bloquerInsertionCommande
BEFORE INSERT
ON LigneLivraison
REFERENCING
NEW AS NouvelleLivraison
FOR EACH ROW
BEGIN
SELECT LigneLivraison.noProduit, LigneLivraison.noCommande, LigneCommande.quantite, SUM(LigneLivraison.quantiteLivree) shipped, (LigneCommande.quantite - shipped) Total
FROM LigneLivraison, LigneCommande
WHERE LigneLivraison.noCommande = LigneCommande.noCommande
GROUP BY noCommande AND noProduit;
IF :NouvelleLivraison.quantiteLivree > Total THEN raise_application_error(-20100, 'La quantite a livrer est trop elevee');
END IF;
END;
/
Since shipping can be made seperately and an x number of times, I need to add all the same product of a certain order number to compare it with what the client originally ordered.
Your create table commands are not working as there are multiple issues as mentioned in the comments inline in following code:
CREATE TABLE LigneLivraison
(noLivraison NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
noCommande NUMBER(19) NOT NULL,
quantiteLivree NUMBER(19) NOT NULL,
PRIMARY KEY (noLivraison),
FOREIGN KEY (noLivraison) REFERENCES Livraison(pk_column_name_of_livraison_table), -- you need to add column name of referencing table here
FOREIGN KEY (noProduit) REFERENCES Produit(pk_column_name_of_produit_table), -- you need to add column name of referencing table here
FOREIGN KEY (noCommande) REFERENCES Commande(pk_column_name_of_commande_table) -- you need to add column name of referencing table here
); -- ending statement with ;
-- / -- this is not needed in sql statements
CREATE TABLE LigneCommande
(noCommande NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
quantite NUMBER(19) NOT NULL CHECK (quantite > 0), -- combined it in single column level constraint
PRIMARY KEY (noCommande, noProduit),
FOREIGN KEY (noCommande) REFERENCES Commande(pk_column_name_of_commande_table), -- you need to add column name of referencing table here
FOREIGN KEY (noProduit) REFERENCES Produit(pk_column_name_of_produit_table) -- you need to add column name of referencing table here
);
--/
Also, pk of referencing table is written in comment for clarity. You can use pk or unique key in references clause according to your requirement.
After your tables are created properly then only you will be able to identify proper error in the trigger.

connecting tables in sql apex?

hi i created tables using sql :-
create tables
create table customer (
cust_id number not null constraint customer_id_pk primary key,
cust_first varchar2(4000),
cust_last varchar2(4000),
cust_city varchar2(255),
)
;
create table medicine (
med_id number not null constraint medicine_id_pk primary key,
med_name varchar2(255),
med_info varchar2(4000),
med_prise number
)
;
create table the_order (
order_id number not null constraint the_order_id_pk primary key,
order_date date,
order_buyer varchar2(4000),
order_med varchar2(4000)
)
;
how do i connect the tables together into the order table i am using Apex to make a project.
i know its about foreign key but in the apex platform to show the forms as a report on one big table ?
Defining foreign keys is something done on the database, for a number of good reasons.
Your queries will work without them, but won't perform as well, and you could get dirty data over time.
It's all about the SQL. APEX is just a conduit to get the data from the database into a web page in front of the user's eyes.
You have a table design issue.
Provided you follow their best practices, it should figure out foreign keys and indexes for you.
Example
Quick SQL
customers
first_name /nn
last_name /nn
city
medicines
name /nn
info
price num /nn
orders
date /nn
customer_id /nn
order_lines
medicine_id /nn
medicine_name /nn
medicine_price num /nn
quantity num /nn
total num /nn
Generated SQL
-- create tables
create table customers (
id number generated by default on null as identity
constraint customers_id_pk primary key,
first_name varchar2(255 char) not null,
last_name varchar2(255 char) not null,
city varchar2(255 char)
)
;
create table medicines (
id number generated by default on null as identity
constraint medicines_id_pk primary key,
name varchar2(255 char) not null,
info varchar2(4000 char),
price number not null
)
;
create table orders (
id number generated by default on null as identity
constraint orders_id_pk primary key,
customer_id number
constraint orders_customer_id_fk
references customers on delete cascade not null,
the_date date not null
)
;
-- table index
create index orders_i1 on orders (customer_id);
create table order_lines (
id number generated by default on null as identity
constraint order_lines_id_pk primary key,
order_id number
constraint order_lines_order_id_fk
references orders on delete cascade,
medicine_id number
constraint order_lines_medicine_id_fk
references medicines on delete cascade not null,
medicine_name varchar2(255 char) not null,
medicine_price number not null,
quantity number not null,
total number not null
)
;
-- table index
create index order_lines_i1 on order_lines (medicine_id);
create index order_lines_i2 on order_lines (order_id);

Oracle: Constraints of keys

I have an entity within Oracle's SQL:
and I'm wondering, what does "UF" represent? I've written the SQL code but I do not know how to represent the "UF" attribute as a constraint.
CREATE TABLE entry (
entryno NUMBER(4) NOT NULL,
carndate DATE NOT NULL,
entrystarttime DATE NOT NULL,
entryfinishtime DATE NOT NULL,
entryplace NUMBER(4) NOT NULL,
charname VARCHAR2(30) NOT NULL,
compno NUMBER(4) NOT NULL,
eventypecode CHAR(3) NOT NULL,
teamname VARCHAR2(30) NOT NULL
);
ALTER TABLE entry ADD CONSTRAINT entry_pk PRIMARY KEY ( entryno,
carndate );
ALTER TABLE entry
ADD CONSTRAINT entry_charity_fk FOREIGN KEY ( charname )
REFERENCES charity ( charname );
ALTER TABLE entry
ADD CONSTRAINT entry_carnival_fk FOREIGN KEY ( carndate )
REFERENCES carnival ( carndate );
ALTER TABLE entry #
ADD CONSTRAINT entry_competitor_fk FOREIGN KEY ( compno )
REFERENCES competitor ( compno );
ALTER TABLE entry #
ADD CONSTRAINT entry_event_fk FOREIGN KEY ( carndate, eventypecode )
REFERENCES evenet ( carndate, eventypecode );
ALTER TABLE entry
ADD CONSTRAINT entry_team_fk FOREIGN KEY ( teamname, carndate )
REFERENCES team ( teamname,carndate );
ALTER TABLE entry
ADD CONSTRAINT ... ("UF")
The UF is actually two flags.
As you've recognised the F means the column is a foreign key. The U stands for Unique. Those columns form part of the compound unique key:
ALTER TABLE entry ADD CONSTRAINT entry_carnival_un UNIQUE
( carndate, compno, eventypecode );
carndate is also part of the compound primary key so really it should be flagged PFU but I guess the modelling tool didn't allow for three flags.

Postgresql: syntax error at or near “.”

Why do I get syntax error at or near “.” ?
CREATE TABLE myschema.products (
product_no SERIAL PRIMARY KEY,
date date,
group_number INTEGER,
CHECK (myschema.products >1 AND myschema.products <1001)
);
CREATE TABLE myschema.orders (
order_id SERIAL PRIMARY KEY,
name varchar,
schedule integer[][]
);
CREATE TABLE myschema.tabletime (
id SERIAL,
products INTEGER,
orders INTEGER,
CONSTRAINT pkey PRIMARY KEY (id),
CONSTRAINT integrity CHECK (products IS NOT NULL
AND orders IS NOT NULL),
CONSTRAINT products_exists FOREIGN KEY(products)
REFERENCES myschema.products(product_no),
CONSTRAINT orders_exists FOREIGN KEY(orders)
REFERENCES myschema.orders(order_id)
);
The error is at the line:
CHECK (myschema.products >1 AND myschema.products <1001)
you cannot use the name of the relation inside a check, only the name of the columns. The manual says:
Currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns of the current row.