简体   繁体   中英

Create a View in Oracle with SQL

I am trying to create a view that has both my strSupplierName column from my tblSuppliers table as well as my strProductName column from my tblProducts table, and have been having some trouble. Any help would be appreciated.

CREATE TABLE  "tblSuppliers" 
   (    "lngSupplierID" VARCHAR2(100) NOT NULL ENABLE, 
    "strSupplierName" VARCHAR2(100) NOT NULL ENABLE, 
    "strSupplierAddressStreet" VARCHAR2(200) NOT NULL ENABLE, 
    "strSupplierAddressCity" VARCHAR2(150) NOT NULL ENABLE, 
    "strSupplierAddressCountry" VARCHAR2(100) NOT NULL ENABLE, 
    "strSupplierAddressPostCode" VARCHAR2(25), 
    "strSupplierPhoneNo" VARCHAR2(15) NOT NULL ENABLE, 
    "strSupplierFaxNo" VARCHAR2(20), 
    "strPaymentTerms" VARCHAR2(500), 
     CONSTRAINT "tblSuppliers_PK" PRIMARY KEY ("lngSupplierID") ENABLE
   ) ;

CREATE OR REPLACE TRIGGER  "BI_tblSuppliers" 
  before insert on "tblSuppliers"               
  for each row  
begin   
  if :NEW."lngSupplierID" is null then 
    select "TBLSUPPLIERS_SEQ".nextval into :NEW."lngSupplierID" from dual; 
  end if; 
end; 


CREATE TABLE  "strProducts" 
   (    "lngProductID" VARCHAR2(100) NOT NULL ENABLE, 
    "strProductName" VARCHAR2(100) NOT NULL ENABLE, 
    "chkInStock" VARCHAR2(5) NOT NULL ENABLE, 
    "lngUnitsInStock" NUMBER(38,0) NOT NULL ENABLE, 
    "curProductUnitPurchasePrice" VARCHAR2(10), 
    "curProductUnitSalePrice" VARCHAR2(10), 
    "lngSupplierID" VARCHAR2(100), 
     CONSTRAINT "tblProducts_PK" PRIMARY KEY ("lngProductID") ENABLE
   ) ;

ALTER TABLE  "tblProducts" ADD FOREIGN KEY ("lngSupplierID")
      REFERENCES  "tblSuppliers" ("lngSupplierID") ENABLE;

CREATE OR REPLACE TRIGGER  "BI_tblProducts" 
  before insert on "tblProducts"               
  for each row  
begin   
  if :NEW."lngProductID" is null then 
    select "TBLPRODUCTS_SEQ".nextval into :NEW."lngProductID" from dual; 
  end if; 
end; 

/
ALTER TRIGGER  "BI_tblProducts" ENABLE;

Is this what you're looking for?

CREATE VIEW "yourViewName" AS
SELECT
 p."lngProductID",
 p."strProductName",
 s."lngSupplierID",
 s."strSupplierName"
FROM "strProducts" p
INNER JOIN "tblSuppliers" s ON s."lngSupplierID" = p."lngSupplierID"

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