编辑:为了清楚起见,我修改了我的原始问题。希望这有助于解释我想要更清楚地完成的事情。
我有一个标准的SQL表VEHICLES
,我使用OLTP_VEHICLES
语句将其名称更改为RENAME
。
我创建了一个新的VEHICLES表作为维度表,它是此数据库的星型模式的“开头”。
我现在需要完成以下任务:
“对于vehicleCode
主键列,请使用Oracle序列填充值。对于vehicleDescription
列,请使用来自vehicleMake
和vehicleModel
的串联组合OLTP_VEHICLES table
。“
我需要通过使用PL / SQL块填充描述列来完成此操作,方法是从vehicleMake
表中选择vehicleModel
和OLTP_VEHICLES
,然后将连接的组合插入到VEHICLES
维度表,通过循环中的游标。
通过这条指令,我感到非常困惑。我认为我之前让你感到困惑的事实是我遗漏了涉及插入vehicleMake
和vehicleModel
连接的“第二部分”。
这有助于更好地解释我追求的目标吗?如果没有,我很抱歉。我对此很困惑,我甚至无法解释它。再次感谢您的协助。
CREATE TABLE VEHICLES
(vehicleVIN VARCHAR(25) PRIMARY KEY,
vehicleType VARCHAR(10) NOT NULL CHECK (lower(vehicleType) IN ('compact', 'midsize', 'fullsize', 'suv', 'truck')),
vehicleMake VARCHAR(15) NOT NULL,
vehicleModel VARCHAR(15) NOT NULL,
vehicleWhereFrom VARCHAR(20) NOT NULL CHECK (lower(vehicleWhereFrom) IN ('maryland','virginia','washington, d.c.')),
vehicleWholesaleCost DECIMAL(9,2) NOT NULL,
vehicleTradeID INT);
INSERT INTO VEHICLES
(vehicleVIN,vehicleType,vehicleMake,vehicleModel,vehicleWhereFrom,vehicleWholesaleCost,vehicleTradeID)
VALUES
('147258HHE91K3RT','compact','chevrolet','spark','Maryland',20583.00,NULL);
INSERT INTO VEHICLES
(vehicleVIN,vehicleType,vehicleMake,vehicleModel,vehicleWhereFrom,vehicleWholesaleCost,vehicleTradeID)
VALUES
('789456ERT0923RFB6','Midsize','ford','Taurus','washington, d.c.',25897.22,1);
INSERT INTO VEHICLES
(vehicleVIN,vehicleType,vehicleMake,vehicleModel,vehicleWhereFrom,vehicleWholesaleCost,vehicleTradeID)
VALUES
('1234567890QWERTYUIOP','fullsize','Lincoln','towncar','Virginia',44222.10,NULL);
INSERT INTO VEHICLES
(vehicleVIN,vehicleType,vehicleMake,vehicleModel,vehicleWhereFrom,vehicleWholesaleCost,vehicleTradeID)
VALUES
('WER234109TEO458GZ','SUV','Chevrolet','suburban','Maryland',52789.00,2);
ALTER TABLE VEHICLES RENAME TO OLTP_VEHICLES;
CREATE TABLE VEHICLES
(vehicleCode VARCHAR(25) PRIMARY KEY,
vehicleDescription VARCHAR(50) NOT NULL);
如果有人想测试一些内容,我也会把它放到SQL Fiddle中:http://sqlfiddle.com/#!4/2de3ae
谢谢!
答案 0 :(得分:0)
Finally figured it out, after lots of research, trial and error, and of course, help from @MatBailie. Here's the solution I was after:
--Oracle sequence to populate primary key values in VEHICLES dimension table
DROP SEQUENCE SEQ_VEHICLES;
CREATE SEQUENCE SEQ_VEHICLES START WITH 1 INCREMENT BY 1;
CREATE OR REPLACE TRIGGER VEHICLES_PK
BEFORE INSERT ON VEHICLES FOR EACH ROW
BEGIN
SELECT SEQ_VEHICLES.NEXTVAL INTO :new.vehicleCode FROM DUAL;
END;
/
--PL/SQL Block to populate the description column via a cursor in a loop w/concatenation
DECLARE
CURSOR vehDesCur IS
SELECT LOWER(vehicleMake) || ' ' || LOWER(vehicleModel) AS vehicleDescription
FROM OLTP_VEHICLES;
BEGIN
FOR oltp_data IN vehDesCur
LOOP
INSERT INTO VEHICLES (vehicleDescription)
SELECT oltp_data.vehicleDescription
FROM dual
WHERE NOT EXISTS
(SELECT *
FROM VEHICLES v
WHERE v.vehicleDescription = oltp_data.vehicleDescription);
END LOOP;
END;
/