Error 1136 in mysql - mysql

i keep getting the error 1136 in mysql, i need help, what's wrong with this code?
CREATE table Artist (
ArtistID INT,Salary varchar (20),Contract_End_Date date,Trackname varchar (20),
Artistname varchar (15),
Fname varchar (20),
Lname varchar (20),
Birthday date ,
PRIMARY KEY(ArtistID),
FOREIGN KEY (Salary) REFERENCES Contract(Salary),
FOREIGN KEY (Contract_End_Date) REFERENCES Contract(Contract_End_Date),
FOREIGN KEY (Trackname) REFERENCES Track(Trackname));
INSERT INTO Artist(ArtistID, Artistname, Fname, Lname, Birthday, Salary, Contract_End_Date, Trackname) VALUES (
'1','JM','John','Mcfierceson','1978-05-20','$100000','2017-05-08','Cries by the Ocean',
'2','Ray','Ray','Grueson','1990-07-10','$500000','2017-09-12','Jumping Jacks',
'3','Shiin','Charlie','Shiin','1989-02-12','$700000','2020-12-17','I can feel my head',
'4','King','Bobby','Naval','1978-09-24','$7878787','2014-10-11','Rain',
'5','Yellowman','Chris, Yellow','1984-11-11','$8000000','2014-09-08','Falling',
'6','Sting','Karl','Shakur','1967-10-06','$5600000','2014-05-15','X',
'7','Kboy','Kendrick','Maine','1990-12-25','$8099999','2021-09-12','Trick');
CREATE table Contract (
Contractcode varchar (20), Artistname varchar(15),
Contract_start_Date date,
Contract_End_Date date,
Salary varchar(20),
PRIMARY KEY(Contractcode),
FOREIGN KEY (Artistname) REFERENCES Artist(Artistname));
INSERT INTO Contract VALUES (
'1004JM', 'JM', '2011-05-08', '2017-05-08', '$100000 ',
'2424RG', 'Ray', '2013-09-12', '2017-09-12', '$500000',
'3446SC', 'Shiin', '2010-12-17', '2020-12-17', '$700000',
'9999BN', 'King', '1990-10-11', '2014-10-11', '$7878787',
'2546CY', 'Yellowman', '2000-09-08', '2014-09-08', '$8000000',
'4446KS', 'Sting', '1980-05-15', '2014-05-15', '$5600000',
'5454KM', 'Kboy', '2010-09-12', '2021-09-12', '$8099999');
CREATE table Track (
Trackname varchar (20),
Artistname varchar (15),
Tracktype varchar (20),
Tracklength int ,
PRIMARY KEY(Trackname),
FOREIGN KEY (Artistname) REFERENCES Artist(Artistname));
INSERT INTO Track VALUES (
'Cries by the Ocean', 'Jumping Jacks', 'I can feel my head', 'Rain', 'Falling', 'X', 'Trick',
'JM', 'Ray', 'Shiin', 'King', 'Yellowman', 'Sting', 'Kboy',
'Rock', 'Rock', 'Indie', 'RnB', 'Rock', 'Rock', 'Rock',
'4', '5', '3', '3', '5', '5', '5');

Mysql Error 1136 means Column count doesn't match value count.
You seem to be inserting multiple rows with a single insert statement.
Each row of data should be in its own set of parenthesis. And each set of parenthesis should be separated by a comma. Something like this:
INSERT INTO artist
(artistid, artistname, fname, lname, birthday, salary, contract_end_date, trackname)
VALUES
('1', 'JM', 'John', 'Mcfierceson', '1978-05-20', '$100000', '2017-05-08', 'Cries by the Ocean'),
('2', 'Ray', 'Ray', 'Grueson', '1990-07-10', '$500000', '2017-09-12', 'Jumping Jacks'),
('3', 'Shiin', 'Charlie', 'Shiin', '1989-02-12', '$700000', '2020-12-17', 'I can feel my head'),
('4', 'King', 'Bobby', 'Naval', '1978-09-24', '$7878787', '2014-10-11', 'Rain'),
('5', 'Yellowman', 'Chris, Yellow', '1984-11-11', '$8000000', '2014-09-08', 'Falling'),
('6', 'Sting', 'Karl', 'Shakur', '1967-10-06', '$5600000', '2014-05-15', 'X'),
('7', 'Kboy', 'Kendrick', 'Maine', '1990-12-25', '$8099999', '2021-09-12', 'Trick');
INSERT Syntax (Documentation)

