oracle missing right parenthesis (Line 7) - sql

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

Related

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

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