connecting tables in sql apex? - sql

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);

Related

conditional primary key based on value not being null

I have a table that has a composite primary key:
CREATE TABLE tb (
it_service VARCHAR2(200),
hostname VARCHAR2(255),
sw_name VARCHAR2(200 CHAR),
product_home VARCHAR2(500),
product_home_ods VARCHAR2(500),
instance_name VARCHAR2(255),
eg_status VARCHAR2(10),
);
ALTER TABLE tb ADD (
CONSTRAINT pk_tb PRIMARY KEY ( hostname,
instance_name,
product_home,
product_home_ods,
it_service,
sw_name )
);
The problem is that when I insert into this table, the data from the product_home column is null for the rows where product_home_ods has data. The same applies product_home_ods, the rows where that table contain data are null for product_home.
Example:
product_home product_home_ods
java null
python null
null windows
null windows_server
Is it possible to create a primary key based on the condition of these columns ? I understand a primary key should not contain null values, but unsure about the best way to tackle this problem.
And changing to this model ?
product_home will never be NULL and ods_flag will indicate if it should be interpreted a product_home or product_home_ods, something you can do in a view to get back the same data as your original table.
CREATE TABLE tb (
it_service VARCHAR2(200),
hostname VARCHAR2(255),
sw_name VARCHAR2(200 CHAR),
product_home VARCHAR2(500),
ods_flag NUMBER(1,0),
instance_name VARCHAR2(255),
eg_status VARCHAR2(10),
);
ALTER TABLE tb ADD (
CONSTRAINT pk_tb PRIMARY KEY ( hostname,
instance_name,
product_home,
ods_flag,
it_service,
sw_name )
);
(As long as product_home and product_home_ods of your original table are not foreign keys)

Trying to run script through Oracle 11g

