Oracle DDL Error - ORA 00907: missing right parenthesis - sql

This DDL query does not work and I cannot figure out why, would appreciate help from more experienced individuals :)
When I try running it in Oracle it returns the error message ORA-00907: Missing right parenthesis
CREATE TABLE "Attendees" (
"attendee_id" number(8) PRIMARY KEY,
"attendee_name" varchar2(50),
"attendee_class" number(4),
"attendee_school" varchar2(50),
"attendee_status" varchar2(50)
);
CREATE TABLE "History" (
"history_id" number(4) PRIMARY KEY,
"history_dt" date,
"history_time" timestamp,
"history_status" varchar2(50)
);
CREATE TABLE "Event" (
"event_id" number(10) PRIMARY KEY,
"event_name" varchar2(200),
"event_location" varchar2(100),
"event_size" number(4),
"start_dt" date,
"end_dt" date,
"class_restriction" number(4),
"school_restriction" varchar2(100),
---"booking_id" number(10) references Booking(booking_id)
--- "reservation_id" number(3) references Reservation(reservation_id)
);
CREATE TABLE "Booking" (
"booking_id" number(4) PRIMARY KEY,
"booking_date" date,
"booking_cost" number(8),
"booking_status" varchar2(50),
"history_id" number(4) references History(history_id),
"event_id" number(10) references Event(event_id)
);
CREATE TABLE "Reservation" (
"reservation_id" number(3) PRIMARY KEY,
"event_id" references Event(event_id),
"attendee_id" references Attendees(attendee_id),
"reservation_status" varchar2(50)
);

Related

creating table in in cmd [duplicate]

This question already has answers here:
SQL - Missing right parenthesis
(4 answers)
Closed 7 years ago.
create table reservation
(
reservationid varchar2(6) primary key,
userid varchar2(6) foreign key references userprofile(userid),
vehicleid varchar2(6) foreign key references vehicle(vehicleid),
routeid varchar2(8) foreign key references route(routeid),
bookingdate date not null,
journeydate date not null,
driverid varchar2(6) foreign key references driver(driverid),
bookingstatus varchar2(20) not null,
totalfare number(10) not null,
boardingpoint varchar2(30) not null,
droppoint varchar2(30) not null,
vname varchar2(20) not null
);
I am getting an error:
ERROR at line 1:
ORA-00907: missing right parenthesis
You don't need the foreign key for an inline reference. This code works in SQL Fiddle:
create table vehicle (vehicleid varchar2(6) primary key);
create table userprofile (userid varchar2(6) primary key);
create table route (routeid varchar2(8) primary key);
create table driver (driverid varchar2(6) primary key);
create table reservation
(
reservationid varchar2(6) primary key,
userid varchar2(6) references userprofile(userid) ,
vehicleid varchar2(6) references vehicle(vehicleid),
routeid varchar2(8) references route(routeid),
bookingdate date not null,
journeydate date not null,
driverid varchar2(6) references driver(driverid),
bookingstatus varchar2(20) not null,
totalfare number(10) not null,
boardingpoint varchar2(30) not null,
droppoint varchar2(30) not null,
vname varchar2(20) not null
);

oracle error when second table

CREATE TABLE faculty1(
f_num varchar2(5),
f_name varchar2(20),
rank varchar2(30),
d_name char(3),
salary number(8,2),
PRIMARY KEY(f_name)
);
Table created
CREATE TABLE class1(
c_name varchar2(10),
c_time varchar2(10),
f_name varchar2(20),
c_room varchar2(10),
semester varchar2(10),
PRIMARY KEY(c_name, c_time),
FOREIGN KEY(f_name) REFERENCES faculty1(f_name)
);
error at line 8
ORA-02270: no matching unique or primary key for this column-list
What am I doing wrong?
CREATE TABLE grade1(
s_name varchar2(10),
c_name varchar2(10),
grade char(1),
PRIMARY KEY(s_name,c_name),
FOREIGN KEY(c_name) REFERENCES class1(c_name)
);
same error as before
f_name should be primary key in table faculty1 to be referenced as a foreign key in class1 table.
SQLFiddle

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

SQL Creating Table Dependencies

I've been stuck at creating tables.
Here is the code I am stuck at how to make the code work in loan_type to write office worker or non office worker
create table loaner
(
loan_id number(5) primary key,
loan_type VARCHAR2 (16),
loan_start_date date,
loan_end_date date,
)
create table office_worker
(
worker_id number(5) primary_key,
loan_id number(5) references loaner(loan_id),
worker_name varchar2(50)
)
create table nonoffice_worker
(
nonworker_id number(5) primary_key,
loan_id number(5) references loaner(loan_id),
nonworker_name varchar2(50)
);
commit;
You can't create a constrain to check that with the existing table structures. A common way to do it is like this:
create table loaner (
loan_id number(5) primary key,
loan_type VARCHAR2 (16),
loan_start_date date,
loan_end_date date,
constraint loaner_uk unique (loan_id, loan_type)
);
create table office_worker (
worker_id number(5) primary_key,
loan_id number(5),
loan_type VARCHAR2 (16),
worker_name varchar2(50),
constraint office_worker_loaner_fk foreeign key (loan_id, loan_type) references loaner (loan_id, loan_type),
constraint office_worker_loan_type_chk check (loan_type = 'OFFICE')
);
create table nonoffice_worker (
nonworker_id number(5) primary_key,
loan_id number(5),
loan_type VARCHAR2 (16),
nonworker_name varchar2(50),
constraint nonoffice_worker_loaner_fk foreeign key (loan_id, loan_type) references loaner (loan_id, loan_type),
constraint nonoffice_worker_loan_type_chk check (loan_type = 'NONOFFICE')
);
That is:
Create a redundant UNIQUE constraint in (load_id, loan_type) in the first table.
Add loan_type to the subtype tables and base the foreign key on (loan_id, loan_type).
Add a check constraint to each subtype table to ensure the correct loan_type is used.
You can also add a check constraint to your table, but you must check that column loan_type only contains desired values(office worker or non office worker), else this won't work:
alter table loaner add (CONSTRAINT chk_loan_type CHECK
(loan_type='office worker' or loan_type='non office worker'));

Strange problems while setting foreign key

Database: Oracle 10.1 on os:xp2002
I am setting foreign key in employee table but it I get an error
no matching unique or primary key for this column-list
I am using following queries for creating the tables.
Department table:
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4));
Employee table:
create table employee(
e_id number(4) ,
e_name varchar2(30) not null,
e_f_name varchar2(30) not null,
e_desg varchar2(20) not null,
e_address varchar2(50) not null,
e_phone_no number(12) not null,
e_salary number(10) not null,
e_house_rent number(6) not null,
e_conv_allow number(6) not null,
e_email varchar2(50) not null unique,
d2_name varchar2(10) not null,
e_hire_month number(2) not null,
e_hire_year number(4) not null,
constraint e_id_pk primary key(e_id),
constraint d2_name_fk foreign key(d2_name) references department(d_name))
;
Any solution please.
the foreign key must be a primary or unique key in the other table.
Make d_name PRIMARY KEY of Department table.
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4),
constraint d_name_pk primary key(d_name));
And then create the Employee table.
From what you've posted you don't seem to have a primary key on the department table. Something like:
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4)
constraint department_pk primary key(d_name));
Or after creation:
alter table department
add constraint department_pk primary key(d_name)
...