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"
Related
create table EMPLOYEES
(
EMPLOYEE_ID NUMBER(6) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(25),
EMAIL VARCHAR2(25),
PHONE_NUMBER VARCHAR2(20) DEFAULT NULL,
HIRE_DATE DATE(7),
JOB_ID VARCHAR2(10),
SALARY NUMBER(8,2) DEFAULT NULL,
COMISSION_PCT NUMBER(2,2) DEFAULT NULL,
MANAGER_ID NUMBER(6) DEFAULT NULL,
DEPARTMENT_ID NUMBER(4) DEFAULT NULL
);
This is what I want to create (constraints and other information)
date datatype doesn't accept the length so no need to write date(7) just simply write date and after you run the command desc EMPLOYEES; it will show you that the length of date is by default 7.
create table EMPLOYEES(
EMPLOYEE_ID NUMBER(6) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(25) NOT NULL,
EMAIL VARCHAR2(25) NOT NULL,
PHONE_NUMBER VARCHAR2(20) DEFAULT NULL,
HIRE_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
SALARY NUMBER(8,2) DEFAULT NULL,
COMISSION_PCT NUMBER(2,2) DEFAULT NULL,
MANAGER_ID NUMBER(6) DEFAULT NULL,
DEPARTMENT_ID NUMBER(4) DEFAULT NULL
);
I was trying to create two object-relational tables and wanted to create a foreign key in the department table to reference the primary key of the employee table.
It throws "ORA-02330: datatype specification not allowed" when I try to create the DEPARTMENT TABLE when I try to create the department table.
CREATE or Replace TYPE EMP_TYPE AS OBJECT
(
EMP_ID INTEGER,
EMP_FNAME VARCHAR2(20),
EMP_LNAME VARCHAR2(25),
ADDRESS VARCHAR(30),
SALARY NUMBER (10),
JOB_TITLE VARCHAR2 (20));
CREATE TABLE EMPLOYEE OF EMP_TYPE (EMP_ID PRIMARY KEY);
CREATE OR REPLACE TYPE DEPT_TYPE AS OBJECT(
DEPT_ID INTEGER,
EMP_ID INTEGER,
DEPT_NAME VARCHAR(20),
LOCATION VARCHAR(20));
CREATE TABLE DEPARTMENT OF DEPT_TYPE (EMP_ID FOREIGN KEY REFERENCES EMPLOYEE(EMP_ID))
The last line is the one that throws the error
Wrong syntax. Should be
SQL> CREATE OR REPLACE TYPE EMP_TYPE AS OBJECT
2 (
3 EMP_ID INTEGER,
4 EMP_FNAME VARCHAR2 (20),
5 EMP_LNAME VARCHAR2 (25),
6 ADDRESS VARCHAR (30),
7 SALARY NUMBER (10),
8 JOB_TITLE VARCHAR2 (20)
9 );
10 /
Type created.
SQL> CREATE TABLE EMPLOYEE OF EMP_TYPE
2 ( EMP_ID PRIMARY KEY );
Table created.
SQL> CREATE OR REPLACE TYPE DEPT_TYPE AS OBJECT
2 (
3 DEPT_ID INTEGER,
4 EMP_ID INTEGER,
5 DEPT_NAME VARCHAR (20),
6 LOCATION VARCHAR (20)
7 );
8 /
Type created.
SQL> CREATE TABLE DEPARTMENT OF DEPT_TYPE
2 ( EMP_ID REFERENCES EMPLOYEE (EMP_ID) );
Table created.
SQL>
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;
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 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.