You are trying to plug more data than you have fields for. Try this:
CREATE table Track ( Trackname varchar (20),
Artistname varchar (15),
Tracktype varchar (20),
Tracklength int ,
PRIMARY KEY(Trackname),
FOREIGN KEY (Artistname)
REFERENCES Artist(Artistname));
INSERT INTO Track VALUES ( 'Cries by the Ocean', 'JM', 'Rock', '4'),
('Jumping Jacks', 'Ray', 'Rock', '5');
Enclose each row in a set of (), one row of data at a time.

Related

SQL Error Code: 1452 when inserting data into table with FK referring to another table

I am currently trying to populate my tables with data via a SQL script and have been having issues getting my FKs cooperate while populating my book table. I've read the other threads on the issue and have tried ON DELETE CASCADE and the other suggestions of populating the FKs first didn't work for me either. I also saw you can override the FK check however that seems counterproductive to making a working database.
I've attached my scripts and create.sql runs fine but insert.sql as I said gets stuck on the last command.
Thanks for your time!
-----
create.sql
-----
CREATE TABLE Dept (
Dept_ID INT(2) NOT NULL,
Dept_Name CHAR(16) NOT NULL,
Dept_Floor INT(1) NOT NULL,
PRIMARY KEY (Dept_ID)
);
CREATE TABLE Publisher (
Publisher_Name VARCHAR(50) NOT NULL,
PubAddress VARCHAR(20) NOT NULL,
Phone_Number VARCHAR(15) NOT NULL,
PRIMARY KEY (Publisher_Name)
);
CREATE TABLE Book (
Book_ID INT(6) NOT NULL,
Title VARCHAR(32) NOT NULL,
Dept_ID INT(2),
Publisher_Name VARCHAR(20),
PRIMARY KEY (Book_ID),
FOREIGN KEY (Dept_ID) REFERENCES Dept(Dept_ID),
FOREIGN KEY (Publisher_Name) REFERENCES Publisher(Publisher_Name)
);
-----
insert.sql
-----
INSERT INTO Dept (Dept_ID, Dept_Name, Dept_Floor)
VALUES ('01', 'Fantasy', '1'), ('02', 'Sci-Fi', '1'), ('03', 'Fiction', '2'), ('04', 'Educational', '1'), ('05', 'Crime', '2');
INSERT INTO Publisher (Publisher_Name, PubAddress, Phone_Number)
VALUES ('Bloomsbury', '234 Hay Street', '14325673489'), ('Charles Scribner','236 Hone Way','173677654765'),('Pigeon Books','5444 North Street','17884787644'), ('Academic Press', '2354 Bridge Road', '175634598234'), ('Bantam Books', '2623 River Road', '17234648444');
INSERT INTO Book (Book_ID, Title, Dept_ID, Publisher_Name)
VALUES ('000001','Harry Potter and the Goblet of Fire', '01', 'Bloomsbury'), ('000002','A Brief History of Time', '04', 'Puffin Books'), ('000003','The Great Gatsby', 'Charles Scribners Sons', '03'), ('000004','Advances in Ecological Research', 'Academic Press', '04'), ('000005','The Case of Joe', 'Bantam Books', '05');
Your columns are not aligned and your data length is too small for your given data.
CREATE TABLE Dept (
Dept_ID INT(2) NOT NULL,
Dept_Name CHAR(16) NOT NULL,
Dept_Floor INT(1) NOT NULL,
PRIMARY KEY (Dept_ID)
);
CREATE TABLE Publisher (
Publisher_Name VARCHAR(50) NOT NULL,
PubAddress VARCHAR(20) NOT NULL,
Phone_Number VARCHAR(15) NOT NULL,
PRIMARY KEY (Publisher_Name)
);
CREATE TABLE Book (
Book_ID INT(6) NOT NULL,
Title VARCHAR(40) NOT NULL,
Dept_ID INT(2),
Publisher_Name VARCHAR(50),
PRIMARY KEY (Book_ID),
FOREIGN KEY (Dept_ID) REFERENCES Dept(Dept_ID),
FOREIGN KEY (Publisher_Name) REFERENCES Publisher(Publisher_Name)
);
INSERT INTO Dept (Dept_ID, Dept_Name, Dept_Floor)
VALUES ('01', 'Fantasy', '1'), ('02', 'Sci-Fi', '1'), ('03', 'Fiction', '2'), ('04', 'Educational', '1'), ('05', 'Crime', '2');
INSERT INTO Publisher (Publisher_Name, PubAddress, Phone_Number)
VALUES ('Bloomsbury', '234 Hay Street', '14325673489'), ('Charles Scribner','236 Hone Way','173677654765'),('Pigeon Books','5444 North Street','17884787644'), ('Academic Press', '2354 Bridge Road', '175634598234'), ('Bantam Books', '2623 River Road', '17234648444');
INSERT INTO Book (Book_ID, Title, Dept_ID, Publisher_Name)
VALUES ('000001','Harry Potter and the Goblet of Fire', '01', 'Bloomsbury'), ('000002','A Brief History of Time', '04', 'Bantam Books'), ('000003','The Great Gatsby', '03', 'Charles Scribner'), ('000004','Advances in Ecological Research', '04', 'Academic Press'), ('000005','The Case of Joe', '05', 'Bantam Books');
In your case, this error/failure means that the foreign key constraint is working correctly. The book insert is failing because you're trying to insert a book with a publisher that doesn't exist. Two of them, in fact:
('000003','The Great Gatsby', 'Charles Scribners Sons', '03')
You don't have "Charles Scribners Sons" publisher, only "Charles Scribner".
('000002','A Brief History of Time', '04', 'Puffin Books'),
You don't have Puffin Books publisher, only Pigeon Books.
Also, your book records are wrong, the last two of them. They've got Publisher name, then Dept ID...
In short, this is precisely the case when precisely this error should occur, because your data is malformed.
Create.sql and insert.sql both having data size and ordering issue.
I just corrected your query and now it will work fine for you.
---create.sql----
CREATE TABLE Dept (
Dept_ID INT NOT NULL
,Dept_Name CHAR(16) NOT NULL
,Dept_Floor INT NOT NULL
,PRIMARY KEY (Dept_ID)
);
CREATE TABLE Publisher (
Publisher_Name VARCHAR(50) NOT NULL
,PubAddress VARCHAR(20) NOT NULL
,Phone_Number VARCHAR(15) NOT NULL
,PRIMARY KEY (Publisher_Name)
);
CREATE TABLE Book (
Book_ID INT NOT NULL
,Title VARCHAR(50) NOT NULL
,Dept_ID INT
,Publisher_Name VARCHAR(50)
,PRIMARY KEY (Book_ID)
,FOREIGN KEY (Dept_ID) REFERENCES Dept(Dept_ID)
,FOREIGN KEY (Publisher_Name) REFERENCES Publisher(Publisher_Name)
);
---insert.sql---
INSERT INTO Dept (Dept_ID, Dept_Name, Dept_Floor)
VALUES ('01', 'Fantasy', '1'), ('02', 'Sci-Fi', '1'), ('03', 'Fiction', '2'), ('04', 'Educational', '1'), ('05', 'Crime', '2');
INSERT INTO Publisher (Publisher_Name, PubAddress, Phone_Number)
VALUES ('Bloomsbury', '234 Hay Street', '14325673489'), ('Charles Scribner','236 Hone Way','173677654765'),('Pigeon Books','5444 North Street','17884787644'), ('Academic Press', '2354 Bridge Road', '175634598234'), ('Bantam Books', '2623 River Road', '17234648444');
INSERT INTO Book (Book_ID, Title, Dept_ID, Publisher_Name)
VALUES ('000001','Harry Potter and the Goblet of Fire', '01', 'Bloomsbury'), ('000002','A Brief History of Time', '04', 'Charles Scribner'), ('000003','The Great Gatsby', '03','Pigeon Books'), ('000004','Advances in Ecological Research', '04','Academic Press'), ('000005','The Case of Joe', '05','Bantam Books' );

