"ORA-00922: missing or invalid option" SQL create table error - sql

CREATE TABLE sales_invoice -- missing or invalid option
(sales_invoice_no NUMBER(6) NOT NULL
CONSTRAINT sales_invoice_no_pk PRIMARY KEY
, cust_id NUMBER(6) NOT NULL
, CONSTRAINT sales_cust_invoice_fk FOREIGN KEY (cust_id)
REFERENCES customer(cust_id))
, art_id NUMBER(6) NOT NULL
, CONSTRAINT art_invoice_fk FOREIGN KEY (art_id)
REFERENCES art_sale(art_id)
, sales_emp_id NUMBER(6) NOT NULL
, CONSTRAINT sales_invoice_emp_fk (sales_emp_id)
REFERENCES sales_emp(emp_id)
, manager_emp_id NUMBER(6)
, CONSTRAINT manager_invoice_emp_fk (manager_emp_id)
REFERENCES manager_emp(emp_id)
, sales_invoice_amount NUMBER (10)
CONSTRAINT sales_amount_check CHECK (sales_invoice_amount > 0 ));
I'm getting this error when I try to run this. I'm trying to create a table that draws from several other tables and print it as a sales invoice. Thanks in advance.

Your formatting makes it harder to spot errors I think. I reformatted your code and found an extra ")" after your "sales_cust_invoice_fk" constraint. Here is the reformatted code:
CREATE TABLE sales_invoice (
sales_invoice_no NUMBER(6) NOT NULL CONSTRAINT sales_invoice_no_pk PRIMARY KEY
cust_id NUMBER(6) NOT NULL,
art_id NUMBER(6) NOT NULL,
sales_emp_id NUMBER(6) NOT NULL,
manager_emp_id NUMBER(6),
sales_invoice_amount NUMBER (10),
CONSTRAINT sales_cust_invoice_fk FOREIGN KEY (cust_id) REFERENCES customer(cust_id),
CONSTRAINT art_invoice_fk FOREIGN KEY (art_id) REFERENCES art_sale(art_id),
CONSTRAINT sales_invoice_emp_fk (sales_emp_id) REFERENCES sales_emp(emp_id),
CONSTRAINT manager_invoice_emp_fk (manager_emp_id) REFERENCES manager_emp(emp_id),
CONSTRAINT sales_amount_check CHECK (sales_invoice_amount > 0 )
);

this is the query
CREATE TABLE sales_invoice
(sales_invoice_no NUMBER(6) NOT NULL
CONSTRAINT sales_invoice_no_pk PRIMARY KEY
, cust_id NUMBER(6) NOT NULL
, CONSTRAINT sales_cust_invoice_fk FOREIGN KEY (cust_id)
REFERENCES customer(cust_id)
, art_id NUMBER(6) NOT NULL
, CONSTRAINT art_invoice_fk FOREIGN KEY (art_id)
REFERENCES art_sale(art_id)
, sales_emp_id NUMBER(6) NOT NULL
, CONSTRAINT sales_invoice_emp_fk FOREIGN KEY (sales_emp_id)
REFERENCES sales_emp(emp_id)
, manager_emp_id NUMBER(6)
, CONSTRAINT manager_invoice_emp_fk FOREIGN KEY (manager_emp_id)
REFERENCES manager_emp(emp_id)
, sales_invoice_amount NUMBER (10)
CONSTRAINT sales_amount_check CHECK (sales_invoice_amount > 0 ));

Related

Getting ORA-00942 Error for "Table or View does not exist"

