SQL ORACLE need assistance- - sql

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) );

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.

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

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

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 ));

SQL ORA-02256: number of referencing columns must match referenced columns

these are my tables :
CREATE TABLE EMPLOYEE(
Emp_id number(4),
Emp_name varchar2(30),
Emp_gender varchar2(1),
Status varchar2(30),
Years_service number(4),
Primary Key (emp_id)
);
CREATE TABLE ACTIVITY(
Act_id number(4),
Description varchar2(30),
Category_code varchar2(1),
Primary Key(Act_id)
);
CREATE TABLE ALLOCATION(
Emp_id number(4) NOT NULL,
Act_id number(4) NOT NULL,
Hourly_rate number(5,2) NOT NULL,
Primary Key (Act_id, Emp_id),
Foreign Key (Act_id) REFERENCES ACTIVITY,
Foreign Key (Emp_id) REFERENCES EMPLOYEE,
CONSTRAINT CK_ALLOCATION_RATE CHECK(Hourly_rate > 0 and Hourly_rate<300)
);
CREATE TABLE ACTION(
Week_no number(2) NOT NULL,
Hrs_worked number(4,1) NOT NULL,
Act_id number(4) NOT NULL,
emp_id number(4) NOT NULL,
Primary Key (Week_no, Act_id, emp_id),
Foreign Key (Act_id) References Allocation,
Foreign Key (emp_id) References Allocation
);
Table employee, activity and allocation are created perfectly. but when i try to create table action and referencing the foreign key to table allocation, it says: ORA-02256: number of referencing columns must match referenced columns.
When using references I think it is a good idea to include the columns in both tables, even if the syntax makes this unnecessary (the default is to the primary key).
You have a composite primary key, so you need a composite key for the foreign reference:
CREATE TABLE ACTION (
Week_no number(2) NOT NULL,
Hrs_worked number(4,1) NOT NULL,
Act_id number(4) NOT NULL,
emp_id number(4) NOT NULL,
Primary Key (Week_no, Act_id, emp_id),
Foreign Key (Act_id, emp_id) References Allocation(Act_id, emp_id)
);