如何创建 ORACLE 序列以根据另一个 table 填充维度 Table
How To create ORACLE Sequence to Populate Dimension Table based off another table
编辑:为了清楚起见,我修改了我原来的问题。希望这有助于更清楚地解释我要完成的工作。
我有一个标准的 SQL table VEHICLES
,我用 RENAME
语句将其名称更改为 OLTP_VEHICLES
。
我创建了一个新的 VEHICLES table 作为维度 table,它是我的数据库星型模式的 "beginning"。
我现在需要完成以下任务:
"For the vehicleCode
primary key column, use an Oracle Sequence to populate the values. For the vehicleDescription
column, use a concatenated combination of vehicleMake
and vehicleModel
from the OLTP_VEHICLES table
."
我需要通过使用 PL/SQL 块来完成此操作,通过从 OLTP_VEHICLES
table 中选择 vehicleMake
和 vehicleModel
来填充描述列然后通过循环中的游标将串联组合插入 VEHICLES
维度 table。
看到这条指令,我完全莫名其妙。我认为我之前让你们这些好人感到困惑的地方是我遗漏了涉及插入 vehicleMake
和 vehicleModel
连接的 "second part"。
这是否有助于更好地解释我所追求的?如果没有,我深感抱歉。我对此很困惑,甚至无法解释它。再次感谢您的帮助。
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
谢谢!
经过大量研究、反复试验,当然还有@MatBailie 的帮助,终于弄明白了。这是我寻求的解决方案:
--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;
/
编辑:为了清楚起见,我修改了我原来的问题。希望这有助于更清楚地解释我要完成的工作。
我有一个标准的 SQL table VEHICLES
,我用 RENAME
语句将其名称更改为 OLTP_VEHICLES
。
我创建了一个新的 VEHICLES table 作为维度 table,它是我的数据库星型模式的 "beginning"。
我现在需要完成以下任务:
"For the vehicleCode
primary key column, use an Oracle Sequence to populate the values. For the vehicleDescription
column, use a concatenated combination of vehicleMake
and vehicleModel
from the OLTP_VEHICLES table
."
我需要通过使用 PL/SQL 块来完成此操作,通过从 OLTP_VEHICLES
table 中选择 vehicleMake
和 vehicleModel
来填充描述列然后通过循环中的游标将串联组合插入 VEHICLES
维度 table。
看到这条指令,我完全莫名其妙。我认为我之前让你们这些好人感到困惑的地方是我遗漏了涉及插入 vehicleMake
和 vehicleModel
连接的 "second part"。
这是否有助于更好地解释我所追求的?如果没有,我深感抱歉。我对此很困惑,甚至无法解释它。再次感谢您的帮助。
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
谢谢!
经过大量研究、反复试验,当然还有@MatBailie 的帮助,终于弄明白了。这是我寻求的解决方案:
--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;
/