Integrity constraint (LEETO14.SYS_C00414504) violated

I can't figure out why I'm getting this error:
integrity constraint (LEETO14.SYS_C00414504) violated - parent key
not found
Everything works up until the insert values for CurrentLoan. Here is my code:
CREATE TABLE Book
(bookID INT PRIMARY KEY,
ISBN INT,
title varchar (20),
author varchar (20),
publish_year INT,
category varchar(20));
CREATE TABLE Member
(memberID INT PRIMARY KEY,
lastname varchar (20),
firstname varchar (20),
address varchar(20),
phone_number INT,
limit_ INT);
CREATE TABLE CurrentLoan
(memberID INT,
bookID INT,
loan_date DATE,
due_date DATE,
PRIMARY KEY (memberID, bookID),
FOREIGN KEY (memberID) REFERENCES Member(memberID),
FOREIGN KEY (bookID) REFERENCES Book(bookID));
INSERT INTO Book VALUES (10, 1113312336, 'The Dog', 'Jack Crow', 1990, 'fiction');
INSERT INTO Book VALUES (12, 2221254896, 'Worms', 'Jim Kan', 2013, 'fiction');
INSERT INTO Book VALUES (13, 3332546987, 'Crow', 'Jan Flo', 2000, 'fiction');
INSERT INTO Book VALUES (14, 4443456215, 'Big Dog', 'Lan Big', 1993, 'fiction');
INSERT INTO Book VALUES (15, 5552314569, 'Green Apple', 'Theo Brown', 1978, 'fiction');
INSERT INTO Book VALUES (16, 6664581631, 'Red Bean', 'Khang Nk', 2017, 'fiction');
INSERT INTO Book VALUES (17, 7771452369, 'The Dark Car', 'Author Le', 2017, 'fiction');
INSERT INTO Book VALUES (18, 8881245525, 'The Dark Room', 'Jack Se', 2017, 'fiction');
INSERT INTO Book VALUES (19, 9991123546, 'Lonely Mens', 'Geen Brown', 2014, 'fiction');
INSERT INTO Book VALUES (20, 1122112356, 'The Big Tree', 'Heart Le', 2002, 'fiction');
INSERT INTO Member VALUES (001, 'Lee', 'Nancy', 'Brownlea Drive', 1254896325, 2);
INSERT INTO Member VALUES (002, 'Le', 'Ray', '10th Street', 1234561256, 2);
INSERT INTO Member VALUES (003, 'Kan', 'Charlie', '5th Street', 1234567236, 2);
INSERT INTO Member VALUES (004, 'Brown', 'Joe', 'Elm Street', 1234567845, 2);
INSERT INTO Member VALUES (005, 'Smith', 'John', '33 East', 1234567890, 2);
INSERT INTO CurrentLoan VALUES (00123, 0236, '13-SEP-17', '14-NOV-17');
INSERT INTO CurrentLoan VALUES (00134, 2350, '13-JAN-17', '15-NOV-17');
INSERT INTO CurrentLoan VALUES (00125, 2034, '14-FEB-17', '12-MAR-17');
INSERT INTO CurrentLoan VALUES (00345, 0105, '12-OCT-17', '09-NOV-17');
INSERT INTO CurrentLoan VALUES (00311, 5012, '13-APR-17', '12-MAY-17');
INSERT INTO CurrentLoan VALUES (00213, 2051, '11-JUN-17', '02-OCT-17');
The answer is pretty self explanatory. You have foreign keys in the table, CurrentLoan. Looking at your insert statement, you're trying to add loans for members that you have not created and books you have not created.

