Where is the wrong logic in my Create Table statements? - sql

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.

Related

I have a problem with the referencing of this Script

There is an error with the referencing of table booking to person and driver.
ERROR at line 17:
ORA-02270: no matching unique or primary key for this column-list
I've tried almost everything please help me.
alter session set NLS_DATE_FORMAT='DD/MM/YYYY';
DROP TABLE CARSERVICE;
DROP TABLE DRIVERBEN;
DROP TABLE BENEFITS;
DROP TABLE FOLLOWUP;
DROP TABLE INCIDENT;
DROP TABLE DSESSION;
DROP TABLE TRAINING;
DROP TABLE DRIVERINS;
DROP TABLE BOOKING;
DROP TABLE DRIVER;
DROP TABLE PERSON;
CREATE TABLE PERSON
(
PID CHAR(6) PRIMARY KEY,
PName VARCHAR(20),
PDOB DATE,
Sex CHAR(1),
PMOBILE CHAR(12)
);
CREATE TABLE DRIVER
(
DID CHAR(6),
DGrade CHAR(1),
DCarId CHAR(6),
DLicense CHAR(8),
DStart DATE,
DIPlan CHAR(1),
DSession CHAR(6),
DNRIC CHAR(12),
PID CHAR(6),
PRIMARY KEY (DID, DCarId),
FOREIGN KEY (DID) references PERSON(PID)
);
CREATE TABLE BOOKING
(
BookingID CHAR(6),
PickLoc VARCHAR(250),
DropLoc VARCHAR(250),
TripRating NUMBER(1),
RideFare NUMBER(*,1),
TollOther NUMBER(*,1),
TDate DATE,
TTime VARCHAR(5),
BStatus CHAR(1),
Payment VARCHAR(12),
IncRep CHAR(1),
PID CHAR(6),
DID CHAR(6),
PRIMARY KEY(BookingID,PID,DID),
FOREIGN KEY (DID) references Driver(DID),
FOREIGN KEY (PID) references PERSON(PID)
);
CREATE TABLE DRIVERINS
(
DIID CHAR(6) PRIMARY KEY,
PAmount NUMBER(*,1),
PDt DATE,
FOREIGN KEY (DIID) references DRIVER (DID)
);
CREATE TABLE TRAINING
(
TID CHAR(6) PRIMARY KEY,
TrainingPrg VARCHAR(50),
PrgSession VARCHAR2(10)
);
CREATE TABLE DSESSION
(
SID CHAR(6) PRIMARY KEY,
SDate DATE,
TID CHAR(6),
FOREIGN KEY (SID) references DRIVER(DID),
FOREIGN KEY (TID) references TRAINING(TID)
);
CREATE TABLE INCIDENT
(
INCID CHAR(6) PRIMARY KEY,
RIncident VARCHAR(30),
IncDate DATE,
FOREIGN KEY (INCID) references BOOKING(BookingID)
);
CREATE TABLE FOLLOWUP
(
FollowID CHAR(6) PRIMARY KEY,
IncPIC VARCHAR(30),
FollowUpDt VARCHAR2(50),
IncStatus CHAR(1),
FollowUpDate DATE,
FOREIGN KEY (FollowID) references INCIDENT(INCID)
);
CREATE TABLE BENEFITS
(
BID CHAR(6) PRIMARY KEY,
CMedBenefit NUMBER(*,1),
OBetterCars NUMBER(*,1),
PrepRet NUMBER(*,1)
);
CREATE TABLE DRIVERBEN
(
DBID CHAR(6),
CMedBenefit CHAR(1),
OBetterCars CHAR(1),
PrepRet CHAR(1),
FOREIGN KEY (DBID) references DRIVER(DID)
);
CREATE TABLE CARSERVICE
(
CarID CHAR(6) PRIMARY KEY,
CarType VARCHAR(20),
ServRem VARCHAR(250),
CarServDate DATE,
NextServD DATE,
FOREIGN KEY (CarID) references DRIVER(DCarId)
);
You have these definitions:
CREATE TABLE DRIVER (
. . .
PRIMARY KEY (DID, DCarId),
. . .
);
CREATE TABLE BOOKING (
. .
FOREIGN KEY (DID) references Driver(DID),
. . .
);
These are inconsistent. The primary key for Driver has two component keys. The foreign key reference only uses one. You need to reference both.

Error Code 1005. Can't create table : "takes" and "Teaches"

