I am working on online platform to make an ERD and to get the PL/SQL code of it but I get the following code (see below) but I am not sure if it is PL/SQL.
I need to verify if this code is PL/SQL or not:
CREATE TABLE "CATEGORY" (
"ID" NUMBER(10) PRIMARY KEY,
"THLEVEL" NUMBER(10) NOT NULL
);
CREATE SEQUENCE "CATEGORY_SEQ" NOCACHE;
CREATE TRIGGER "CATEGORY_BI"
BEFORE INSERT ON "CATEGORY"
FOR EACH ROW
BEGIN
IF :NEW."ID" IS NULL THEN
SELECT "CATEGORY_SEQ".NEXTVAL INTO :NEW."ID" FROM DUAL;
END IF;
END;;
CREATE TABLE "REPORT" (
"ID" NUMBER(10) PRIMARY KEY,
"CLIENT" CLOB NOT NULL,
"VERSION" NUMBER(10) NOT NULL
);
CREATE SEQUENCE "REPORT_SEQ" NOCACHE;
CREATE TRIGGER "REPORT_BI"
BEFORE INSERT ON "REPORT"
FOR EACH ROW
BEGIN
IF :NEW."ID" IS NULL THEN
SELECT "REPORT_SEQ".NEXTVAL INTO :NEW."ID" FROM DUAL;
END IF;
END;;
CREATE TABLE "ASSET" (
"ID" NUMBER(10) PRIMARY KEY,
"REPORT" NUMBER(10) NOT NULL,
"IP" VARCHAR2(1000 CHAR) NOT NULL,
"NAME" VARCHAR2(1000 CHAR)
);
CREATE INDEX "IDX_ASSET__REPORT" ON "ASSET" ("REPORT");
ALTER TABLE "ASSET" ADD CONSTRAINT "FK_ASSET__REPORT" FOREIGN KEY ("REPORT") REFERENCES "REPORT" ("ID");
CREATE SEQUENCE "ASSET_SEQ" NOCACHE;
CREATE TRIGGER "ASSET_BI"
BEFORE INSERT ON "ASSET"
FOR EACH ROW
BEGIN
IF :NEW."ID" IS NULL THEN
SELECT "ASSET_SEQ".NEXTVAL INTO :NEW."ID" FROM DUAL;
END IF;
END;;
CREATE TABLE "SOLUTION" (
"ID" NUMBER(10) PRIMARY KEY,
"IMPLEVEL" VARCHAR2(1000 CHAR) NOT NULL,
"DIFFICULTY" VARCHAR2(1000 CHAR) NOT NULL,
"DESCRIPTION" CLOB NOT NULL
);
CREATE SEQUENCE "SOLUTION_SEQ" NOCACHE;
CREATE TRIGGER "SOLUTION_BI"
BEFORE INSERT ON "SOLUTION"
FOR EACH ROW
BEGIN
IF :NEW."ID" IS NULL THEN
SELECT "SOLUTION_SEQ".NEXTVAL INTO :NEW."ID" FROM DUAL;
END IF;
END;;
CREATE TABLE "VULNERABILITY" (
"ID" VARCHAR2(1000 CHAR) PRIMARY KEY,
"ASSET" NUMBER(10) NOT NULL,
"SOLUTION" NUMBER(10) NOT NULL,
"CATEGORY" NUMBER(10) NOT NULL,
"CVE" VARCHAR2(1000 CHAR),
"DATE" TIMESTAMP NOT NULL,
"LOCATION" CLOB NOT NULL
);
CREATE INDEX "IDX_VULNERABILITY__ASSET" ON "VULNERABILITY" ("ASSET");
CREATE INDEX "IDX_VULNERABILITY__CATEGORY" ON "VULNERABILITY" ("CATEGORY");
CREATE INDEX "IDX_VULNERABILITY__SOLUTION" ON "VULNERABILITY" ("SOLUTION");
ALTER TABLE "VULNERABILITY" ADD CONSTRAINT "FK_VULNERABILITY__ASSET" FOREIGN KEY ("ASSET") REFERENCES "ASSET" ("ID");
ALTER TABLE "VULNERABILITY" ADD CONSTRAINT "FK_VULNERABILITY__CATEGORY" FOREIGN KEY ("CATEGORY") REFERENCES "CATEGORY" ("ID");
ALTER TABLE "VULNERABILITY" ADD CONSTRAINT "FK_VULNERABILITY__SOLUTION" FOREIGN KEY ("SOLUTION") REFERENCES "SOLUTION" ("ID");
CREATE TABLE "EXPLOIT" (
"ID" NUMBER(10) PRIMARY KEY,
"VULNERABILITY" VARCHAR2(1000 CHAR) NOT NULL,
"NAME" VARCHAR2(1000 CHAR) NOT NULL,
"TYPE" CLOB NOT NULL,
"DESCRIPTION" CLOB NOT NULL
);
CREATE INDEX "IDX_EXPLOIT__VULNERABILITY" ON "EXPLOIT" ("VULNERABILITY");
ALTER TABLE "EXPLOIT" ADD CONSTRAINT "FK_EXPLOIT__VULNERABILITY" FOREIGN KEY ("VULNERABILITY") REFERENCES "VULNERABILITY" ("ID");
CREATE SEQUENCE "EXPLOIT_SEQ" NOCACHE;
CREATE TRIGGER "EXPLOIT_BI"
BEFORE INSERT ON "EXPLOIT"
FOR EACH ROW
BEGIN
IF :NEW."ID" IS NULL THEN
SELECT "EXPLOIT_SEQ".NEXTVAL INTO :NEW."ID" FROM DUAL;
END IF;
END;
So is this PL/SQL or is somethin else from Oracle? How this will sound in PL/SQL?
Well that is PL/SQL and it will work in Oracle if you remove the double semi-colon you have on create trigger.
https://dba.stackexchange.com/questions/1121/how-to-differentiate-between-sql-and-pl-sql
That is NOT PL/SQL. That is DDL (Data Definition Language). Specifically it is Oracle DDL.
These are DDL statements in which we can create, alter tables ,PL/SQL is a Procedural language SQL in which we use DML Statements for any Process.
These are a set of DDL commands and not PL/SQL as such. However if you incorporate these commands in a nice stored procedure or package, it might be termed as PL/SQL (procedural part of it) but not currently in its current shape.
Related
I'm trying to execute a simple .sql file that resets my database. It works fine when I run it in sqlplus with #, but returns ORA-00922: missing or invalid option, when I run it using php like that:
$query = file_get_contents($path);
$stid = oci_parse($con, $query);
$r = oci_execute($stid);
Here is the .sql file itself, it's quite simple and I simply can't find anything that is wrong with the commands:
ALTER SESSION SET NLS_LANGUAGE = AMERICAN;
ALTER SESSION SET NLS_TERRITORY = AMERICA;
DROP TABLE users;
DROP TABLE friends;
DROP TABLE replies;
DROP TABLE tweets;
DROP TABLE retweets;
DROP TABLE mentions;
DROP TABLE hashtags;
CREATE TABLE users (
id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
name VARCHAR2(40) NOT NULL,
screen_name VARCHAR2(20) NOT NULL,
followers NUMBER(9) NOT NULL,
friends NUMBER(9) NOT NULL
);
CREATE TABLE friends (
id1_str VARCHAR2(20) NOT NULL,
id2_str VARCHAR2(20) NOT NULL,
CONSTRAINT friendship PRIMARY KEY (id1_str, id2_str)
);
CREATE TABLE replies (
id1_str VARCHAR2(20) NOT NULL,
id2_str VARCHAR2(20) NOT NULL,
CONSTRAINT reply PRIMARY KEY (id1_str, id2_str)
);
CREATE TABLE tweets (
id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
user_id_str VARCHAR2(20) NOT NULL,
created DATE NOT NULL
);
CREATE TABLE retweets (
user_id_str VARCHAR2(20) NOT NULL,
tweet_id_str VARCHAR2(20) NOT NULL,
CONSTRAINT retweet PRIMARY KEY (user_id_str, tweet_id_str)
);
CREATE TABLE hashtags (
tweet_id_str VARCHAR2(20) NOT NULL,
text VARCHAR2(140) NOT NULL,
CONSTRAINT hashtag PRIMARY KEY (tweet_id_str, text)
);
CREATE TABLE mentions (
user_id_str VARCHAR2(20) NOT NULL,
tweet_id_str VARCHAR2(20) NOT NULL,
CONSTRAINT mention PRIMARY KEY (user_id_str, tweet_id_str)
);
COMMIT;
I know it's probably silly, but I've spent hours looking for the solution to no avail and would appreciate any tips (even a different method than loading this file).
The problem is simple. Your file contains multiple commands, and oci_parse is meant to work with single statements.
You've got two options:
Read the file, parse it for semi-colons, and pass each resulting string (minus the semi-colon) to oci_parse/oci_execute, or
Change your file so it's a valid PL/SQL block, with BEGIN at the beginning and END; at the end, with each of the DDL statements you want to execute in an EXECUTE IMMEDIATE as shown below:
BEGIN
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_LANGUAGE = AMERICAN';
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = AMERICA';
EXECUTE IMMEDIATE 'DROP TABLE users';
EXECUTE IMMEDIATE 'DROP TABLE friends';
EXECUTE IMMEDIATE 'DROP TABLE replies';
EXECUTE IMMEDIATE 'DROP TABLE tweets';
EXECUTE IMMEDIATE 'DROP TABLE retweets';
EXECUTE IMMEDIATE 'DROP TABLE mentions';
EXECUTE IMMEDIATE 'DROP TABLE hashtags';
EXECUTE IMMEDIATE 'CREATE TABLE users (
id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
name VARCHAR2(40) NOT NULL,
screen_name VARCHAR2(20) NOT NULL,
followers NUMBER(9) NOT NULL,
friends NUMBER(9) NOT NULL
)';
EXECUTE IMMEDIATE 'CREATE TABLE friends (
id1_str VARCHAR2(20) NOT NULL,
id2_str VARCHAR2(20) NOT NULL,
CONSTRAINT friendship PRIMARY KEY (id1_str, id2_str)
)';
EXECUTE IMMEDIATE 'CREATE TABLE replies (
id1_str VARCHAR2(20) NOT NULL,
id2_str VARCHAR2(20) NOT NULL,
CONSTRAINT reply PRIMARY KEY (id1_str, id2_str)
)';
EXECUTE IMMEDIATE 'CREATE TABLE tweets (
id_str VARCHAR2(20) NOT NULL PRIMARY KEY,
user_id_str VARCHAR2(20) NOT NULL,
created DATE NOT NULL
)';
EXECUTE IMMEDIATE 'CREATE TABLE retweets (
user_id_str VARCHAR2(20) NOT NULL,
tweet_id_str VARCHAR2(20) NOT NULL,
CONSTRAINT retweet PRIMARY KEY (user_id_str, tweet_id_str)
)';
EXECUTE IMMEDIATE 'CREATE TABLE hashtags (
tweet_id_str VARCHAR2(20) NOT NULL,
text VARCHAR2(140) NOT NULL,
CONSTRAINT hashtag PRIMARY KEY (tweet_id_str, text)
)';
EXECUTE IMMEDIATE 'CREATE TABLE mentions (
user_id_str VARCHAR2(20) NOT NULL,
tweet_id_str VARCHAR2(20) NOT NULL,
CONSTRAINT mention PRIMARY KEY (user_id_str, tweet_id_str)
)';
END;
How to define the autonomous pragma for error logging in oracle with the help of procedure.My table structure is like below.
CREATE TABLE error_logs (
id NUMBER(10) NOT NULL,
log_timestamp TIMESTAMP NOT NULL,
error_message VARCHAR2(4000),
CONSTRAINT error_logs_pk PRIMARY KEY (id)
);
CREATE SEQUENCE error_logs_seq
start with 1
nocycle
nocache;
i tried below code for my requirement.
CREATE OR REPLACE PROCEDURE log_errors (p_error_message IN VARCHAR2) AS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO error_logs (id, log_timestamp, error_message)
VALUES (error_logs_seq.NEXTVAL, SYSTIMESTAMP, p_error_message);
COMMIT;
END;
/
I have been tasked to populate a table called Sales_Facts with a PL/SQL block but I return 0 results. The procedure is executed with out error and I run my script to populate my table but my SELECT COUNT script returns nothing. I cannot see what I am doing wrong.
Here's what I have:
CREATE TABLE Sales (
sale_ID VARCHAR2(10) NOT NULL,
salesperson_ID VARCHAR2(10) NOT NULL,
cust_ID VARCHAR2(10) NOT NULL,
sale_date DATE,
VIN VARCHAR2(20) NOT NULL,
mileage INT,
vehicle_status VARCHAR2(15),
gross_sale_price NUMBER(8,2) NOT NULL,
PRIMARY KEY (sale_ID),
CONSTRAINT FK_Customer_ID FOREIGN KEY (cust_ID) REFERENCES Customers(cust_ID),
CONSTRAINT FK_VIN_ID FOREIGN KEY (VIN) REFERENCES Sale_Vehicles(VIN));
CREATE TABLE Times (
sale_day DATE NOT NULL, --populated from Sales sale_date
day_type VARCHAR2(50) NOT NULL,
PRIMARY KEY (sale_day));
CREATE TABLE Vehicles (
vehicle_Code VARCHAR2(10),
description VARCHAR2(100),
PRIMARY KEY (vehicle_Code));
Vehicles is populated with this:
CREATE SEQUENCE veh_code_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 20;
COMMIT;
--PL/SQL Block
SET SERVEROUTPUT ON
DECLARE
vehType VARCHAR2(50);
v_make OLTP_Vehicles.make%type;
v_model OLTP_Vehicles.model%type;
CURSOR v_type IS SELECT DISTINCT make, model FROM OLTP_Vehicles;
BEGIN
OPEN v_type;
LOOP
FETCH v_type INTO v_make, v_model;
vehType := v_make || ', ' || v_model;
INSERT INTO Vehicles (vehicle_Code, description)
VALUES (veh_code_seq.NEXTVAL, vehType);
EXIT WHEN v_type%notfound;
END LOOP;
CLOSE v_type;
END;
/
CREATE TABLE Financing_Plans (
plan_ID VARCHAR2(10) NOT NULL,
institution VARCHAR2(25) NOT NULL,
loan_type VARCHAR2(15) NOT NULL,
percentage DECIMAL(4,2) NOT NULL,
min_down NUMBER(8,2) NOT NULL,
max_loan_amt NUMBER(8,2) NOT NULL,
max_term INT NOT NULL,
PRIMARY KEY (plan_ID));
CREATE TABLE Dealerships (
dealer_ID VARCHAR2(5) NOT NULL,
location VARCHAR(30) NULL,
region_ID VARCHAR(5) NULL,
street_address VARCHAR2(100) NOT NULL,
city VARCHAR2(25) NOT NULL,
state VARCHAR2(15) NOT NULL,
zip VARCHAR2(5) NOT NULL,
phone VARCHAR2(10) NOT NULL,
sqft NUMERIC(8,2) NULL,
opened_date DATE,
manager VARCHAR2(100) NULL,
district_ID VARCHAR2(5) NOT NULL,
PRIMARY KEY (dealer_ID),
CONSTRAINT UC_Dealership UNIQUE (dealer_ID,district_ID));
CREATE TABLE Sales_Facts (
sale_day DATE NOT NULL,
vehicle_Code VARCHAR2(10) NOT NULL,
plan_ID VARCHAR2(10) NOT NULL,
dealer_ID VARCHAR2(5) NOT NULL,
vehicles_sold NUMBER(8,2) NOT NULL,
gross_sales_amt NUMBER(8,2) NOT NULL,
CONSTRAINT PK_Sales_Facts PRIMARY KEY (sale_day, vehicle_Code, plan_ID, dealer_ID),
CONSTRAINT FK_Sale_Day FOREIGN KEY(sale_day) References Times(sale_day),
CONSTRAINT FK_Vehicle_Code FOREIGN KEY(vehicle_Code) References Vehicles(vehicle_Code),
CONSTRAINT FK_Fin_Plan_ID FOREIGN KEY(plan_ID) References Financing_Plans(plan_ID),
CONSTRAINT FK_Dealer_ID FOREIGN KEY(dealer_ID) References Dealerships(dealer_ID));
And here is my procedure that is not returning any results:
CREATE OR REPLACE PROCEDURE Populate_Sales_Facts
AS
l_sale_day DATE;
l_vehicle_Code VARCHAR2(10);
l_plan_ID VARCHAR2(10);
l_dealer_ID VARCHAR2(5);
l_vehicles_sold NUMBER(8,2);
l_gross_sales_amt NUMBER(8,2);
CURSOR c1 IS SELECT sale_day,vehicle_Code,fp.plan_ID,d.dealer_ID,
COUNT (*) AS vehicles_sold,
SUM (s.gross_sale_price) AS gross_sales_amt
FROM Times t, Sales s, Financing_Plans fp, Dealerships d, Vehicles v
WHERE t.sale_day = s.sale_date
GROUP BY sale_day, vehicle_Code, fp.plan_ID, d.dealer_ID;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO l_sale_day, l_vehicle_Code, l_plan_ID, l_dealer_ID, l_vehicles_sold, l_gross_sales_amt;
EXIT WHEN c1%NOTFOUND;
IF l_vehicles_sold <> 0 THEN
INSERT INTO SALES_FACTS (sale_day,vehicle_Code,plan_ID,dealer_ID,vehicles_sold, gross_sales_amt)
VALUES (l_sale_day,l_vehicle_Code,l_plan_ID,l_dealer_ID,l_vehicles_sold,l_gross_sales_amt);
END IF;
END LOOP;
CLOSE c1;
END;
/
BEGIN
Populate_Sales_Facts;
END;
/
I was given the fields to populate the Sales table and they cannot be changed
per my requirements but I did fix my WHERE statement to pull sale_day from the Times table where equal to the sale_date in the Sales table because those are the only fields that linked. So I was able to get the table to populate but now instead of getting no more than 200 rows, I am getting 61065 rows of data. Here are my requirements: get every possible combination of the dimension tables’ primary keys and then the total vehicles sold and gross sales amount for each combination. If these values for Total_Vehicles_Sold and Gross_Sales_Amount for a combination are zero then don’t INSERT a row into the SALES_FACT table. Only insert rows for combinations of the four foreign key columns where there
were some vehicles sold. Maybe I am just misunderstanding the task but I feel like I am getting too many rows now.
I have to create more than 500 tables having the same columns,indexes and constraints.
In The below code I have created table called TABLE_1 like wise i have to create tables from TABLE_1 to TABLE_500....
It takes more time for creating 500 tables and indexes.
Is there any way to increase speed of table creation.?
CREATE TABLE TABLE_1 (FEATURE_ID NUMBER(*,0) NOT NULL ENABLE,
COL_1 VARCHAR2(3 CHAR),
COL_2 VARCHAR2(5 CHAR),
COL_3 NUMBER(*,0),
COL_4 NUMBER(*,0),
COL_5 VARCHAR2(5 CHAR),
COL_6 CHAR(5 BYTE),
COL_7 NUMBER(*,0),
COL_8 VARCHAR2(50 CHAR),
COL_9 NUMBER(*,0),
COL_10 VARCHAR2(20 CHAR),
GEOMETRY SDO_GEOMETRY);
CREATE BITMAP INDEX TABLE_1_DM_IDX ON TABLE_1(COL_3);
CREATE BITMAP INDEX TABLE_1_ATR_IDX ON TABLE_1(COL_4);
CREATE INDEX TABLE_1_SPATIAL_IDX ON TABLE_1(GEOMETRY)
INDEXTYPE IS MDSYS.SPATIAL_INDEX;
ALTER TABLE TABLE_1 ADD CONSTRAINT TABLE_1_PK PRIMARY KEY (FEATURE_ID);
ALTER TABLE TABLE_1 ADD CONSTRAINT TABLE_1_PK PRIMARY KEY (FEATURE_ID);
Create a PLSQL script that creates the names for you.
begin
for i in 1..500 loop
execute immediate 'CREATE TABLE TABLE_'||i|| '
(FEATURE_ID NUMBER(*,0) NOT NULL ENABLE,
COL_1 VARCHAR2(3 CHAR),
COL_2 VARCHAR2(5 CHAR),
... ';
execute immediate 'CREATE BITMAP INDEX TABLE_'||i||'_DM_IDX ON TABLE_'||i||'(COL_3)';
... etc.
end loop;
end;
/
I used the below create statement in Oracle Express 10g.
CREATE TABLE Employee( EmpNo VARCHAR2(8) NOT NULL,
EmpName VARCHAR2(30), Department VARCHAR2(30) NOT NULL,
Email VARCHAR2(30) NOT NULL, Phone VARCHAR2(15) NOT NULL,
CONSTRAINT PK_EMPNO PRIMARY KEY (EmpNo) );
And then used the following insert statement.
Insert into EMPLOYEE (EMPNO,EMPNAME,DEPARTMENT,EMAIL,PHONE)
values('E100','Chuck Coordinator','Administration','chuck#colorado.edu','3-1111');
Why am I getting?
"Ivalid Character"