oracle number of referencing columns must match referenced columns - sql

I keep getting an error saying The number of columns in the foreign-key referenced list is not equal to the number of columns in the referenced list.
This is the line I am getting the error on.
foreign key(EID, Lastname, Firstname, Midinitial) references employee,
Does anyone know why I am getting this error?
create table employee(
EID varchar(20) primary key,
Lastname varchar(20),
Firstname varchar(20),
Midinitial char(1),
gender char(1),
street varchar(20),
city varchar(20)
);
create table works(
EID varchar(20) primary key,
Lastname varchar(20),
Firstname varchar(20),
Midinitial char(1),
company_name varchar(20),
salary numeric(5,0),
foreign key(EID, Lastname, Firstname, Midinitial) references employee,
foreign key(company_name) references company
);
create table company(
company_name varchar(20) primary key,
city varchar(20),
foreign key(city)references employee
);

You need the Primary Key from Employees only:
foreign key(EID) references employee

Related

Integrity constraint (JAVAUSER.SYS_C007925) violated - parent key not found

SQL Error
I'm not sure what I did wrong here
Here is the DDL I used to create my tables
Create Table HomeState (StateAbbreviation char(2) Primary Key,
StateName varchar(25));
Create Table Country (CountryAbbreviation char(2) Primary Key,
CountryName varchar(35));
Create Table Employee (EmployeeID Integer Primary Key NOT NULL,
FirstName varchar(20),
LastName varchar(30),
MI char(1),
HomeAddress varchar(30),
Zip char(5),
DateOfBirth date,
HireDate date,
TerminationDate date,
AnnualSalary number(20,2),
LicenseDate date,
StateAbbreviation char(2),
CountryAbbreviation char(2),
Foreign Key (StateAbbreviation) references HomeState,
Foreign Key (CountryAbbreviation) references Country);
Create Table Truck (VinNumber Integer Primary Key,
Make varchar(25),
Model varchar(30),
Year Integer,
PurchasePrice number(20,2),
LicenseNumber varchar(15));
Create Table EmployeeTruck (EmployeeID Integer,
VinNumber Integer,
Primary Key(EmployeeID,VinNumber),
Foreign Key (EmployeeID) references Employee,
Foreign Key (VinNumber) references Truck);
Create Table Accident (AccidentID Integer Primary Key,
DateOfAccident date,
AccidentDescription varchar(200),
AccidentLocation varchar(100),
EmployeeID Integer,
Foreign Key (EmployeeID) references Employee);
and here is the command i used to try and fill in the employee table
insert into employee
values ('1','brian','kim','j','adfasdf',
'1234','24-nov-1993','24-sep-1993','24-sep-1993',
'1234','24-sep-1993','as','as')
but it always gives me the error i put as the title of this question...
In the table "Employee", you say that Foreign Key (StateAbbreviation) references HomeState, and Foreign Key (CountryAbbreviation) references Country). The values you insert into Employee.HomeState and Employee.CountryAbbreviation have to exist in the tables HomeState and Country before you can insert into Employee.
Insert rows into HomeState and Country before you insert into Employee.
You have other problems, too. Here's an example.
Create Table HomeState (StateAbbreviation char(2) Primary Key,
StateName varchar(25));
insert into HomeState values ('AL', 'Alabama');
insert into HomeState values ('AM', 'Alabama');
insert into HomeState values ('AN', 'Alabama');
All those insert statements succeed. They shouldn't. In this case, StateName is also a candidate key.
Create Table HomeState (
StateAbbreviation char(2) Primary Key,
StateName varchar(25) not null unique
);
Let's take this a little further.
Create Table HomeState (
StateAbbreviation char(2) Primary Key,
StateName varchar(25) not null unique
);
Create Table Country (
CountryAbbreviation char(2) Primary Key,
CountryName varchar(35) not null unique
);
insert into HomeState values ('CA', 'California');
insert into Country values ('AF', 'Afghanistan');
insert into Employee (EmployeeID, StateAbbreviation, CountryAbbreviation)
values (-42, 'CA', 'AF');
The state "California, US" makes sense. The state "California, Afghanistan" doesn't.
An employee having no name doesn't make sense. Declare FirstName, LastName, and HireDate not null.
Bet on this: whatever nonsense your database allows will appear. It's just a matter of time.

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

ORA-02270: no matching unique or primary key for this column-list

I use iSQLPlus to create table and when i reference multiple foreign keys, they give me error ORA-02270:
ORA-02270: no matching unique or primary key for this column-list
Here is my code:
create table People
(name varchar(50),
ssn varchar(50) not null,
G# varchar(50),
primary key (ssn),
unique(ssn, G#))
create table Professor
(
name varchar(50),
ssn varchar(50) not null,
G# varchar(50),
teach_record varchar(50),
primary key (ssn),
unique (ssn, G#),
foreign key (name, ssn, G#) references People(name, ssn, G#) ON DELETE CASCADE
)
It says error on the line before the ")" in professor script, which references People(name, ssn, G#)
I couldnt figure what the problem is.
Try this:
create table People
(name varchar(50),
ssn varchar(50) not null,
G# varchar(50),
primary key (ssn),
unique(name, ssn, G#))
create table Professor
(
name varchar(50),
ssn varchar(50) not null,
G# varchar(50),
teach_record varchar(50),
primary key (ssn),
unique (ssn, G#),
foreign key (name, ssn, G#) references People(name, ssn, G#) ON DELETE CASCADE
)

oracle SQL - ORA-009007 Missing right parenthesis

I have this sql
CREATE TABLE PATIENTS
(
patientID NUMBER NOT NULL,
healthInsID NUMBER,
fname VARCHAR(20) NOT NULL,
minit VARCHAR(15),
lname VARCHAR(30) NOT NULL,
gender CHAR(2),
email VARCHAR(40),
street VARCHAR(40),
postalCode NUMBER,
city VARCHAR(20),
country VARCHAR(20),
PRIMARY KEY (patientID),
FOREIGN KEY (healthInsID) REFERENCES HEALTH_INSURANCES
ON DELETE SET NULL ON UPDATE CASCADE
);
I have no idea why I keep getting this error. I have search a lot but still nothing that can solve it. Any ideas?
Thanks
You have to specify column from HEALTH_INSURANCES table in FOREIGN KEY caluse.
You should also remove ON UPDATE CASCADE. You can use a trigger on update instead of this clause.
CREATE TABLE PATIENTS
(
patientID NUMBER NOT NULL,
healthInsID NUMBER,
fname VARCHAR(20) NOT NULL,
minit VARCHAR(15),
lname VARCHAR(30) NOT NULL,
gender CHAR(2),
email VARCHAR(40),
street VARCHAR(40),
postalCode NUMBER,
city VARCHAR(20),
country VARCHAR(20),
PRIMARY KEY (patientID),
FOREIGN KEY (healthInsID) REFERENCES HEALTH_INSURANCES (healthInsID)
ON DELETE SET NULL
);
More information about constraints

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.