Would appreciate help. Can not create the "Teaches" and "takes" table.
CREATE DATABASE university;
use university;
CREATE TABLE classroom(
building VARCHAR(20),
room_number INT,
capacity INT,
PRIMARY KEY (building,room_number)
) ;
CREATE TABLE Sales(
item VARCHAR(20),
color VARCHAR(20),
clothes_size VARCHAR(20),
quantity INT,
PRIMARY KEY (item,color,clothes_size,quantity)
);
CREATE TABLE Department (
dept_name VARCHAR(20),
building VARCHAR(20),
budget INT,
FOREIGN KEY (building) references classroom(building),
PRIMARY KEY (dept_name,building,budget)
);
CREATE TABLE Course (
course_id VARCHAR(20)PRIMARY KEY,
title VARCHAR(20),
dept_name VARCHAR(20),
credits INT,
FOREIGN KEY (dept_name) references Department(dept_name)
);
CREATE TABLE Instructor(
ID VARCHAR(20) PRIMARY KEY,
neme VARCHAR(20),
dept_name VARCHAR(20),
salary INT,
FOREIGN KEY (dept_name) references Department(dept_name)
);
CREATE TABLE section(
course_id VARCHAR(20),
sec_id INT,
semester VARCHAR(20),
year1 INT,
building1 VARCHAR(20),
room_number INT,
time_slot_id CHAR,
FOREIGN KEY (course_id) REFERENCES Course(course_id),
FOREIGN KEY (building1,room_number) references classroom(building,room_number),
PRIMARY KEY(course_id,sec_id,semester,year1)
);
CREATE TABLE Teaches(
ID VARCHAR(20),
course_id VARCHAR(20),
sec_id VARCHAR(20),
semester VARCHAR(20),
year1 INT,
FOREIGN KEY (ID) references Instructor(ID),
FOREIGN KEY (sec_id) references section(sec_id),
FOREIGN KEY (semester,year1) references section(semester,year1),
FOREIGN KEY (course_id) references course(course_id),
PRIMARY KEY (ID,course_id,sec_id,year1,semester)
);
CREATE TABLE student(
ID VARCHAR(20) PRIMARY KEY,
name1 VARCHAR(20),
dept_name VARCHAR(20),
tot_cred INT);
CREATE TABLE Takes(
ID VARCHAR(20),
course_id VARCHAR(20),
sec_id INT,
semester VARCHAR(20),
year1 INT,
grade char,
FOREIGN KEY (ID) references student(ID),
FOREIGN KEY (sec_id) references section(sec_id),
FOREIGN KEY (semester) references section(semester),
FOREIGN KEY (year1) references section(year1),
FOREIGN KEY (course_id) references course(course_id),
PRIMARY KEY(ID,sec_id,semester,year1,course_id)
);
Foreign key references need to have the same types as the primary keys they refer to. So consider:
CREATE TABLE Department (
dept_name VARCHAR(20),
building VARCHAR(20),
budget INT,
FOREIGN KEY (building) references classroom(building),
PRIMARY KEY (dept_name,building,budget)
);
CREATE TABLE Instructor (
ID VARCHAR(20) PRIMARY KEY,
neme VARCHAR(20),
dept_name VARCHAR(20),
salary INT,
FOREIGN KEY (dept_name) references Department(dept_name)
);
The primary key for Department has three components -- dept_name, building, budget. However, the reference has only one component. They cannot match; the number is not correct, much less the types.
I would recommend that you use auto-incremented ids for your primary keys. You can also specify uniqueness constraints, if you like. So for this small example:
create table buildings (
building_id int auto_increment primary key,
building varchar(20),
constraint unq_buildings_building unique(building)
);
create table classrooms (
classroom_id int auto_increment primary key,
building_id int,
room_number int,
capacity int,
constraint fk_classrooms_building_id foreign key (building_id) references buildings (building_id)
) ;
create table Departments (
department_id int auto_increment primary key,
dept_name varchar(20),
building_id int,
budget INT,
constraint fk_departments_building_id foreign key (building_id) references buildings(building_id)
);
create table instructors (
instructor_id int auto_increment primary key,
id varchar(20) unique,
name varchar(20),
department_id int,
salary int,
foreign key instructors_department_id foreign key (department_id) references departments (department_id)
);
This is just a small sample of your tables. But the following are some rules that I follow:
Table names are plural.
They have a primary key which is the singular form followed by _id (or Id).
Foreign key references are only to primary keys.
To the extent possible, foreign keys and primary keys have the exact same name.
Explicit constraints are given names. The names follow a very precise naming convention.
I also added a buildings table. It is referenced in at least two places, so it seems worthy of being its own entity.
This should give you some ideas on how to build your database.

ORA-00904: "BRANCH_ID": invalid identifier

Why is the create tables not allowing me to add the staff tables, the CONSTRAINT seem logical to me.
CREATE TABLE branch
(
Branch_ID VARCHAR(2),
Branch_Name VARCHAR(20),
Branch_Address VARCHAR(40),
Branch_Postcode VARCHAR(15),
Branch_Telephone NUMBER(15),
Branch_email VARCHAR(40),
Branch_Fax NUMBER(15),
PRIMARY KEY ( Branch_ID )
);
CREATE TABLE staff
(
Staff_ID 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),
CONSTRAINT BRANCH_fk FOREIGN KEY(Branch_ID ) REFERENCES branch(Branch_ID )
);
ORA-00904: "BRANCH_ID": invalid identifier
I think you forgot to add the Branch_ID field.
You are referencing this one to be your foreign key at the Staff table, but you didn't define it in your staff table yet.
Change staff table definition to:
CREATE TABLE staff
(
Staff_ID 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),
Branch_ID VARCHAR2(2),
CONSTRAINT BRANCH_fk FOREIGN KEY(Branch_ID) REFERENCES branch(Branch_ID)
);
About Gordon Linoff's comment, check the link below. I modified my answer to match the 'best-practice'.
Difference VARCHAR and VARCHAR2

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

Oracle SQL error ORA-00907: missing right parenthesis

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.