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

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.

Related

ERROR: there is no unique constraint matching given keys for referenced table

I am getting an error when i create my tabels.
The problem is that AssCode is not unique, so i can set it to unique, the combination of courseCode and AssCode is unique, thats why they are set as primary keys. I am using postgressql
here is the error:
ERROR: there is no unique constraint matching given keys for referenced table "assignments"
SQL state: 42830
here is my code:
CREATE TABLE Teachers (
BSN int primary key,
Surname varchar(40) NOT NULL,
Name varchar(40) NOT NULL
);
CREATE TABLE Courses (
CourseCode varchar(10) primary key,
Name varchar(20) NOT NULL
);
CREATE TABLE Assignments (
CourseCode varchar(10) REFERENCES Courses ON DELETE CASCADE,
AssCode varchar(10),
primary key(CourseCode,AssCode),
DependOn varchar(10),
Year date,
week int
);
CREATE TABLE WorkOn (
BSN int REFERENCES Teachers(BSN),
CourseCode varchar(10) REFERENCES Assignments(CourseCode),
AssCode varchar(10) REFERENCES Assignments(AssCode),
primary key (CourseCode,BSN,AssCode)
);
I found the answer:
CREATE TABLE WorkOn (
BSN int primary key REFERENCES Teachers(BSN),
CourseCode varchar(10),
AssCode varchar(10),
foreign key (CourseCode, AssCode) references Assignments (CourseCode, AssCode)
);

Cross Referencing Table Value for Another Value to allow Insert

How do I look at one table and use it to check another table for data integrity?
I have two SQL Tables.
One that is a person table
CREATE TABLE PERSON
(
ID INT IDENTITY(10000,1) NOT NULL,
Firstname VARCHAR(15),
Lastname VARCHAR(25) NOT NULL,
Birthdate DATE,
Gender VARCHAR(1),
CHECK ( GENDER IN ('M', 'F')),
Street VARCHAR(50),
City VARCHAR(15),
State VARCHAR(2),
CHECK (State IN ('FL','GA','PA')),
Zip INT,
Phone VARCHAR(10),
Employee VARCHAR(1),
CHECK ( Employee IN('Y','N')),
Member VARCHAR(1),
CHECK ( Member IN('Y','N')),
CHECK (Member IN ('Y') or Employee IN ('Y')),
CONSTRAINT PERSON_PK PRIMARY KEY (ID));
And Employee Table
CREATE TABLE EMPLOYEE
(
ID INT NOT NULL,
Datehired DATE DEFAULT GETDATE(),
Status VARCHAR(1),
CHECK ( Status IN ('F','C')),
Position VARCHAR(25),
EmpType VARCHAR(25),
CONSTRAINT EMPLOYEE_PK PRIMARY KEY (ID),
CONSTRAINT EMPLOYEE_PERSON_FK FOREIGN KEY (ID) REFERENCES PERSON);
Let's say someone isn't an employee. I can still insert them into the Employee Table.
INSERT INTO EMPLOYEE
(ID, Status, Position, EmpType)
VALUES
('10000','C','Teaching Classes','Instructor');
How Do I prevent this from happening.
One method is to have a redundant key:
alter table person
contraint unq_person_id_employee
unique (id, employee);
Then add a computed column to employee:
alter table employee add employee as ('Y') persisted;
Finally, add the constraint:
alter table employee
add constraint fk_employee_person
foreign key (id, employee) references person(id, employee);
Now, you are guaranteed that only employees are in the Employee table.

Oracle SQL foreign key data not being displayed when selected

I'm trying to do a project with Oracle SQL and for some reason the foreign keys won't work and I can't see why, there's no errors occurring it just won't display the data when I use SELECT, any help would be greatly appreciated, the code below is the one i'm using
CREATE TABLE Customer(
customerName VARCHAR(10) NOT NULL,
street VARCHAR(120) NOT NULL,
customerCity VARCHAR(10) NOT NULL,
PRIMARY KEY (customerName) );
CREATE TABLE Branch(
branchName VARCHAR(10) NOT NULL,
branchCity VARCHAR(10) NOT NULL,
assets INT NOT NULL,
PRIMARY KEY (branchName) );
CREATE TABLE Deposit(
accountNumber INT NOT NULL,
customerName VARCHAR(10),
branchName VARCHAR(10),
balance DECIMAL(6,2) DEFAULT'0',
PRIMARY KEY (accountNumber),
CONSTRAINT Depositbranch
FOREIGN KEY (branchName)
REFERENCES Branch (branchName)
ON DELETE CASCADE,
CONSTRAINT Depositcustomer
FOREIGN KEY (customerName)
REFERENCES Customer (customerName)
ON DELETE CASCADE);
CREATE TABLE Loan(
loanNumber INT NOT NULL,
customerName VARCHAR(10),
branchName VARCHAR(10),
amount DECIMAL(6,2) DEFAULT'0',
PRIMARY KEY (loanNumber),
CONSTRAINT branchNam
FOREIGN KEY (branchName)
REFERENCES Branch (branchName)
ON DELETE CASCADE,
CONSTRAINT customerNam
FOREIGN KEY (customerName)
REFERENCES Customer (customerName)
ON DELETE CASCADE);
The only statement in which you insert into deposit is the following,
INSERT INTO Deposit(accountNumber, balance) VALUES ('1', '100');
where you are only inserting into accountNumber and balance.
The column customerName has no default value, so in this case it will be null.
In fact, if you query all the columns of the table, you have:
SQL> SELECT * FROM deposit;
ACCOUNTNUMBER CUSTOMERNA BRANCHNAME BALANCE
------------- ---------- ---------- ----------
1 100
SQL>

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.

oracle number of referencing columns must match referenced columns

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