I am trying to run SQL statements in the Oracle 11g Express edition, where I am to create tables. Here is my SQL code:
CREATE TABLE STORE
(
StoreID INT PRIMARY KEY,
StoreName VARCHAR2(30) NOT NULL,
City VARCHAR2(30) NOT NULL,
Country VARCHAR2(30) NOT NULL CHECK Country in ('China','Egypt','United States','Spain','New Zealand','Mexico','Africa'),
Phone VARCHAR2(30) NOT NULL,
Fax VARCHAR2(30),
Email VARCHAR2(50) UNIQUE,
Contact VARCHAR2(30) NOT NULL,
UNIQUE (StoreName, City)
);
CREATE TABLE PURCHASE_ITEM
(
PurchaseItemID INT PRIMARY KEY,
StoreID INT NOT NULL REFERENCES STORE(StoreID) ON DELETE CASCADE ON UPDATE CASCADE,
"Date" DATE NOT NULL,
Description VARCHAR2(30) NOT NULL,
Category VARCHAR2(30),
PriceUsed NUMBER(15, 2)
);
CREATE SEQUENCE pur_seq
START WITH 500
INCREMENT BY 5;
CREATE OR REPLACE TRIGGER Purchase
BEFORE INSERT ON PURCHASE_ITEM
FOR EACH ROW
BEGIN
SELECT pur_seq.NEXTVAL
INTO :new.PurchaseItemID
FROM dual;
END;
CREATE TABLE SHIPPER
(
ShipperID INT PRIMARY KEY,
ShipperName VARCHAR2(30) NOT NULL,
Phone VARCHAR2(30) NOT NULL,
Fax VARCHAR2(30),
Email VARCHAR2(50) UNIQUE,
Contact VARCHAR2(30) NOT NULL
);
CREATE TABLE SHIPMENT
(
ShipmentID INT PRIMARY KEY Auto Increment,
ShipperID INT NOT NULL REFERENCES SHIPPER(ShipperID) ON DELETE CASCADE ON UPDATE CASCADE,
ShipperInvoiceNumber INT NOT NULL UNIQUE,
Origin VARCHAR2(30) NOT NULL,
Destination VARCHAR2(30) NOT NULL,
DepartureDate DATE,
ArrivalDate DATE
);
ALTER TABLE SHIPMENT AUTO_INCREMENT = 100;
CREATE TABLE SHIPMENT_ITEM
(
ShipmentID INT NOT NULL REFERENCES SHIPMENT(ShipmentID) ON DELETE CASCADE ON UPDATE CASCADE,
ShipmentItemID INT NOT NULL,
PurchaseItemID INT NOT NULL REFERENCES PURCHASE_ITEM(PurchaseItemID) ON DELETE CASCADE ON UPDATE CASCADE,
InsuredValue NUMBER(15, 2) NOT NULL defaut 100,
PRIMARY KEY (ShipmentID, ShipmentItemID)
);
It only ends up processing 4 statements and I keep getting these error messages:
CREATE TABLE STORE ( StoreID INT PRIMARY K - ORA-00906: missing left parenthesis
CREATE TABLE PURCHASE_ITEM ( PurchaseItemID INT - ORA-00907: missing right parenthesis
CREATE SEQUENCE pur_seq START WITH 500 INCREMENT BY 5 - ORA-00955: name is already used by an existing object
CREATE OR REPLACE TRIGGER Purchase BEFORE INSERT ON PURCHASE - ORA-00942: table or view does not exist
I am wholly unfamiliar with Oracle 11g. I am unsure if I am using the correct application in it for my assignment. I am only going by these instructions:
"For this assignment you are to write scripts to create tables and insert records. In oracle 11g I don’t want you to use the tools to generate the tables, you are required to write scripts to create the tables and records and will need to include the scripts for your submission."
Please, what am I doing wrong?
Let me summarize the problems with your scripts
First you have to enclose the check constraint with braces like below
CREATE TABLE STORE
(
StoreID INT PRIMARY KEY,
StoreName VARCHAR2(30) NOT NULL,
City VARCHAR2(30) NOT NULL,
Country VARCHAR2(30) NOT NULL CHECK (Country in ('China','Egypt','United States','Spain','New Zealand','Mexico','Africa')),
Phone VARCHAR2(30) NOT NULL,
Fax VARCHAR2(30),
Email VARCHAR2(50) UNIQUE,
Contact VARCHAR2(30) NOT NULL,
UNIQUE (StoreName, City)
);
Second, there is no ON UPDATE CASCADE in Oracle 11g so you need to remove it from the CREATE TABLE statement
Third, there is no Auto increment in Oracle 11G for columns So refer this SO for a workaround
Please let me know with these corrections whether your issue is resolved

Having two different Customer Types in a Database

My system needs two different Customer Types. I need an Individual and Corporate Customer. How would I go about creating the tables in Apex?
Do I need a Customer, Individual and Corporate table or just a Customer and Corporate table?
This system is for a Car Hire.
The attributes I would need are: Customer ID, First Name, Surname, Address, Post Code, Phone Number, Corporate Name, Frequency (this is for the Corporate entity based on how regularly they hire).
A corporation name belongs to the corporation not to an individual customer; so create a Corporations table and then reference that in a foreign key for your corporate customers. You can also separate out the core Customer data from the CorporateCustomer data and store these in separate tables:
Oracle 18c Setup:
CREATE TABLE Customers (
id NUMBER(10,0)
GENERATED ALWAYS AS IDENTITY
CONSTRAINT Customers__id__pk PRIMARY KEY,
first_name VARCHAR2(200)
CONSTRAINT Customers__firstname__nn NOT NULL,
surname VARCHAR2(200)
CONSTRAINT Customers__surname__nn NOT NULL,
post_code VARCHAR2(7)
CONSTRAINT Customers__postcode__nn NOT NULL,
phone_number VARCHAR2(15)
CONSTRAINT Customers__phone__nn NOT NULL
);
CREATE TABLE Corporations(
id NUMBER(10,0)
GENERATED ALWAYS AS IDENTITY
CONSTRAINT Corporations__id__pk PRIMARY KEY,
name VARCHAR2(500)
CONSTRAINT Corporations__name__nn NOT NULL
);
CREATE TABLE Corporate_Customers (
id NUMBER(10,0)
CONSTRAINT CorporateCustomers__id__pk PRIMARY KEY
CONSTRAINT CorporateCustomers__id__fk REFERENCES Customers(id),
corporation NUMBER(10,0)
CONSTRAINT CorporateCustomers__corp__nn NOT NULL
CONSTRAINT CorporateCustomers__corp__fk REFERENCES Corporations(id),
frequency NUMBER(10,0)
CONSTRAINT CorporateCustomers__freq__nn NOT NULL
CONSTRAINT CorporateCustomers__freq_gte_0__chk CHECK ( frequency >= 0 )
);
db<>fiddle

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

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.

PostgreSQL Error: relation "products" does not exist

I'm trying to create the following schema
While I execute the following code I get the relation "products" does not exist error.
CREATE TABLE Orders(
ORDERID serial NOT NULL PRIMARY KEY,
ORDERDATE text NOT NULL default'',
CUSTOMERID serial NOT NULL REFERENCES Customers(CUSTOMERID),
NETAMOUNT text NOT NULL default'',
TAX text NOT NULL default'',
TOTALAMOUNT text NOT NULL default''
);
CREATE TABLE Orderlines(
ORDERLINEID serial NOT NULL,
ORDERID serial NOT NULL REFERENCES Orders(ORDERID),
PROD_ID serial NOT NULL REFERENCES Products(PROD_ID),
QUANTITY text NOT NULL default'',
ORDERDATE text NOT NULL default'',
PRIMARY KEY(ORDERLINEID,ORDERID)
);
CREATE TABLE Products(
PROD_ID serial NOT NULL PRIMARY KEY,
CATEGORY text NOT NULL references Products(CATEGORY),
TITLE text NOT NULL default'',
ACTOR text NOT NULL default'',
PRICE text NOT NULL default''
);
I am doing this in http://sqlfiddle.com/ if it has anything to do with it.
When the table orderlines is created, the create table for the products table has not been executed. You need to add the foreign keys after all tables have been created.
You should only defined the primary key columns as serial, not the foreign key columns - they will be referencing the generated values from the target table. You don't want to generate a new value for the FK column when you insert.
The foreign key:
CATEGORY text NOT NULL references Products(CATEGORY)
is wrong because of two reasons: First because category is not the primary key of the products table. And secondly because it doesn't make sense that the category column references itself. You probably wanted to reference the categories table.
You script is also missing the customers table.
And you are choosing the wrong data types.
Number or dates should NEVER be stored in text (or varchar) columns.
Putting all that together, the script should be something like this:
create table customers
(
customerid serial primary key,
.... other columns
);
create table categories
(
category serial primary key,
categoryname text not null
);
CREATE TABLE Orders
(
ORDERID serial NOT NULL PRIMARY KEY,
ORDERDATE date NOT NULL, -- no text here!
CUSTOMERID integer NOT NULL, --- NO serial here!
NETAMOUNT decimal (22,4) NOT NULL, -- no text here!
TAX decimal(18,2) NOT NULL, -- no text here!
TOTALAMOUNT decimal (24,4) NOT NULL -- no text here!
);
CREATE TABLE Orderlines
(
ORDERLINEID serial NOT NULL,
ORDERID integer NOT NULL, -- NO serial here!
PROD_ID integer NOT NULL, -- NO serial here!
QUANTITY integer NOT NULL,
ORDERDATE date NOT NULL, -- NO text here!
PRIMARY KEY(ORDERLINEID,ORDERID)
);
CREATE TABLE Products
(
PROD_ID serial NOT NULL PRIMARY KEY,
CATEGORY integer NOT NULL references categories,
TITLE text NOT NULL,
ACTOR text NOT NULL,
PRICE decimal (18,2) NOT NULL
);
alter table orderlines add foreign key (orderid) REFERENCES Orders(ORDERID);
alter table orderlines add foreign key (prod_id) REFERENCES products(prod_id);
alter table orders add foreign key (customerid) REFERENCES Customers(CUSTOMERID);