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
Related
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)
);
I'm trying to run these statements and I get an error saying no matching unique or primary key for this column-list. Can you please help me how to fix this problem?
I get the problem when I try to create table SITE:
CREATE TABLE OLMP_COUNTRY (
NOC CHAR(3),
TEAM VARCHAR2(100),
CITY VARCHAR2(100),
CONSTRAINT country_pk PRIMARY KEY(NOC)
);
CREATE TABLE ATHLETE (
ATHELTE_ID CHAR(8),
NAME VARCHAR2(100),
AGE CHAR(3),
SEX CHAR(1),
HEIGHT CHAR(3),
WEIGHT DECIMAL(3,1),
NOC CHAR(3),
CONSTRAINT athlete_pk PRIMARY KEY(ATHLETE_ID),
CONSTRAINT country_fk FOREIGN KEY(NOC) REFERENCES OLMP_COUNTRY(NOC)
);
CREATE TABLE SITE (
NOC CHAR(3),
CITY VARCHAR2(100),
SEASON VARCHAR2(20),
YEAR CHAR(4),
CONSTRAINT site_pk PRIMARY KEY(NOC),
CONSTRAINT country_fk FOREIGN KEY(CITY) REFERENCES OLMP_COUNTRY(CITY)
);
CREATE TABLE RESULTS (
RESULT_ID CHAR(8),
MEDAL CHAR(6),
ATHLETE_ID CHAR(8),
SPORT_EVENT VARCHAR2(100),
YEAR CHAR(4),
GAMES VARCHAR2(50),
CONSTRAINT results_pk PRIMARY KEY(RESULTS_ID)
);
CREATE TABLE EVENT (
SPORT_EVENT VARCHAR2(100),
SPORT VARCHAR2(50),
GAMES VARCHAR2(50)
CONSTRAINT event_pk PRIMARY KEY(SPORT_EVENT)
);
A foreign key should be referencing the primary key of the table it is referring to.
So I think you want:
CREATE TABLE SITE (
NOC CHAR(3),
CITY VARCHAR2(100),
SEASON VARCHAR2(20),
YEAR CHAR(4),
CONSTRAINT site_pk PRIMARY KEY(NOC),
CONSTRAINT site_country_fk FOREIGN KEY(NOC) REFERENCES OLMP_COUNTRY(NOC)
);
I have no idea why you are repeating CITY in both tables, but the foreign key constraint should be to the primary key. You can look up the city using JOIN. It should not be repeated.
CREATE TABLE brunch
(
br_id INT NOT NULL PRIMARY KEY,
br_name INT NOT NULL
)
CREATE TABLE employee
(
e_id INT NOT NULL PRIMARY KEY,
bdate DATE,
fname VARCHAR2(30),
lname VARCHAR2(30),
sal NUMBER,
sex VARCHAR2(1),
address VARCHAR2(50),
super_id INT REFERENCES employee(e_id)
)
ALTER TABLE brunch ADD mgr_id int REFERENCES employee(e_Id);
ALTER TABLE employee ADD b_id INT REFERENCES brunch(br_id);
CREATE TABLE client
(
c_id INT NOT NULL PRIMARY KEY,
c_name VARCHAR2(40),
c_oemail VARCHAR2(40),
c_email VARCHAR2(40),
b_id reference brunch(b_id)
)
The problem is with the last table, table client, and it give me an error message that says
ORA-00907: missing right parenthesis
but I don't see any problem with the syntax. Thank you
The last DDL should be
create table client
(
c_id int primary key,
c_name varchar2(40),
c_oemail varchar2(40),
c_email varchar2(40),
b_id references brunch(br_id)
);
where column name b_id should be br_id and
there's no keyword called reference but references
P.S. There's no need to use NOT NULL for a column defined as PRIMARY KEY(already includes NOT NULL).
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)
);
How are you all?
Basically I've written up this bit of SQL code to create a table but I keep getting the error stated in the title, any idea as to why?
Here's the code:
CREATE TABLE staff(
staffID INT NOT NULL PRIMARY KEY,
firstName VARCHAR2(20),
lastName VARCHAR2(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
salary DECIMAL (19,4),
branchID INT FOREIGN KEY REFERENCES branches(branchID)
);
Also here is the code for my 'branches' table
CREATE TABLE branches
(branchID int NOT NULL PRIMARY KEY,
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
manager VARCHAR2(20));
Any help would be appreciated!
Thank you!
A few suggestions:
First make sure that the branches table has been created.
Second, I would alter the create table code to the following:
CREATE TABLE staff(
staffID INT NOT NULL PRIMARY KEY,
firstName VARCHAR(20),
lastName VARCHAR(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
salary DECIMAL (19,4),
branchID INT,
constraint fk_branchId FOREIGN KEY (branchID) REFERENCES branches(branchID)
);
See SQL Fiddle with Demo. The syntax to create a FOREIGN KEY during table creation is:
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
);
Here is the creation of table staff1
CREATE TABLE staff
(
staffID INT NOT NULL PRIMARY KEY,
firstName VARCHAR2(20),
lastName VARCHAR2(20),
addressLine_1 VARCHAR2(30),
city VARCHAR2(15),
postcode VARCHAR2(7),
telephone VARCHAR2(15),
branchID int,
salary DECIMAL (19,4),
CONSTRAINT BRANCH_fk FOREIGN KEY(branchID) REFERENCES branches(branchID)
)
SQL> /
Table created.
Please use constraint name such that finding an error becomes easy.
create table medication (
id int not null primary key,
name varchar(20),
mudslig price number (10),
protect date not null default (getdate()),
finish date not null default (getdate()),
company proect varchre2 (20),
shelf id int,
chemistid int,
constraint shelf_fk foreign key (shelf id) refences shelf (shelf id),
constraint chemist_fk foreign key (chemistid) refences chemist (chemistid)
);
Please use constraint name such that finding an error becomes easy.