Having two different Customer Types in a Database - sql

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

Related

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

Trouble adding Foreign Key Constraint (error ORA-02270: no matching unique or primary key for this column-list)

I am learning how to create a small and simple database for class with Primary Key and Foreign Key restraints. I am getting ORA-02770 when attempting to run my ALTER TABLE statement, which as I understand is notifying me that the column I am referencing in the outside table is not listed as a primary key constraint. However, from what I can see my syntax is correct naming the primary keys in my CREATE TABLES statements. I searched inside the all_cons_columns table for the player_info table and it showed the primary keys listed as well. Could I get some guidance? Listed below is my current script:
CREATE TABLE Player_Game
( school varchar2(30),
player_number number(2,0),
game_number number(1,0),
CONSTRAINT playergame_pk PRIMARY KEY (school, player_number,game_number)
);
CREATE TABLE School
( school varchar2(30),
city varchar2(30),
coach varchar2(30),
team_name varchar2(30),
win_record number (2,0),
loss_record number (2,0),
CONSTRAINT school_pk PRIMARY KEY (school)
);
CREATE TABLE Game
( school varchar2(30),
game_number number(1,0),
game_date DATE,
game_score varchar2(15),
CONSTRAINT game_pk PRIMARY KEY (school, game_number)
);
CREATE TABLE player_info
( school varchar2(30),
player_number number(2,0),
player_name varchar2(25),
CONSTRAINT playerinfo_pk PRIMARY KEY (school, player_number)
);
CREATE TABLE city
( city varchar2(30),
population number(5,0),
CONSTRAINT city_pk PRIMARY KEY (city)
);
/*Here is the failing alter command */
ALTER TABLE Player_Game
ADD CONSTRAINT playergame_fk FOREIGN KEY (school) REFERENCES game(school);
You have incorrect column list in playergame_fk in the alter table statement. The column list of the foreign key must exactly match with the column list of the primary key it is referencing to.
Primary Key column list is school, game_number, therefore, your foreign key must have the same columns:
ALTER TABLE Player_Game
ADD CONSTRAINT playergame_fk FOREIGN KEY (school, game_number)
REFERENCES game(school, game_number);

How do I fix a ORA-00902: invalid datatype creating a table