getting duplicate primary key error

INSERT INTO REQUEST (OrderNumber, CustomerID, ItemID)
VALUES ('10011', '10', '1001'),
('10011', '10', '1007'),
('10011', '10', '1014'),
('10021', '05', '1008'),
('10021', '05', '1014'),
('10031', '07', '1014'),
('10031', '07', '1019'),
('10041', '31', '1015'),
('10041', '31', '1007'),
('10041', '31', '1024'),
('10051', '50', '1014'),
('10051', '50', '1004'),
('10051', '50', '1011'),
You need a new table that holds the many to many relationship between REQUEST and ITEMS
For example for table REQUEST and ITEMS
CREATE TABLE REQUEST (
OrderNumber Integer,
CustomerID Integer,
OrderDate DateTime,
PRIMARY KEY (OrderNumber)
);
Insert INTO REQUEST values
( 10011, 10, now()),
( 10041, 31, now());
CREATE TABLE ITEMS (
ItemID Integer,
ItemDescription Varchar(50),
PRIMARY KEY (ItemID)
);
Insert into ITEMS values
( 1001,'Screw'),
( 1007,'Rod'),
( 1015,'Nail');
This will be the new table
CREATE TABLE REQUEST_ITEMS (
OrderNumber Integer,
ItemID Integer,
OrderQuantity Integer,
PRIMARY KEY (OrderNumber, ItemID)
);
Insert into REQUEST_ITEMS values
(10011, 1001, 3),
(10011, 1007, 25);
Insert into REQUEST_ITEMS values
(10041, 1015, 10),
(10041, 1007, 15);
I hope this helps
Regards,