Not sure why, but referenced tabled not able to be identified. The following is the code:
CREATE TABLE Store (
Store_ID NUMBER NOT NULL,
Dept_ID NUMBER NOT NULL,
Manager_ID NUMBER NOT NULL,
Employee_ID NUMBER NOT NULL,
CONSTRAINT Store_PK PRIMARY KEY (Store_ID),
CONSTRAINT Manager_FK FOREIGN KEY (Manager_ID) REFERENCES Manager (Manager_ID),
CONSTRAINT Employee_FK FOREIGN KEY (Employee_ID) REFERENCES Employee (Employee_ID)
);
CREATE TABLE Manager
(
Manager_ID NUMBER NOT NULL,
Employee_ID NUMBER NOT NULL,
Manager_Lname VARCHAR(30) NOT NULL,
Manager_Fname VARCHAR(30) NOT NULL,
CONSTRAINT Manager_PK PRIMARY KEY (Manager_ID),
CONSTRAINT Employee_FK FOREIGN KEY (Employee_ID) REFERENCES Employee (Employee_ID)
);
CREATE TABLE OrderTable
(
Order_ID NUMBER NOT NULL,
Customer_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Tracking_Num NUMBER NOT NULL,
CONSTRAINT Order_PK PRIMARY KEY (Order_ID),
CONSTRAINT Customer_FK FOREIGN KEY (Customer_ID) REFERENCES Customer (Customer_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID),
CONSTRAINT Shipping_FK FOREIGN KEY (Tracking_Num) REFERENCES Shipping (Tracking_Num)
);
CREATE TABLE Customer
(
Customer_ID NUMBER NOT NULL,
Customer_Lname VARCHAR(30),
Customer_Fname VARCHAR(30),
Email VARCHAR(30),
Payment_Type VARCHAR(15),
CONSTRAINT Customer_PK PRIMARY KEY (Customer_ID)
);
CREATE TABLE Product
(
Product_ID NUMBER NOT NULL,
Price NUMBER NOT NULL,
Brand VARCHAR(30),
Classification VARCHAR(30),
CONSTRAINT Product_PK PRIMARY KEY (Product_ID)
);
CREATE TABLE Employee
(
Employee_ID NUMBER NOT NULL,
Title INT,
Employee_Lname VARCHAR(30),
Employee_Fname VARCHAR(30),
CONSTRAINT Employee_PK PRIMARY KEY (Employee_ID)
);
CREATE TABLE Vendor
(
Vendor_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Quantity NUMBER,
CONSTRAINT Vendor_PK PRIMARY KEY (Vendor_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID)
);
CREATE TABLE Retail
(
Retail_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Order_ID NUMBER NOT NULL,
Price NUMBER NOT NULL,
Quantity NUMBER NOT NULL,
CONSTRAINT Retail_PK PRIMARY KEY (Retail_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID),
CONSTRAINT OrderTable_FK FOREIGN KEY (Order_ID) REFERENCES OrderTable (Order_ID)
);
CREATE TABLE Shipping
(
Tracking_Num NUMBER NOT NULL,
Order_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Address VARCHAR2(50),
Shipping_Date NUMBER NOT NULL,
CONSTRAINT Shipping_PK PRIMARY KEY (Tracking_Num),
CONSTRAINT OrderTable_FK FOREIGN KEY (Order_ID) REFERENCES OrderTable (Order_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID)
);
Assuming this is the order you're creating your tables, this is likely your problem:
CREATE TABLE Store (
...
CONSTRAINT Manager_FK FOREIGN KEY (Manager_ID)
REFERENCES Manager (Manager_ID)
...
You're referencing a table in your table definition that doesn't exist yet.

I can't create table error ORA_00903

DROP TABLE Orders CASCADE CONSTRAINTS;
DROP TABLE Order_Items CASCADE CONSTRAINTS;
CREATE TABLE Orders (
ORDER_NO VARCHAR(5),
ORDER_DATE DATE CONSTRAINT BNL_ORDER_DATE_NN NOT NULL,
CUSTOMER_NAME VARCHAR(20) CONSTRAINT BNL_CUSTOMER_NAME_NN NOT NULL,
POSTAGE NUMBER(5,2) CONSTRAINT BNL_POSTAGE_NN NOT NULL,
TOTAL NUMBER(10,2) CONSTRAINT BNL_TOTAL_NN NOT NULL,
CONSTRAINT ORD_ID_PK PRIMARY KEY (ORDER_NO)
);
CREATE TABLE Order_ITEMS (
ITEM_NO VARCHAR(10),
ITEM_DES VARCHAR(20),
IETM_SIZE VARCHAR(5) CONSTRAINT ITE_ITEM_SIZE_NN NOT NULL,
ITEM_COST NUMBER(10,2) CONSTRAINT ITE_ITEM_COST_NN NOT NULL,
QTY NUMBER(5) CONSTRAINT ITE_QTY_NN NOT NULL,
ORDER_NO NUMBER(5),
CONSTRAINT ITE_ID_PK PRIMARY KEY (ITEM_NO),
CONSTRAINT ITE_ORD_FK FOREIGN KEY(ORDER_NO)
REFERENCES ORDER(ORDER_NO)
);
The Orders table is working but the Order_Items table is not working show error ORA_00903. I had change many other names, but it still show the error ORA-00903: invalid table name.
Your foreign key constraint is incorrect:
CONSTRAINT ITE_ORD_FK FOREIGN KEY(ORDER_NO)
REFERENCES ORDER(ORDER_NO)
^
You have named the referenced table ORDER instead of ORDERS.

SQL ORACLE need assistance-

SQL>
CREATE TABLE Loan(
Loan_ID NUMBER (5)
CONSTRAINT pk_Loan primary key,
Start_Date DATE NOT NULL,
End_Date DATE NOT NULL,
Copy_ID NUMBER(5),
CONSTRAINT fk_Copy_ID references Copy (Copy_ID),
customer_ID NUMBER(5),
CONSTRAINT fk_customer_ID references Customers (Customer_ID),
Evaluation NUMBER(1) NOT NULL,
check (Evaluation>=0 and Evaluation<=5>);
CONSTRAINT fk_Copy_ID references Copy (Copy_ID),
*
ERROR at line 7:
ORA-00907: missing right parenthesis
Try this please:
CREATE TABLE Loan(
Loan_ID NUMBER (5)
CONSTRAINT pk_Loan primary key,
Start_Date DATE NOT NULL,
End_Date DATE NOT NULL,
Copy_ID NUMBER(5),
customer_ID NUMBER(5),
Evaluation NUMBER(1) NOT NULL,
CONSTRAINT fk_Copy_ID foreign key (copy_id) references Copy (Copy_ID),
CONSTRAINT fk_customer_ID foreign key (customer_id) references Customers (Customer_ID),
constraint chk_evaluation check (Evaluation>=0 and evaluation<=5) );

ORA-00957 duplicate column name error, when trying to reference the same primary key with 3 foreign keys

I'm having problems with creating tables:
CREATE TABLE EMPLOYEE
(
employee_id NUMBER(5) NOT NULL UNIQUE,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
CONSTRAINT employee_pk PRIMARY KEY (employee_id)
);
CREATE TABLE PROJECT
(
project_id NUMBER(5) NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT project_pk PRIMARY KEY (PROJECT_ID),
CONSTRAINT fk_leader
FOREIGN KEY (consultant_leader, developer_leader, project_leader)
REFERENCES EMPLOYEE (employee_id, employee_id, employee_id)
);
In the last section, when I try to reference the employee's table employee_id, I'm getting ORA-00957. I think it's because the 3 different leader type foreign key references the same employee_id, but as far as I know, it should not be a problem. Is the syntax wrong?
Your immediate problem is that you need three foreign key relationships, not one with three columns.
But, there is no need to declare a primary key as being unique. So, I would recommend:
CREATE TABLE EMPLOYEE (
employee_id NUMBER(5) NOT NULL PRIMARY KEY,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
);
CREATE TABLE PROJECT (
project_id NUMBER(5) NOT NULL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT fk_leader FOREIGN KEY (consultant_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (developer_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (project_leader)
REFERENCES EMPLOYEE (employee_id)
);
You don't need to put the PRIMARY KEY constraint in-line, of course. The advantage of declaring it separately is that you can give the constraint a name to your liking.
I think you should create three distinct FK: FK_Consultant, FK_developer, FK_projleader

oracle missing right parenthesis (Line 7)

Im trying to create a constraint to ensure to columns are different values but keep getting an error on the timesheet_approved line.
create table Funtom_timesheet
(
timesheet_ID number(3) constraint timesheet_pk primary key,
timesheet_Emp number(3) constraint timesheet_Emp not null references funtom_employee,
timesheet_Wc date constraint timesheet_Wc not null,
timesheet_OT number(2) default 0,
timesheet_Approved number(3) constraint timesheet_approved_uc unique(timesheet_Approved,timesheet_Emp) constraint timesheet_approved references funtom_employee
)
;
I think multi-column constraints are not part of the column definition. Try separating them out:
create table Funtom_timesheet (
timesheet_ID number(3) constraint timesheet_pk primary key,
timesheet_Emp number(3) constraint timesheet_Emp not null references funtom_employee(??),
-------------------------------------------------------------------------------------------------------^
timesheet_Wc date constraint timesheet_Wc not null,
timesheet_OT number(2) default 0,
timesheet_Approved number(3),
constraint timesheet_approved_uc unique(timesheet_Approved,timesheet_Emp),
constraint timesheet_approved references funtom_employee(??)
-------------------------------------------------------------^
);
And fill in the column for the references