I am creating a table in a command SQL sections into a script already populated I have created several tables already but in this one I get a message saying
ORA-00902: invalid datatype
CREATE TABLE Weapons
(
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2 (10),
CONSTRAINT pk_Weapons PRIMARY_KEY(id),
CONSTRAINT fk_Weapons_company
FOREIGN_KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo
FOREIGN_KEY(ammo_id) REFERENCES Ammo(id)
);
In the CONSTRAINT, it should be FOREIGN KEY and not FOREIGN_KEY. Also it should be PRIMARY KEY, not PRIMARY_KEY.
There is no underscore required as per syntax. So the query will be:
CREATE TABLE Weapons (
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2(10),
CONSTRAINT pk_Weapons PRIMARY KEY(id),
CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES Ammo(id)
);
About Foreign Keys: https://www.techonthenet.com/oracle/foreign_keys/foreign_keys.php
About Primary Keys: https://www.techonthenet.com/oracle/primary_keys.php
Here's a working example. I've created the AMMO table (whose description you didn't post, so I used only the ID column so that the foreign key constraint wouldn't fail). Pay attention to comments I wrote within the code.
SQL> create table ammo
2 ( id VARCHAR2(10),
3 CONSTRAINT pk_ammo PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Company
2 ( id VARCHAR(3),
3 name VARCHAR(30), --> switch from CHAR to VARCHAR2
4 CONSTRAINT pk_Company PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Weapons
2 ( id NUMBER(4),
3 name VARCHAR2(30),
4 damage NUMBER(4),
5 company_id VARCHAR2(3), --> should match COMPANY.ID datatype
6 ammo_id VARCHAR2(10), --> should match AMMO.ID datatype
7 CONSTRAINT pk_Weapons PRIMARY KEY(id),
8 CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
9 CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES ammo(id) );
Table created.
SQL>
In referential integrity constraint, you should match datatypes of the foreign and primary key columns. There's no sense in having a VARCHAR2(10) in the detail table which points to a VARCHAR2(3) column in the master table; you won't be able to put anything longer than 3 characters into the detail table's column anyway (foreign key constraint won't let you).

Check atribute value from another table using a foreign key in table creation

Is it possible to check the value of an attribute in a different table in table creation? For example I have this table ticket:
CREATE TABLE ticket
(
id_ticket_pk NUMBER(4) PRIMARY KEY,
hand_in_date DATE NOT NULL,
num_clothes NUMBER(4) NOT NULL,
deposite NUMBER (8,2) NOT NULL,
comment VARCHAR2(40),
tax NUMBER(5,3) NOT NULL,
status VARCHAR2(15) NOT NULL,
id_counter_fk1 NUMBER(4),
id_client_fk2 NUMBER(4),
CONSTRAINT ticket_id_counter_fk1
FOREIGN KEY (id_counter_fk1) REFERENCES empleado(id_employee_pk),
CONSTRAINT ticket_id_client_fk2
FOREIGN KEY(id_client_fk2) REFERENCES client(id_client_pk)
);
This table relates to the employee table which has an attribute called type that can be equal to C - for counter, S- for secretary etc... I would like to know if its possible to add a constraint in the creation of the table that verifies that the employee inserted has an employee.type = 'C' where employe.id_pk = ticket.id_counter_fk2. Or do I have to create an external function or client App that manages this?
You seem to be asking whether you can use the foreign keying mechanism to ensure that only people with the job title of "counter" can create support tickets.
The short answer is no, you can't have a foreign key on this table that checks the employee table pk for the value given in id_counter_fk1 and enforces that the employee type be C
You could do it several other ways, the most direct being a trigger that prevents the insert if the employee linked is not a C type, but the FK mechanism exists to ensure that a remote record exists or not, not that it exists and has particular properties

Cant create tables, sql error ORA-02270

Yeah, not too sure on this one. New to sql and I thought everything was done right, any ideas? If this is not the way to add foreign keys, could someone please explain to me how the correct way to do it is please? Would appreciate it, thanks.
CREATE TABLE customer (
reference NUMBER(5) PRIMARY KEY,
company_name VARCHAR2(30),
address VARCHAR2(30),
post_code VARCHAR2(10),
telephone VARCHAR(20),
contact_fname VARCHAR2(20),
contact_sname VARCHAR2(20),
contact_email VARCHAR2(30)
);
CREATE TABLE manifest (
barcode NUMBER(10) PRIMARY KEY,
trip_id NUMBER(10),
pickup_customer_ref VARCHAR2(30),
delivery_customer_ref VARCHAR2(30),
category NUMBER(1),
weight NUMBER(10)
);
CREATE TABLE category (
category NUMBER(1) PRIMARY KEY,
description VARCHAR2(15),
requirements VARCHAR2(30),
FOREIGN KEY (category) REFERENCES manifest(category)
);
CREATE TABLE trip (
trip_id NUMBER(10) PRIMARY KEY,
departure_date DATE,
return_date DATE,
vehicle_id VARCHAR2(10),
employee_no NUMBER(10),
FOREIGN KEY (trip_id) REFERENCES manifest(trip_id)
);
CREATE TABLE vehicle (
registration VARCHAR2(10) PRIMARY KEY,
vehicle_type_id VARCHAR2(10),
model VARCHAR2(15),
make VARCHAR2(15),
body VARCHAR2(15),
year NUMBER(4),
FOREIGN KEY (registration) REFERENCES trip(registration)
);
CREATE TABLE model (
vehicle_type_id VARCHAR(10) PRIMARY KEY,
make VARCHAR2(15),
model VARCHAR2(15),
FOREIGN KEY (vehicle_type_id) REFERENCES vehicle(vehicle_type_id)
);
CREATE TABLE driver (
employee_no NUMBER(10) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR(20),
ni_no VARCHAR2(15),
telephone VARCHAR2(20),
mobile VARCHAR2(12),
hazardous_goods VARCHAR2(1),
FOREIGN KEY (employee_no) REFERENCES trip(employee_no)
);
and the error message I get is
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
I get this error for every table after manifest btw
The error message is pretty clear. The reference for a foreign key needs to be a unique or primary key in the other table. So this is wrong in the category table.
FOREIGN KEY (category) REFERENCES manifest(category)
I assume you intend for the manifest to look like this:
CREATE TABLE manifest (
barcode NUMBER(10) PRIMARY KEY,
trip_id NUMBER(10) REFERENCES trip(trip_id),
pickup_customer_ref VARCHAR2(30),
delivery_customer_ref VARCHAR2(30),
category NUMBER(1) REFERENCES category(category),
weight NUMBER(10)
);
And so on for the other places where foreign keys are used. (This uses a short-hand notation for the foreign key reference; a separate clause in CREATE TABLE is also fine.)
Of course, the definition of manifest has to go after category and trip, in this example.
In other words, the reference goes in the other table, not where the primary key is defined.
Also, I would recommend being consistent with the names of the foreign keys. At the very least, they should generally have the same name as the primary key, where possible.
trip_id, vehicle_type_id, and employee_no all need to be marked as UNIQUE.