Create a View which spans multiple tables

I am trying to create a view in MySQL which shows a Player's name, their Guardians name, their Guardians phone number and the team the Player plays in.
I have this script which creates the database :
DROP TABLE IF EXISTS TeamCoach;
DROP TABLE IF EXISTS TeamPlayer;
DROP TABLE IF EXISTS CoachQualification;
DROP TABLE IF EXISTS PlayerGuardian;
DROP TABLE IF EXISTS PersonAddress;
DROP TABLE IF EXISTS PersonPhoneNumber;
DROP TABLE IF EXISTS Coach;
DROP TABLE IF EXISTS Player;
DROP TABLE IF EXISTS Qualification;
DROP TABLE IF EXISTS Team;
DROP TABLE IF EXISTS PhoneNumber;
DROP TABLE IF EXISTS School;
DROP TABLE IF EXISTS Person;
DROP TABLE IF EXISTS Address;
DROP TABLE IF EXISTS Guardian;
DROP FUNCTION IF EXISTS TeamSize;
CREATE TABLE Qualification (
qualificationID int NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
level int NOT NULL,
PRIMARY KEY (qualificationID)
) ENGINE=InnoDB;
CREATE TABLE Team (
teamID int NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
ageGroup varchar(30) NOT NULL,
year int NOT NULL,
PRIMARY KEY (teamID)
) ENGINE=InnoDB;
CREATE TABLE Address (
addressID int NOT NULL AUTO_INCREMENT,
number int NOT NULL,
street varchar(30) NOT NULL,
suburb varchar(30) NOT NULL,
townCity varchar(30) NOT NULL,
PRIMARY KEY (addressID)
) ENGINE=InnoDB;
CREATE TABLE PhoneNumber (
phoneNumberID int NOT NULL AUTO_INCREMENT,
number varchar(20) NOT NULL,
PRIMARY KEY (phoneNumberID)
) ENGINE=InnoDB;
CREATE TABLE School (
schoolID int NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
PRIMARY KEY (schoolID)
) ENGINE=InnoDB;
CREATE TABLE Person (
personID int NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
email varchar(30) NOT NULL,
photo varchar(30) NOT NULL,
PRIMARY KEY (personID)
) ENGINE=InnoDB;
CREATE TABLE PersonAddress (
personID int NOT NULL,
addressID int NOT NULL,
KEY personID (personID),
KEY addressID (addressID),
FOREIGN KEY (personID) REFERENCES Person (personID),
FOREIGN KEY (addressID) REFERENCES Address (addressID)
) ENGINE=InnoDB;
CREATE TABLE PersonPhoneNumber (
personID int NOT NULL,
phoneNumberID int NOT NULL,
KEY personID (personID),
KEY phoneNumberID (phoneNumberID),
FOREIGN KEY (personID) REFERENCES Person (personID),
FOREIGN KEY (phoneNumberID) REFERENCES PhoneNumber (phoneNumberID)
) ENGINE=InnoDB;
CREATE TABLE Coach (
coachID int NOT NULL PRIMARY KEY REFERENCES Person (personID),
dateBeganCoaching varchar(10) NOT NULL
) ENGINE=InnoDB;
CREATE TABLE Player (
playerID int NOT NULL PRIMARY KEY REFERENCES Person (personID),
DOB varchar(10) NOT NULL,
schoolID int NOT NULL,
KEY schoolID (schoolID),
FOREIGN KEY (schoolID) REFERENCES School (schoolID)
) ENGINE=InnoDB;
CREATE TABLE Guardian (
guardianID int NOT NULL PRIMARY KEY REFERENCES Person (personID)
)ENGINE=InnoDB;
CREATE TABLE PlayerGuardian (
guardianID int NOT NULL,
playerID int NOT NULL,
KEY guardianID (guardianID),
KEY playerID (playerID),
FOREIGN KEY (guardianID) REFERENCES Guardian (guardianID),
FOREIGN KEY (playerID) REFERENCES Player (playerID)
) ENGINE=InnoDB;
CREATE TABLE TeamPlayer (
teamID int NOT NULL,
playerID int NOT NULL,
KEY teamID (teamID),
KEY playerID (playerID),
FOREIGN KEY (teamID) REFERENCES Team (teamID),
FOREIGN KEY (playerID) REFERENCES Player (playerID)
) ENGINE=InnoDB;
CREATE TABLE TeamCoach (
teamID int NOT NULL,
coachID int NOT NULL,
KEY teamID (teamID),
KEY coachID (coachID),
FOREIGN KEY (teamID) REFERENCES Team (teamID),
FOREIGN KEY (coachID) REFERENCES Coach (coachID)
) ENGINE=InnoDB;
CREATE TABLE CoachQualification (
coachID int NOT NULL,
qualificationID int NOT NULL,
KEY coachID (coachID),
KEY qualificationID (qualificationID),
FOREIGN KEY (coachID) REFERENCES Coach (coachID),
FOREIGN KEY (qualificationID) REFERENCES Qualification (qualificationID)
) ENGINE=InnoDB;
DELIMITER //
CREATE FUNCTION TeamSize(Team varchar(30))
RETURNS int
DETERMINISTIC CONTAINS SQL
BEGIN
DECLARE Size int;
SELECT COUNT(*) INTO Size FROM ((
SELECT * FROM TeamPlayer WHERE teamID=(
SELECT teamID FROM Team WHERE name='Red Bulls')))AS TeamSize;
RETURN Size;
END //
DELIMITER ;
And this script which fills it with data :
INSERT INTO Qualification (name, level) VALUES ('Under 7s', '3');
INSERT INTO Qualification (name, level) VALUES ('Under 8s', '1');
INSERT INTO Qualification (name, level) VALUES ('Under 9s', '5');
INSERT INTO Qualification (name, level) VALUES ('Under 10s', '4');
INSERT INTO Qualification (name, level) VALUES ('Under 80s', '10');
INSERT INTO Team (name, ageGroup, year) VALUES ('Blue Hawks', 'Under 7s', '2015');
INSERT INTO Team (name, ageGroup, year) VALUES ('Yellow Dolphins', 'Under 8s', '2013');
INSERT INTO Team (name, ageGroup, year) VALUES ('Red Bulls', 'Under 9s', '2014');
INSERT INTO Team (name, ageGroup, year) VALUES ('Turquiose Turtles', 'Under 10s', '2015');
INSERT INTO Team (name, ageGroup, year) VALUES ('Violet Butterflies', 'Under 80s', '2015');
INSERT INTO Address (number, street, suburb, townCity) VALUES ('6', 'Selwyn Street', 'North East Valley', 'Dunedin');
INSERT INTO Address (number, street, suburb, townCity) VALUES ('6', 'Inverleith Street', 'Woodhaugh', 'Dunedin');
INSERT INTO Address (number, street, suburb, townCity) VALUES ('40', 'Chaucer Street', 'Milton', 'Milton');
INSERT INTO Address (number, street, suburb, townCity) VALUES ('105', 'Inniscort Street', 'Decent Part', 'Cromwell');
INSERT INTO Address (number, street, suburb, townCity) VALUES ('43', 'Chambers Street', 'North East Valley', 'Dunedin');
INSERT INTO PhoneNumber (number) VALUES ('034178669');
INSERT INTO PhoneNumber (number) VALUES ('0272637393');
INSERT INTO PhoneNumber (number) VALUES ('0277147957');
INSERT INTO PhoneNumber (number) VALUES ('0220826217');
INSERT INTO PhoneNumber (number) VALUES ('0800838383');
INSERT INTO School (name) VALUES ('Tokomairiro High');
INSERT INTO School (name) VALUES ('Cromwell College');
INSERT INTO School (name) VALUES ('Otago Boys');
INSERT INTO School (name) VALUES ('Otago Girls');
INSERT INTO School (name) VALUES ('Woodhaugh Rest Palace');
INSERT INTO Person (name, email, photo) VALUES ('Andrew Fletcher', 'gmail#gmail.com', 'puppy.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Sam Bates', 'outlook#outlook.com', 'kitten.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Mason Osbourne', 'yahoo#gmail.com', 'cheetah.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Zara DeMontgomery', 'hola#malware.com', 'elephant.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Reuben Crimp', 'norton#avg.com', 'dolphins.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Joy Gasson', 'example#example.com', 'owl.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Brian Treanor', 'www#www.com', 'whale.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Dale Parsons', 'java#oracle.com', 'swordfish.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Tom Clark', 'GNU#linux.com', 'lion.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Jim Beam', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Jack Daniels', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('John Snow', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Ned Stark', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Tywin Lannister', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Hodor Hodor', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Joe Bloggs', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('John Doe', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Jane Doe', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Lassie Dog', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Jake Sully', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO PersonAddress (personID, addressID) VALUES ('1', '3');
INSERT INTO PersonAddress (personID, addressID) VALUES ('2', '4');
INSERT INTO PersonAddress (personID, addressID) VALUES ('3', '5');
INSERT INTO PersonAddress (personID, addressID) VALUES ('4', '3');
INSERT INTO PersonAddress (personID, addressID) VALUES ('5', '2');
INSERT INTO PersonPhoneNumber (personID, phoneNumberID) VALUES ('1', '2');
INSERT INTO PersonPhoneNumber (personID, phoneNumberID) VALUES ('2', '4');
INSERT INTO PersonPhoneNumber (personID, phoneNumberID) VALUES ('3', '1');
INSERT INTO PersonPhoneNumber (personID, phoneNumberID) VALUES ('4', '5');
INSERT INTO PersonPhoneNumber (personID, phoneNumberID) VALUES ('5', '3');
INSERT INTO Coach (coachID, dateBeganCoaching) VALUES ('6', '2014');
INSERT INTO Coach (coachID, dateBeganCoaching) VALUES ('7', '2013');
INSERT INTO Coach (coachID, dateBeganCoaching) VALUES ('8', '2012');
INSERT INTO Coach (coachID, dateBeganCoaching) VALUES ('9', '2014');
INSERT INTO Coach (coachID, dateBeganCoaching) VALUES ('10', '1993');
INSERT INTO Player (playerID, DOB, schoolID) VALUES ('1', '08/07/1993', '1');
INSERT INTO Player (playerID, DOB, schoolID) VALUES ('2', '02/06/1993', '2');
INSERT INTO Player (playerID, DOB, schoolID) VALUES ('3', '08/04/1995', '1');
INSERT INTO Player (playerID, DOB, schoolID) VALUES ('4', '08/01/1994', '1');
INSERT INTO Player (playerID, DOB, schoolID) VALUES ('5', '25/12/1992', '5');
INSERT INTO Player (playerID, DOB, SchoolID) VALUES ('11', '06/07/1998', '3');
INSERT INTO Player (playerID, DOB, SchoolID) VALUES ('12', '06/07/1998', '3');
INSERT INTO Player (playerID, DOB, SchoolID) VALUES ('13', '06/07/1998', '4');
INSERT INTO Player (playerID, DOB, SchoolID) VALUES ('14', '06/07/1998', '5');
INSERT INTO Player (playerID, DOB, SchoolID) VALUES ('15', '06/07/1998', '4');
INSERT INTO Guardian (guardianID) VALUES ('16');
INSERT INTO Guardian (guardianID) VALUES ('17');
INSERT INTO Guardian (guardianID) VALUES ('18');
INSERT INTO Guardian (guardianID) VALUES ('19');
INSERT INTO Guardian (guardianID) VALUES ('20');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('16', '1');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('16', '2');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('17', '3');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('17', '4');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('18', '5');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('18', '11');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('19', '12');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('19', '13');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('20', '14');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('20', '15');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('1', '1');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('2', '2');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('3', '3');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('4', '4');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('5', '5');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('2', '11');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('4', '12');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('4', '13');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('3', '14');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('5', '15');
INSERT INTO TeamCoach (teamID, coachID) VALUES ('1', '6');
INSERT INTO TeamCoach (teamID, coachID) VALUES ('2', '7');
INSERT INTO TeamCoach (teamID, coachID) VALUES ('3', '8');
INSERT INTO TeamCoach (teamID, coachID) VALUES ('4', '9');
INSERT INTO TeamCoach (teamID, coachID) VALUES ('5', '10');
INSERT INTO CoachQualification (coachID, qualificationID) VALUES ('6', '5');
INSERT INTO CoachQualification (coachID, qualificationID) VALUES ('7', '4');
INSERT INTO CoachQualification (coachID, qualificationID) VALUES ('8', '3');
INSERT INTO CoachQualification (coachID, qualificationID) VALUES ('9', '2');
INSERT INTO CoachQualification (coachID, qualificationID) VALUES ('10', '1');
Is what I am trying to do even possible?
I've attempted to create a query that will work for your data, but this is assuming you only have one guardian for each player (Which you said in your comment that this is not possible). Here is the SQLFiddle if you want to play around with it a bit more.
SELECT p.name, pg.name AS `GuardianName`, pgpn.`number` AS `GuardianNumber`
FROM `Player` play
LEFT JOIN `Person` p ON play.playerID = p.personID
LEFT JOIN `PlayerGuardian` g ON play.playerID = g.playerID
LEFT JOIN `Person` pg ON g.guardianID = pg.personID
LEFT JOIN `PersonPhoneNumber` pgppn ON pg.personID = pgppn.personID
LEFT JOIN `PhoneNumber` pgpn ON pgpn.phoneNumberID = pgppn.phoneNumberID
The problem you're having is, how do you display multiple guardians for each player? Do you want players to show up multiple times for each guardian that player has? Then you have to worry about grouping coding level, and this can be a big hassle.
My recommendation: Use an ORM similar to CakePHP that will do the heavy lifting for you. You set up the relationships between the tables, and it will do the magic of linking them up for you, in multiple efficient queries.

mysql statement wont insert

Hey guys I've searched for answers through the forums but to no avail so I'm using MySql and I'm trying to insert statements for certain tables and they aren't going into the tables and I'm getting errors like "Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated. The statement has been terminated."
These are the statements I'm having problems with.`INSERT INTO Course VALUES
INSERT INTO Course VALUES (12345, 'DatabaseManagement', '2015-2-1', '2014-5-9');
INSERT INTO Course VALUES (12346, 'Calculus', '2015-1-12', '2015-5-9');
INSERT INTO Course VALUES (12347, 'Biology', '2015-1-3', '2015-5-9');
INSERT INTO Course VALUES (12348, 'Chemistry', '2015-1-2', '2015-5-9');
INSERT INTO Grade VALUES (10, 12345, 012, 'A');
INSERT INTO Grade VALUES (11, 12346, 013, 'B');
INSERT INTO Grade VALUES (12, 12347, 014, 'C');
INSERT INTO Grade VALUES (13, 12348, 015, 'D');
INSERT INTO Grade VALUES (14, 12345, 016, 'B');
INSERT INTO Student VALUES (54321, 'Rachel', 'Cotterel', '2013-4-15', '2016-3-4');
INSERT INTO Student VALUES (54320, 'John', 'Smith', '2012-1-23', NULL);
INSERT INTO Student VALUES (54319, 'Johny', 'Depp', '2010-5-12', '2012-10-10');
INSERT INTO Student VALUES (54318, 'Orlando', 'Bloom', '2014-6-24', NULL);
INSERT INTO Student VALUES (54317, 'Linda', 'Jacob', '2015-4-4', '2019-8-6');
I didn't get any error for insert into Course statements. I got error for INSERT INTO Grade statements. Its because there is no reference available for StudentID 012,013 etc in Student table. And you are trying to add them in grade table.
Try using this:
INSERT INTO table1 (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
These are the field types:
CREATE TABLE Course
(
CourseID int,
Description varchar(20) NOT NULL,
StartDate DATE NOT NULL,
EndDate DATE NOT NULL,
CONSTRAINT [PK_CourseID] PRIMARY KEY (CourseID)
);
CREATE TABLE Grade
(
GradeID integer(10) NOT NULL,
CourseID integer(10) NOT NULL,
StudentID integer(10) NOT NULL,
Grade varchar (10) NULL,
CONSTRAINT [PK_GradeID] PRIMARY KEY (GradeID),
CONSTRAINT [FK_CourseID] FOREIGN KEY (CourseID) REFERENCES Course(CourseID),
CONSTRAINT [FK_StudentID] FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);
CREATE TABLE Student
(
StudentID integer(10) NOT NULL,
FirstName varchar(45) NOT NULL,
LastName varchar(45) NOT NULL,
RegistrationDate varchar (45) NOT NULL,
GraduationDate DATE NULL,
CONSTRAINT [PK_StudentlID] PRIMARY KEY (StudentID)
);
String or binary data would be truncated
The reason that you get this message should be that you are trying to insert some value to some field to which you haven't assigned enough size to hold the value.
Can you send what the exact error message you get?
I tried to do it myself.But the error I got was from you insertion query to Grade table foreign key fails which refer Student table because you are trying to insert Student_IDs which are not there in you Student table