Error ORA-02291: Integrity Constraint - 1:1 instead of 1:M? - sql

I see that the parent key isn't found. In my case, both FK_PLAYERTEAM and FK_PLAYERPOSITION. I noticed that everywhere I see this error, it's an order of operations mistake. The values for position, or team id were not entered in before entering or referencing those values in the Player table. It seems in my case, that once there are 14 players entered, the same amount of teams there are, that I can no longer enter any more players.
So do I have a one to one rather than a one a to many relationship here? Please review my work! I'm new, doing this for my summer class project.
/*CITY TABLE*/
DROP TABLE CITY;
CREATE TABLE CITY (
ID int NOT NULL PRIMARY KEY,
NAME varchar2(255) NOT NULL,
STATE varchar2(255) NOT NULL,
COUNTRY varchar2(100) default 'USA'
);
/*SEASON TABLE*/
DROP TABLE SEASON;
CREATE TABLE SEASON (
ID int NOT NULL PRIMARY KEY,
YEAR varchar2(4) NOT NULL
);
/*POSITION TABLE*/
DROP TABLE POSITION;
CREATE TABLE POSITION (
ID int NOT NULL PRIMARY KEY,
NAME varchar2(255) NOT NULL,
NAME_ABR varchar2(3) NOT NULL,
CATEGORY varchar2(255) NOT NULL
);
/*TEAM TABLE*/
DROP TABLE TEAM;
CREATE TABLE TEAM (
ID int NOT NULL PRIMARY KEY,
NAME varchar2(255) DEFAULT 'FREE AGENT',
SHORT_NAME varchar2(3) NOT NULL,
COACH_NAME varchar2(255) NULL,
CITY_ID int NULL,
CONSTRAINT FK_TEAMCITY FOREIGN KEY (ID) REFERENCES CITY(ID)
);
*/PLAYER TABLE*/
DROP TABLE PLAYER;
CREATE TABLE PLAYER (
ID int NOT NULL PRIMARY KEY,
FIRSTNAME varchar2(255) NOT NULL,
LASTNAME varchar2(255) NOT NULL,
DATE_OF_BIRTH varchar2(255) NOT NULL,
PLAYER_NUM int NOT NULL,
COUNTRY varchar2(3) DEFAULT 'USA',
TEAM_ID int NULL,
CONSTRAINT FK_PLAYERTEAM
FOREIGN KEY (ID) REFERENCES TEAM(ID),
POSITION_ID int NULL,
CONSTRAINT FK_PLAYERPOSITION
FOREIGN KEY (ID) REFERENCES POSITION(ID)
);
/*MATCH TABLE*/
DROP TABLE MATCH;
CREATE TABLE MATCH (
ID int NOT NULL PRIMARY KEY,
GAME_DAY date NOT NULL,
GAME_TIME timestamp NOT NULL,
WIN_TEAM int NULL,
CONSTRAINT FK_WIN_TEAM
FOREIGN KEY (WIN_TEAM) REFERENCES TEAM(ID),
HOME_TEAM int NOT NULL,
CONSTRAINT FK_HOME_TEAM
FOREIGN KEY (HOME_TEAM) REFERENCES TEAM(ID),
AWAY_TEAM int NOT NULL,
CONSTRAINT FK_AWAY_TEAM
FOREIGN KEY (AWAY_TEAM) REFERENCES TEAM(ID),
SEASON_ID int NOT NULL,
CONSTRAINT FK_SEASON_ID
FOREIGN KEY (SEASON_ID) REFERENCES SEASON(ID)
);
/*GOALS TABLE*/
DROP TABLE GOALS;
CREATE TABLE GOALS (
ID int NOT NULL,
MINUTE int NOT NULL,
MATCH_ID int NOT NULL,
CONSTRAINT FK_GOALS_MATCH
FOREIGN KEY (MATCH_ID) REFERENCES MATCH(ID),
PLAYER_ID int NOT NULL,
CONSTRAINT FK_GOALS_PLAYER
FOREIGN KEY (PLAYER_ID) REFERENCES PLAYER(ID)
);
As i enter values here is my order.
/*CITY POPULATE*/
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('1','Springfield','MA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('2','Toledo','OH','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('3','New Orleans','LA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('4','Erie','PA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('5','Dallas','TX','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('6','Denver','CO','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('7','Reno','NV','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('8','Scranton','PA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('9','Virginia Beach','VA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('10','Green Bay','WI','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('11','Atlanta','GA','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('12','Columbus','OH','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('13','Orlando','FL','USA');
INSERT INTO CITY (ID,NAME,STATE,COUNTRY) VALUES ('14','Austin','TX','USA');
/*SEASON POPULATE*/
INSERT INTO SEASON (ID,YEAR) VALUES (1,2013);
INSERT INTO SEASON (ID,YEAR) VALUES (2,2014);
INSERT INTO SEASON (ID,YEAR) VALUES (3,2015);
INSERT INTO SEASON (ID,YEAR) VALUES (4,2016);
INSERT INTO SEASON (ID,YEAR) VALUES (5,2017);
INSERT INTO SEASON (ID,YEAR) VALUES (6,2018);
/*POSITION POPULATE*/
INSERT INTO POSITION
VALUES ('1','GoalKeeper','GK','GoalKeeper');
INSERT INTO POSITION
VALUES ('2','Sweeper','SW','Defender');
INSERT INTO POSITION
VALUES ('3','Centerback','CB','Defender');
INSERT INTO POSITION
VALUES ('4','Leftback','LB','Defender');
INSERT INTO POSITION
VALUES ('5','Rightback','RB','Defender');
INSERT INTO POSITION
VALUES ('6','Left-Wingback','LWB','Defender');
INSERT INTO POSITION
VALUES ('7','Right-Wingback','RWB','Defender');
INSERT INTO POSITION
VALUES ('8','Defending Midfielder','DM','Midfielder');
INSERT INTO POSITION
VALUES ('9','Central Midfielder','CM','Midfielder');
INSERT INTO POSITION
VALUES ('10','Attacking Midfielder','AM','Midfielder');
INSERT INTO POSITION
VALUES ('11','Left Winger','LW','Midfielder');
INSERT INTO POSITION
VALUES ('12','Right Winger','RW','Midfielder');
INSERT INTO POSITION
VALUES ('13','Center Forward','CF','Forward');
INSERT INTO POSITION
VALUES ('14','Withdrawn Forward','WF','Forward');
INSERT INTO POSITION
VALUES ('15','Striker','S','Forward');
/*TEAM POPULATE*/
INSERT INTO TEAM
VALUES ('1','Springfield FC','SFC','Dolan Mcguire','1');
INSERT INTO TEAM
VALUES ('2','Toleda Stars FC','TS','Wing Warren','2');
INSERT INTO TEAM
VALUES ('3','Louisiana AF','LAF','Zane Valentine','3');
INSERT INTO TEAM
VALUES ('4','Erie FC','EAF','Brenda Rios','4');
INSERT INTO TEAM
VALUES ('5','FC Dallas','FCD','Mark Horne','5');
INSERT INTO TEAM
VALUES ('6','Colorado Rapids','CR','Brian Cameron','6');
INSERT INTO TEAM
VALUES ('7','Reno 1868 FC','RFC','Russel Klabough','7');
INSERT INTO TEAM
VALUES ('8','Scranton Football','SF','Mark York','8');
INSERT INTO TEAM
VALUES ('9','Beach FC','BFC','Zidane Zidan','9');
INSERT INTO TEAM
VALUES ('10','Green Bay Phoenix','GBP','Shad Ramos','10');
INSERT INTO TEAM
VALUES ('11','Atlanta United FC','AFC','Gernado Martino','11');
INSERT INTO TEAM
VALUES ('12','Columbus Crew SC','CCS','Gregg Berhalter','12');
INSERT INTO TEAM
VALUES ('13','Orlando City SC','OSC','Jason Kreis','13');
INSERT INTO TEAM
VALUES ('14','Austin Aztex','AAX','Adrian Health','14');
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (1,'Ashton','Hewitt','05/07/1991',31,'USA',10,13);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (2,'Fuller','Farrell','08/12/1987',45,'USA',3,2);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (3,'Allistair','Mccullough','05/20/1993',20,'USA',5,14);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (4,'Connor','Caldwell','03/28/1996',66,'USA',14,5);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (5,'Igor','Britt','09/27/1992',45,'USA',12,11);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (6,'Cyrus','Barron','10/20/1991',41,'USA',7,1);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (7,'Quamar','Melendez','09/28/1986',20,'USA',7,15);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (8,'Dustin','Sandoval','07/05/1987',34,'USA',11,9);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (9,'Addison','Santana','12/07/1994',17,'USA',8,10);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (10,'Christian','Murphy','11/06/1986',73,'USA',5,15);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (11,'Isaiah','Reeves','03/23/1990',59,'USA',1,8);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (12,'Nero','Luna','03/14/1996',31,'USA',10,4);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (13,'Deacon','Maddox','05/10/1989',54,'USA',4,5);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (14,'Macaulay','Velez','07/13/1994',33,'USA',7,15);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (15,'Jamal','Cox','02/03/1990',44,'USA',12,11);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (16,'Axel','Dominguez','01/13/1996',15,'USA',3,5);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (17,'Jakeem','Barlow','03/09/1994',61,'USA',12,6);
INSERT INTO PLAYER (ID,FIRSTNAME,LASTNAME,DATE_OF_BIRTH,PLAYER_NUM,COUNTRY,TEAM_ID,POSITION_ID) VALUES (18,'Mufutau','Hodges','09/09/1987',69,'USA',1,9);
But I end up getting after the 14th line:
ORA-02291: integrity constraint (SQL_DFIJAZERMYRXHWLVJVYDVODHP.FK_PLAYERTEAM) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721
ORA-02291: integrity constraint (SQL_DFIJAZERMYRXHWLVJVYDVODHP.FK_PLAYERPOSITION) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721
ORA-02291: integrity constraint (SQL_DFIJAZERMYRXHWLVJVYDVODHP.FK_PLAYERPOSITION) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721
ORA-02291: integrity constraint (SQL_DFIJAZERMYRXHWLVJVYDVODHP.FK_PLAYERPOSITION) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721

You are applying an incorrect foreign key constraint, try this:
CREATE TABLE TEAM (
ID int NOT NULL PRIMARY KEY,
NAME varchar2(255) DEFAULT 'FREE AGENT',
SHORT_NAME varchar2(3) NOT NULL,
COACH_NAME varchar2(255) NULL,
CITY_ID int NULL,
CONSTRAINT FK_TEAMCITY FOREIGN KEY (CITY_ID) REFERENCES CITY(ID)
);

Related

Getting duplicate data when using the select statements

I'm getting duplicate data when trying to use the select statements, I have 9 rooms but I'm getting around 50-70 rooms when trying to display them. Help please?
I'm trying to insert data and display it using the select statement.
create table gym (
GymName VARCHAR(200) primary key,
openTime time not null,
closeTime time not null,
Price decimal not null,
);
create table Spa (
spaName VARCHAR(200) primary key,
openTime time not null,
closeTime time not null,
Price decimal not null,
);
create table customer (
CustomerID int primary key,
Firstname varchar(200) not null,
LastName varchar(200) not null,
DOB date not null check (DATEDIFF(year,DOB,getdate ()) > 18) ,
Gender char(4) not null check(Gender ='M' or Gender = 'F'),
Address varchar(200) not null default 'Jordan',
spaName VARCHAR(200) foreign key references Spa(spaName),
GymName VARCHAR(200) foreign key references gym(GymName),
);
Create table CustomerPhoNo (
CustomerID int foreign key references customer(CustomerID),
PhoneNo bigint not null,
);
create table Room (
roomNo int primary key,
Availability char(4) not null,
NoOfBeds int not null,
Rate int not null,
CheckIn date,
CheckOut date,
Price Decimal not null,
Breakfast char(4),
CustomerID int foreign key references customer(CustomerID),
);
create table LocationOfRoom (
roomNo int foreign key references Room(roomNo),
seaview char(4),
Location varchar(20) not null,
);
create table RoomType (
roomNo int foreign key references Room(roomNo),
familyRoom char(4),
doubleRoom char(4),
singleRoom char(4),
);
create table Gservice (
GymName VARCHAR(200) foreign key references gym(GymName),
Service VARCHAR(500) not null,
MachineType VARCHAR(500) not null,
);
create table PaymentCard (
CardID int primary key,
issueDate date not null,
Expirydate date not null,
CustomerID int foreign key references customer(CustomerID),
);
insert into customer values (325,'Mohammad','Alasharan','06-04-1984','M','Amman', 'BeautySpa', 'StrongBody')
insert into customer values (348,'John','Shelby','10-18-1998','M','Birmingham', 'LushLife', 'SilverGym')
insert into customer values (495,'Thomas','Hoffman','04-26-1968','M','Johannesburg', 'RelaxationTherapy', 'SilverGym')
insert into customer values (194,'Anne','Frank','07-22-2001','F','Frankfurt', 'BeautySpa', 'StrongBody')
insert into customer values (628,'Katie','Swan','02-10-1997','F','New South Wales', 'LushLife', 'FitnessHeroes')
insert into customer values (246,'Mahmoud','Alkutaifan','04-21-1994','M','Amman', 'BeautySpa', 'FitnessHeroes')
insert into customer values (864,'Karl-Heinz','Rummenigge','09-25-1955','M','Lippstadt', 'RelaxationTherapy', 'FitnessHeroes')
insert into customer values (824,'Dennis','Law','09-21-1979','M','london', 'RelaxationTherapy', 'FitnessHeroes')
insert into customer values (463,'Carles','Puyol','06-17-1973','M','madrid', 'LushLife', 'FitnessHeroes')
insert into Room values (124,'yes','1','4',null,null,'30','yes',null)
insert into Room values (135,'no','2','5','05-06-2022','05-09-2022','55','yes',495)
insert into Room values (121,'yes','1','3',null,null,'40','yes',null)
insert into Room values (139,'no','3','4','05-10-2022','05-14-2022','110','no',194)
insert into Room values (131,'no','3','3','05-18-2022','05-22-2022','130','yes',348)
insert into Room values (136,'no','4','4','04-14-2022','04-24-2022','120','yes',194)
insert into Room values (179,'yes','4','5',null,null,'95','no',null)
insert into Room values (138,'no','3','3','04-02-2022','04-06-2022','75','no',246)
insert into Room values (146,'no','3','5','05-10-2022','05-14-2022','80','yes',864)
insert into LocationOfRoom values (124,'no','south')
insert into LocationOfRoom values (135,'yes','north')
insert into LocationOfRoom values (121,'yes','south')
insert into LocationOfRoom values (139,'no','north')
insert into LocationOfRoom values (131,'no','East')
insert into LocationOfRoom values (136,'yes','west')
insert into LocationOfRoom values (179,'no','south')
insert into LocationOfRoom values (138,'no','west')
insert into LocationOfRoom values (146,'yes','north')
insert into RoomType values (124,'no','no','yes')
insert into RoomType values (135,'no','yes','no')
insert into RoomType values (121,'yes','no','no')
insert into RoomType values (139,'no','no','yes')
insert into RoomType values (131,'no','no','yes')
insert into RoomType values (136,'no','no','yes')
insert into RoomType values (179,'no','no','yes')
insert into RoomType values (138,'no','yes','no')
insert into RoomType values (146,'no','no','yes')
-- display Total number of customers who booked a single room with sea view option
select count(Firstname)
from LocationOfRoom, customer, RoomType, Room
where seaview='yes' and singleRoom='yes'
Any help would be greatly appreciated, thank you in advance!
Your FROM clause is missing the join condition for each table. In other words you are getting the cartesian product (every combination of rows) of the tables. Even distinct won't help (it will get the wrong answer). Use join conditions to link the keys of each table to each other. I'll leave this an exercise for you to try out, but this should be enough information to help you out.
I believe the solution for your problem is that you need to use DISTINCT:
SELECT COUNT(DISTINCT(Firstname))
FROM LocationOfRoom, customer, RoomType, Room
WHERE seaview='yes' AND singleRoom='yes'
I have tested it and it retrieves 9 Rooms.

Violation of PRIMARY KEY constraint 'PK__Transact__'. Cannot insert duplicate key in object 'dbo.Transactions_ID'. The duplicate key value is (1001)

When I am trying to insert execute the table and I am getting that error. And when I try to insert more rows into Employee_in I get an error
The number of columns for each row in a table value constructor must be the same
CREATE DATABASE EmployeeDatabase
USE EmployeeDatabase
CREATE TABLE Employee_In
(
EmployeeID INT NOT NULL PRIMARY KEY,
EmployeeName CHAR(30) NOT NULL,
EmployeeCountry CHAR(30)NOT NULL,
EmployeeSalary INT NOT NULL
)
USE EmployeeDatabase
GO
INSERT INTO Employee_In (EmployeeID, EmployeeName, EmployeeCountry, EmployeeSalary)
VALUES (1001, 'Sundar', 'USA', 125000),
(1002, 'Satya', 'CANADA', 120000);
SELECT * FROM Employee_In
CREATE TABLE Transactions_ID
(
TransactionID INT NOT NULL PRIMARY KEY,
EmployeeID INT FOREIGN KEY REFERENCES Employee_In(EmployeeID),
PostalDate VARCHAR(20) NOT NULL,
Amount VARCHAR(40) NOT NULL,
Description CHAR(25),
);
USE EmployeeDatabase
GO
INSERT INTO Transactions_ID (TransactionID, EmployeeID, PostalDate, Amount)
VALUES ('1001','1001','2020-04-07','20000')
Violation of PRIMARY KEY constraint 'PK__Transact__'. Cannot insert duplicate key in object 'dbo.Transactions_ID'. The duplicate key value is (1001)
This means you already have a value 1001 for the PK TransactionID in your table, most likely from and earlier insert.
INSERT INTO Transactions_ID (TransactionID, EmployeeID, PostalDate, Amount)
VALUES ('1001', '1001', '2020-04-07', '20000')
Here you are trying to insert string in columns with a datatype int... this should be:
INSERT INTO Transactions_ID (TransactionID, EmployeeID, PostalDate, Amount)
VALUES (1001, 1001, '2020-04-07', '20000')
Good luck!

SQL: "foreign key constraint fails" error message

I get an error when attempting to insert data into the songs table. I'm not sure why.
Any help would be greatly appreciated. Thanks. This is me adding details so it will let me post haha.
create table artist
(
id int primary key auto_increment,
name varchar(128) not null,
nationality varchar(128)
) ENGINE=InnoDB;
insert into artist (name, nationality) values ('Metallica', 'American');
insert into artist (name, nationality) values ('Rush', 'Canadian');
create table album
(
id int primary key auto_increment,
name varchar (128) not null,
artist int not null,
foreign key (artist) references artist(id),
genre int,
foreign key (genre) references genre(id)
) ENGINE=InnoDB;
insert into album (name, artist, genre) values ('Ride the Lightning', 1, 1);
insert into album (name, artist, genre) values ('Moving Pictures', 2, 2);
create table song
(
id int primary key auto_increment,
name varchar (128) not null,
duration varchar (128),
album int not null,
foreign key (album) references album(id)
) ENGINE=InnoDB;
insert into song (name, duration, album) values ('Fade to Black', '1 min', 1);
insert into song (name, duration, album) values ('Tom Sawyer', '2 min', 2);
create table genre
(
id int primary key auto_increment,
name varchar (128) not null,
description varchar (256)
) ENGINE=InnoDB;
insert into genre (name, description) values ('Rock', 'Lots of drums and guitars');
insert into genre (name, description) values ('Metal', 'Drums and guitars on steroids');
The order of your code is causing the problem. You need to move the
create table genre
(
id int primary key auto_increment,
name varchar (128) not null,
description varchar (256)
) ENGINE=InnoDB;
insert into genre (name, description) values ('Rock', 'Lots of drums and guitars');
insert into genre (name, description) values ('Metal', 'Drums and guitars on steroids');
Up above the "Create table album"
Here is the correct way to do it: HOW TO DO IT
This is some reasons why it did not work:
There is no table "genre" when you try to create table album:
create table album
(
id int primary key auto_increment,
name varchar (128) not null,
artist int not null,
foreign key (artist) references artist(id),
genre int,
foreign key (genre) references genre(id)
) ENGINE=InnoDB;
Here is the DEMO
Also, when you create table "genre" on time, there is another problem. You have to insert data in table "genre" to be able to insert other data.
Here is the DEMO where all works.
One is the order of the create and insert statements shouldve given you error already as table referres doesnt exists etc.
Assuming if the order is rightly implemented, as per the above error not able to insert data in songs is likely because
Your album table has a null id or id which you are inserting in songs doesnt exists in album first insert the data in album table then insert into songs table as the foreign key of songs table is checking the album table for id data

Error ORA-02291: integrity constraint

I keep getting this error when trying to populate my DB tables.
This is the code to create my tables:
CREATE table tbcustomer (
customerid char(4) not null constraint rg_customerid check (customerid between '1000' and '4999') constraint pk_customer primary key,
customername varchar2(40) not null,
customeraddress varchar2(50) null,
customercity varchar2(30) null,
customerstate char(2) null,
customerzip varchar2(10) null,
customercontact varchar2(30) null,
customerphone varchar2(12) null,
customeremail varchar2(50) null);
CREATE table tborder (
orderno number(11,0) not null constraint pk_order primary key,
orderdate date not null,
customerid char(4) not null constraint fk_customerid_tborder references tbcustomer on delete cascade);
CREATE table tbproduct (
productid char(3) not null constraint pk_product primary key constraint rg_productid check (productid between '100' and '999'),
productname varchar2(30) not null,
budgetsales number(4,0));
CREATE table tbvendor (
vendorid char(4) not null constraint pk_vendor primary key constraint rg_vendorid check (vendorid between '5000' and '9999'),
vendorname varchar2(25) not null,
vendoraddress varchar2(50) null,
vendorcity varchar2(30) null,
vendorstate char(2) null,
vendorzip varchar2(10) null);
CREATE table tbitem (
productid char(4) not null,
vendorid char(4) not null,
itemprice number(10,2) null constraint rg_itemprice check (itemprice>=0.00),
qoh number(8,0) not null,
constraint fk_productid_tbitem foreign key (productid) references tbproduct (productid),
constraint fk_vendorid foreign key (vendorid) references tbvendor (vendorid),
constraint pk_item primary key (productid, vendorid));
CREATE table tborderitem (
orderno number(11,0) not null constraint fk_orderno_tbborderitem references tborder (orderno) on delete cascade,
orderitemno char(2) not null,
productid char(3) not null,
vendorid char(4) not null,
quantity number(4,0) not null,
itemprice number(10,2) null,
constraint pk_orderitem primary key (orderno, orderitemno));
constraint fk_productid_vendorid foreign key (productid, vendorid) references tbitem (productid, vendorid) on delete cascade);
And this the code I use to populate my tables:
INSERT into tbcustomer values ('1123','Z Best','123 Main Street','Cambridge','MA','02139','Carol Jenkins','617-555-2222','jenskinssc#abc.com');
INSERT into tbcustomer values ('1234','Pop Shop','2233 Spring Street','Boston','MA','02114','Mandy Peters','617-344-1111','mpeters#def.com');
INSERT into tbcustomer values ('1667','Zoom','4545 Winter Street','Boston','MA','02112','James Hughes','617-433-3333','jhughes#zoomco.com');
INSERT into tbvendor values ('5100','Wesell','233 South Willow Street','Manchester','NH','03102');
INSERT into tbvendor values ('5200','Givin', '33 Harvard Place','Boston','MA','02211');
INSERT into tbvendor values ('5300','Z A List','4500 Summer Street','Quincy','MA','02161');
INSERT into tbproduct values ('100','Microwave','40');
INSERT into tbproduct values ('121','Toaster','30');
INSERT into tbproduct values ('434','Steamer','40');
INSERT into tbproduct values ('677','Coffee Maker','20');
INSERT into tborder values ('1','10-OCT-12','1667');
INSERT into tborder values ('2','12-OCT-12','1234');
INSERT into tborder values ('3','13-OCT-12','1667');
INSERT into tbitem values ('100','5100','55','22');
INSERT into tbitem values ('100','5200','66','20');
INSERT into tbitem values ('100','5300','70','35');
INSERT into tbitem values ('121','5100','12','20');
INSERT into tbitem values ('121','5300','15','15');
INSERT into tbitem values ('434','5100','18','35');
INSERT into tbitem values ('434','5200','25','25');
INSERT into tbitem values ('677','5100','40','20');
INSERT into tbitem values ('677','5200','46','30');
INSERT into tbitem values ('677','5300','48','20');
INSERT into tborderitem values ('1','01','100','5300','5','70');
INSERT into tborderitem values ('1','02','100','5100','5','55');
INSERT into tborderitem values ('1','03','121','5100','5','12');
INSERT into tborderitem values ('2','01','100','5200','10','65');
INSERT into tborderitem values ('2','02','677','5300','5','48');
INSERT into tborderitem values ('3','01','121','5100','5','12');
INSERT into tborderitem values ('3','02','677','5300','3','45');
INSERT into tborderitem values ('3','03','100','5100','2','55');
When I run the script I keep getting this error when inserting values to the tbitem table:
60-SQL>INSERT into tbitem values ('677','5300','48','20');
INSERT into tbitem values ('677','5300','48','20')
*
ERROR at line 1:
ORA-02291: integrity constraint (MQUIJANO.FK_PRODUCTID_TBITEM) violated -parent key not found
I also get the same error when trying to insert values into the tborderitem:
60-SQL>INSERT into tborderitem values ('1','01','100','5300','5','70');
INSERT into tborderitem values ('1','01','100','5300','5','70')
*
ERROR at line 1:
ORA-02291: integrity constraint (MQUIJANO.FK_PRODUCTID_VENDORID) violated -parent key not found
How can I fix this?
The problem may be that you do not have the same field types for your keys in the first example. Notice that in tbproduct you said:
productid char(3)
And in tbitem you said:
productid char(4)
And again in border item you said:
productid char(3)
Try changing the tbitem table to have productid with a length of 3.

Create view oracle sql

I am making a database with tv-shows and users.
These users can follow each other and are saved in the database by an UserID.
Now I wan't to create a view with the names of the users instead of the UserID's.
--Drop all tables.
DROP TABLE Series CASCADE CONSTRAINTS;
DROP TABLE User1 CASCADE CONSTRAINTS;
DROP TABLE FollowingSeries CASCADE CONSTRAINTS;
DROP TABLE FollowingUser CASCADE CONSTRAINTS;
DROP TABLE Episode CASCADE CONSTRAINTS;
DROP TABLE Watched CASCADE CONSTRAINTS;
DROP VIEW Test CASCADE CONSTRAINTS;
--Create all tables.
CREATE TABLE Series
( SeriesID NUMBER(5) primary key,
Name VARCHAR2(100) NOT NULL,
Status VARCHAR2(20),
Premiered DATE,
Genre VARCHAR2(100),
Country VARCHAR2(100),
Network VARCHAR2(100),
Runtime VARCHAR2(25)
);
CREATE TABLE User1(
UserID NUMBER(5) primary key,
Username VARCHAR2(100) NOT NULL,
Country varchar2(100),
Gender VARCHAR2(10),
Birthday VARCHAR2(100),
About_me VARCHAR2(300),
Password VARCHAR2(100) NOT NULL
);
CREATE TABLE FollowingSeries (
UserID NUMBER(5) NOT NULL,
SeriesID NUMBER(5) NOT NULL,
Ignore1 CHAR(1) DEFAULT 0,
constraint FollowingS_pk primary key(UserID, SeriesID),
constraint FollowingS_fk1 foreign key(UserID) REFERENCES User1(UserID),
constraint FollowingS_fk2 foreign key(SeriesID) REFERENCES Series(SeriesID)
);
CREATE TABLE FollowingUser (
UserID1 NUMBER(5) NOT NULL,
UserID2 NUMBER(5) NOT NULL,
constraint FollowingU_pk primary key(UserID1, UserID2),
constraint FollowingU_fk1 foreign key(UserID1) REFERENCES User1(UserID) ON DELETE SET NULL,
constraint FollowingU_fk2 foreign key(UserID2) REFERENCES User1(UserID) ON DELETE SET NULL
);
CREATE TABLE Episode(
SeriesID NUMBER(5) NOT NULL,
Season NUMBER(2) NOT NULL,
Episode NUMBER(2) NOT NULL,
Name varchar2(100) NOT NULL,
Airdate DATE,
Summary varchar2(1500),
constraint episode_pk primary key(SeriesID, Season, Episode),
constraint episode_fk foreign key(SeriesID) REFERENCES Series(SeriesID) ON DELETE SET NULL
);
CREATE TABLE Watched(
UserID NUMBER(5) NOT NULL,
SeriesID NUMBER(5) NOT NULL,
Season NUMBER(2) NOT NULL,
Episode NUMBER(2) NOT NULL,
constraint watched_pk primary key(UserID, SeriesID, Season, Episode),
constraint watched_fk1 foreign key(UserID) REFERENCES User1(UserID),
constraint watched_fk4 foreign key(SeriesID, Season, Episode) REFERENCES Episode(SeriesID, Season, Episode)
);
--Add users.
INSERT INTO User1 VALUES (1, 'StefPhilipsen', 'Netherlands', 'Male', TO_DATE('02-01-1994','DD-MM-YYYY'), 'Ik ben Stef', 'abcd1234');
INSERT INTO User1 VALUES (2, 'Dorothy142', 'America', 'Female', TO_DATE('01-10-1963','DD-MM-YYYY'), 'I love mountainbiking', 'Oow6sai4Ie');
INSERT INTO User1 VALUES (3, 'Rudo12', 'England', 'Male', TO_DATE('05-6-1985','DD-MM-YYYY'), 'I watched Breaking Bad and a lot more', 'Oph9Ge2yai');
INSERT INTO User1 VALUES (4, 'JohnSmith', 'England', 'Male', TO_DATE('27-9-1945','DD-MM-YYYY'), 'I like potatoes.', 'phiShaip0c');
INSERT INTO User1 VALUES (5, 'EulaWGibson', 'America', 'Female', TO_DATE('12-8-1990','DD-MM-YYYY'), 'I''m Eula. I live in massachusetts and I''m happily married with my husband Hank.', 'EiM5wii2am8');
INSERT INTO User1 VALUES (6, 'Pedro', 'Spain', 'Male', TO_DATE('12-10-1945','DD-MM-YYYY'), 'I love watching Arrow.', 'Gith0yaiw');
INSERT INTO User1 VALUES (7, 'TravisC', 'England', 'Male', TO_DATE('12-7-1970','DD-MM-YYYY'), 'My favorite TV-shows are Modern Family and Dexter.', 'gie9aiPh2Ii');
INSERT INTO User1 VALUES (8, 'DonellaScott', 'America', 'Female', TO_DATE('31-1-1996','DD-MM-YYYY'), 'Living the life.', 'Gued1996');
--Who follows which series.
INSERT INTO FollowingSeries(UserID, SeriesID) VALUES (1,1);
INSERT INTO FollowingSeries(UserID, SeriesID) VALUES (1,2);
INSERT INTO FollowingSeries(UserID, SeriesID) VALUES (1,3);
INSERT INTO FollowingSeries VALUES (1,4,1);
--Who follows who.
INSERT INTO FollowingUser VALUES(1,2);
INSERT INTO FollowingUser VALUES (2,3);
INSERT INTO FollowingUser VALUES (3,4);
INSERT INTO FollowingUser VALUES (1,3);
INSERT INTO FollowingUser VALUES (5,8);
INSERT INTO FollowingUser VALUES (5,6);
INSERT INTO FollowingUser VALUES (6,5);
-- Which episodes are watched by whom.
INSERT INTO Watched VALUES(1, 4, 1, 1);
INSERT INTO Watched VALUES(1, 4, 1, 2);
INSERT INTO Watched VALUES(1, 4, 1, 3);
INSERT INTO Watched VALUES(1, 4, 1, 4);
INSERT INTO Watched VALUES(1, 4, 1, 5);
INSERT INTO Watched VALUES(1, 4, 1, 6);
INSERT INTO Watched VALUES(1, 4, 1, 7);
This is what I tried but then I get an error message
--Create view
CREATE VIEW follownaam AS
SELECT followerUser.Username AS Follower, followingUser.Username AS Following
FROM FollowingUser AS fu
INNER JOIN User1 followerUser ON(fu.UserID1 = followerUser.Username)
INNER JOIN User1 followingUser ON(fu.UserID2 = followingUser.Username)
WHERE fu.UserID1 = 1;
[Err] ORA-00911: invalid character
The table I have exists of 2 UserID's. But now I want exactly the same table, but this view must contain the Usernames of these UserID's.
This should work (although i don't understand why You want a view only for user with ID = 1).
CREATE VIEW follownaam AS
SELECT
followerUser.Username AS Follower, followingUser.Username AS Following
FROM FollowingUser fu
INNER JOIN User1 followerUser ON(fu.UserID1 = followerUser.UserID)
INNER JOIN User1 followingUser ON(fu.UserID2 = followingUser.UserID)
WHERE fu.UserID1 = 1;
I changed Username to UserId because it made much more sense.
Edit:
Here's sqlfiddle i created to test it and it works.