简体   繁体   中英

Oracle SQLDeveloper , using computed column with another table

I'm trying to create a database which will be used by an app made for an auto repair shop. I have a 'Services' table where several services are listed with the corresponding price (eg oil change , 30$). I have another table "Repairs" where I'm storing information about the client's repairs.This table has a column price which must be calculated using each of the services prices in the table 'Services'. I am familiar with the concept of using 'computed columns' , but it appears that I can't compute a column using values from another table. Am i missing something here or how should i approach the problem?

Having a column named "Price" in the table "Repairs", if you are trying to store the total price of all services provided to a customer, you shouldn't do that.

Why don't you try to insert a service record to "Repairs" table for each service provided to a customer with a customer id and repair id. Each service will point to the "Services" table with a foreign key. This way you can query the total price of a repair or you can create a view that will show the total prices of all repairs.

Here are create scripts for sample tables:

-- Table named SERVICES:
CREATE TABLE "SERVICES" 
(
"SERVICE_ID" NUMBER NOT NULL ENABLE, 
"SERVICE_NAME" VARCHAR2(20 BYTE), 
"SERVICE_PRICE" NUMBER, 
CONSTRAINT "SERVICES_PK" PRIMARY KEY ("SERVICE_ID")
)

-- Table named CUSTOMERS:
CREATE TABLE "CUSTOMERS" 
(
"CUSTOMER_ID" NUMBER NOT NULL ENABLE, 
"CUSTOMER_NAME" VARCHAR2(20 BYTE), 
CONSTRAINT "CUSTOMERS_PK" PRIMARY KEY ("CUSTOMER_ID")
)

-- Table named REPAIRS:
CREATE TABLE "REPAIRS" 
(
"REPAIR_ID" NUMBER NOT NULL ENABLE, 
"CUSTOMER_ID" NUMBER NOT NULL ENABLE, 
"REPAIR_DATE" DATE, 
CONSTRAINT "REPAIRS_PK" PRIMARY KEY ("REPAIR_ID")
)

-- Table named REPAIR_SERVICES:
CREATE TABLE "REPAIR_SERVICES" 
(
"REPAIR_ID" NUMBER NOT NULL ENABLE, 
"SERVICE_ID" NUMBER NOT NULL ENABLE, 
CONSTRAINT "REPAIR_SERVICES_FK_1" FOREIGN KEY ("REPAIR_ID") REFERENCES "HR"."REPAIRS" ("REPAIR_ID") ENABLE, 
CONSTRAINT "REPAIR_SERVICES_FK_2" FOREIGN KEY ("SERVICE_ID") REFERENCES "HR"."SERVICES" ("SERVICE_ID") ENABLE
)

If you structure your tables like the ones above, you can use the script below to create a view which will get you the total cost of repairs including all services. Or you can just use the script to make a query.

CREATE VIEW V_REPAIR_PRICES AS (
SELECT REPAIRS.REPAIR_ID, REPAIRS.REPAIR_DATE, CUSTOMERS.CUSTOMER_NAME, T1.TOTAL_PRICE
FROM REPAIRS
INNER JOIN CUSTOMERS ON REPAIRS.CUSTOMER_ID = CUSTOMERS.CUSTOMER_ID
INNER JOIN

(
  SELECT REPAIR_SERVICES.REPAIR_ID, SUM(SERVICES.SERVICE_PRICE) as TOTAL_PRICE
  FROM REPAIR_SERVICES
  INNER JOIN SERVICES ON REPAIR_SERVICES.SERVICE_ID = SERVICES.SERVICE_ID
  GROUP BY REPAIR_SERVICES.REPAIR_ID
) T1

ON REPAIRS.REPAIR_ID = T1.REPAIR_ID
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM