Integrity constraint (LEETO14.SYS_C00414504) violated - mysql

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.

Related

Sum of multiple rows in sql with join statement

I am trying to sum multiple rows of the same athlete so it will return the total amount of medals that they have won overall. I have the following code:
CREATE TABLE athlete (
athlete_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name TINYTEXT NOT NULL,
country TINYTEXT NOT NULL,
birthdate DATE NOT NULL,
age INT UNSIGNED,
height_inch INT UNSIGNED,
PRIMARY KEY (athlete_id)
);
INSERT INTO athlete (name, country, birthdate, age, height_inch) VALUES ('Simone Biles', 'United States', '1997-03-14', 24, 56);
INSERT INTO athlete (name, country, birthdate, age, height_inch) VALUES ('Michael Phelps', 'United States', '1985-06-30', 36, 76);
CREATE TABLE sport (
sport_id INT UNSIGNED NOT NULL,
sport TINYTEXT NOT NULL,
PRIMARY KEY (sport_id)
);
INSERT INTO sport VALUES (101, 'Skiing');
INSERT INTO sport VALUES (102, 'Biathlon');
INSERT INTO sport VALUES (103, 'Curling');
INSERT INTO sport VALUES (104, 'Skating');
INSERT INTO sport VALUES (105, 'Ice Hockey');
INSERT INTO sport VALUES (106, 'Luge');
INSERT INTO sport VALUES (107, 'Snowboard');
INSERT INTO sport VALUES (108, 'Basketball');
INSERT INTO sport VALUES (109, 'Gymnastics');
INSERT INTO sport VALUES (110, 'Swimming');
INSERT INTO sport VALUES (111, 'Diving');
INSERT INTO sport VALUES (112, 'Track and Field');
INSERT INTO sport VALUES (113, 'Badminton');
INSERT INTO sport VALUES (114, 'Tennis');
INSERT INTO sport VALUES (115, 'Volleyball');
INSERT INTO sport VALUES (116, 'Skateboard');
INSERT INTO sport VALUES (117, 'Soccer');
INSERT INTO sport VALUES (118, 'Golf');
INSERT INTO sport VALUES (119, 'Cycling');
INSERT INTO sport VALUES (120, 'Climbing');
INSERT INTO sport VALUES (121, 'Surfing');
INSERT INTO sport VALUES (122, 'Water Polo');
INSERT INTO sport VALUES (123, 'Karate');
CREATE TABLE olympics (
olympics_id INT UNSIGNED NOT NULL,
season TINYTEXT NOT NULL,
year YEAR NOT NULL,
city TINYTEXT NOT NULL,
PRIMARY KEY (olympics_id)
);
INSERT INTO olympics VALUES (1001, 'Summer', 1936, 'Berlin');
INSERT INTO olympics VALUES (1002, 'Summer', 1956, 'Melbourne');
INSERT INTO olympics VALUES (1003, 'Summer', 1960, 'Rome');
INSERT INTO olympics VALUES (1004, 'Summer', 1964, 'Tokyo');
INSERT INTO olympics VALUES (1005, 'Summer', 1976, 'Montreal');
INSERT INTO olympics VALUES (1006, 'Summer', 1984, 'Los Angelos');
INSERT INTO olympics VALUES (1007, 'Summer', 1996, 'Atlanta');
INSERT INTO olympics VALUES (1008, 'Summer', 2000, 'Sydney');
INSERT INTO olympics VALUES (1009, 'Summer', 2004, 'Athens');
INSERT INTO olympics VALUES (1010, 'Summer', 2008, 'Beijing');
INSERT INTO olympics VALUES (1011, 'Summer', 2012, 'London');
INSERT INTO olympics VALUES (1012, 'Summer', 2016, 'Rio de Janeiro');
INSERT INTO olympics VALUES (1013, 'Summer', 2020, 'Tokyo');
CREATE TABLE sport_events (
sport_id INT UNSIGNED NOT NULL,
event_id INT UNSIGNED NOT NULL,
event TINYTEXT NOT NULL,
PRIMARY KEY (event_id),
FOREIGN KEY (sport_id) REFERENCES sport (sport_id)
);
INSERT INTO sport_events VALUES (101, 501, 'Alpine Skiing');
INSERT INTO sport_events VALUES (101, 502, 'Cross-Country Skiing');
INSERT INTO sport_events VALUES (104, 503, 'Figure Skating');
INSERT INTO sport_events VALUES (101, 504, 'Freestyle Skiing');
INSERT INTO sport_events VALUES (104, 505, 'Short Track Speed Skating');
INSERT INTO sport_events VALUES (101, 506, 'Ski Jumping');
INSERT INTO sport_events VALUES (107, 507, 'Half-pipe');
INSERT INTO sport_events VALUES (101, 508, 'Half-pipe');
INSERT INTO sport_events VALUES (104, 509, 'Speed Skating');
INSERT INTO sport_events VALUES (109, 510, 'Artistic Gymnastics');
INSERT INTO sport_events VALUES (109, 511, 'Rhythmic Gymnastics');
INSERT INTO sport_events VALUES (115, 512, 'Beach Volleyball');
INSERT INTO sport_events VALUES (112, 513, 'High Jump');
INSERT INTO sport_events VALUES (112, 514, '100m');
INSERT INTO sport_events VALUES (112, 515, '200m');
INSERT INTO sport_events VALUES (112, 516, '400m');
INSERT INTO sport_events VALUES (112, 517, '800m');
INSERT INTO sport_events VALUES (112, 518, '4x100m relay');
INSERT INTO sport_events VALUES (112, 519, 'Triple Jump');
CREATE TABLE athlete_sport (
athlete_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
sport_id INT UNSIGNED NOT NULL,
PRIMARY KEY (athlete_id),
FOREIGN KEY (athlete_id) REFERENCES athlete (athlete_id),
FOREIGN KEY (sport_id) REFERENCES sport (sport_id)
);
INSERT INTO athlete_sport (sport_id) VALUES (109);
INSERT INTO athlete_sport (sport_id) VALUES (110);
CREATE TABLE compete (
athlete_id INT UNSIGNED NOT NULL,
olympics_id INT UNSIGNED NOT NULL,
sport_id INT UNSIGNED NOT NULL,
event_id INT UNSIGNED,
gold INT UNSIGNED,
silver INT UNSIGNED,
bronze INT UNSIGNED,
FOREIGN KEY (olympics_id) REFERENCES olympics (olympics_id),
FOREIGN KEY (athlete_id) REFERENCES athlete (athlete_id),
FOREIGN KEY (event_id) REFERENCES sport_events (event_id)
);
INSERT INTO compete VALUES (1, 1012, 109, 510, 4, 0, 1);
INSERT INTO compete VALUES (1, 1013, 109, 510, 0, 1, 1);
INSERT INTO compete VALUES (2, 1009, 110, NULL, 6, 0, 2);
INSERT INTO compete VALUES (2, 1010, 110, NULL, 8, 0, 0);
INSERT INTO compete VALUES (2, 1011, 110, NULL, 4, 2, 0);
INSERT INTO compete VALUES (2, 1012, 110, NULL, 5, 1, 0);
I have looked at other answers people have posted and most of them are just saying to use group by, but when I use that it just orders the either names or number of medals together just in a different order. I'm trying to get it so it says that the total number of medals simone biles has is 7 and michael phelps is 28 just in a single table.
This is the query I have that returns the sum of the medals for each olympic games they've been in, but again if I use group by it just orders them.
select a.name, gold+silver+bronze as medalTotal from athlete a join compete c using (athlete_id) group by medalTotal;
+----------------+------------+
| name | medalTotal |
+----------------+------------+
| Simone Biles | 2 |
| Simone Biles | 5 |
| Michael Phelps | 6 |
| Michael Phelps | 8 |
+----------------+------------+
you need to do the following:
group by athlete
sum each medal type
add the sum of each medal type
You need to group by athlete, not total.
You have to use an aggregation function to combine values from all the rows in a group. Use SUM() to add them together.
select a.name, SUM(b.gold+b.silver+b.bronze) as medalTotal
from athlete a
join compete c using (athlete_id)
group by a.athlete_id;
You should apply group by using something unique like athelet id.
This get all atheletes with all medals number including with zero medals
if you want just athelet with medals only use join only.
select
a.name, sum(gold+silver+bronze) as medalTotal
from
athlete a
left join
compete c on c.athlete_id = a.athlete_id
group by
a.athlete_id

Syntax Problems? Insert Into

I'm creating an online store/database in MySQL, and I'm fairly new to the system.
Please tell me what is wrong with this statement:
CREATE TABLE IF NOT EXISTS Customers (
ID INT(15) NOT NULL AUTO_INCREMENT, -- Connects to table 'Orders' andOrderUserID
Email VARCHAR(100) NOT NULL,
PersonName VARCHAR(100) NOT NULL,
City VARCHAR(90) NOT NULL,
Zip INT(10),
CustomerState VARCHAR(50),
Address VARCHAR(200) NOT NULL,
Country VARCHAR(20),
Phone INT(50),
PRIMARY KEY (ID)
);
INSERT INTO Customers (ID, Email, PersonName, City, Zip, CustomerState, Address, Country, Phone) VALUES
(1, '1#gmail.com', 'Fred', 'Brisbane', 6543, 'QLD', '20 Not telling avenue', 'Australia', 932363626),
(2, '2#gmail.com', 'Mark', 'Perth', 3212, 'WA', ' 122 cralic road', 'Australia', 93223463221),
(3, '3#gmail.com', 'Leo', 'Melbourne', 2342, 'VIC', '90 normanly drive', 'Australia', 932645321),
(4, '4#gmail.com', 'Nick', 'Perth', 4294, 'WA', '44 happylane drive', 'Australia', 932222565621),
(5, '5#gmail.com', 'Mary', 'Perth', 6383, 'WA', '3 iverno crescent', 'Australia', 93365543321),
(6, '6#gmail.com', 'Julie', 'Perth', 9563, 'WA', '100 richmane lane', 'Australia', 9323456421),
(7, '7#gmail.com', 'Cynthia', 'Hobart', 3456, 'TAS', '12 trump avenue', 'Australia', 9322364641),
(8, '8#gmail.com', 'Mike', 'Albany', 7544, 'WA', '23 mapatazzie road', 'Australia', 9322236421),
(9, '9#gmail.com', 'Brett', 'Adelaide', 8953, 'SA', '983 chindes lane', 'Australia', 9322234651),
(10, '10#gmail.com', 'Paul', 'Sydney', 7853, 'VIC', '78 yives road', 'Australia', 932223463621),
1.) Always post which error you get when asking for help
2.) You left a comma , at the last line
3.) int(50) does not do what you think it does. It doesn't mean a number that has 50 digits. int means data type, and it will use 4 bytes. 50, in your case, means display length. Not storage length. int is 4 bytes. It can display up to 11 digits only (with minus sign included). Therefore, your int(50) doesn't make it 50 digits long, it will hold at maximum (2^32 - 1) = 4294967295.
Make your Phone a varchar
Update Last line with below line:
(10, '10#gmail.com', 'Paul', 'Sydney', 7853, 'VIC', '78 yives road', 'Australia', 932223463621);
add semicolon
Solution:
The problem is, you mentioned Phone field as integer and given values like 93223463221.
The integer value is greater than 2147483647, hence the error.
To solve,
alter table Customers modify column Phone varchar(50);
Use the above query to modify the Phone(Integer) filed as varchar field.
Then try to insert the values.

SQL Code not running: Drop Tables and Organization issue maybe?

So, this is my code. When I attempt to compile it in the school provided engine, I've gotten a couple errors, and they do change each time I run it, but the one I currently have is this:
ERROR 1064 (42000) at line 123: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near '9:30, 'L210', 103, 15), (86, 25, 2,
6/10/2007 9:30, 'L210', 107, 15), (89, 25, 5' at line 2
I've already tried to take out the spaces, but it makes no difference. Would this have anything to do with the order in which I take information? Or is it something else?
I'm also not sure that the tables are dropping at step one.
/*Step One: Drop Tables*/
DROP TABLE IF EXISTS Student;
DROP TABLE IF EXISTS Zipcode;
DROP TABLE IF EXISTS Instructor;
DROP TABLE IF EXISTS Enrollment;
DROP TABLE IF EXISTS Sections;
DROP TABLE IF EXISTS Course;
/*Step Two: Create Tables*/
CREATE TABLE Zipcode
(
zip int(11),
city varchar(25) NOT NULL,
state varchar(2) NOT NULL,
PRIMARY KEY (zip)
);
CREATE TABLE Student
(
student_ID int(6) UNIQUE,
salutation varchar(5),
first_name varchar(25) NOT NULL,
last_name varchar(25) NOT NULL,
street_address varchar(50),
phone int(15) NOT NULL,
employer varchar(50) ,
registration_date date NOT NULL,
zip int(11),
PRIMARY KEY (student_ID),
FOREIGN KEY (zip) REFERENCES Zipcode (zip)
);
CREATE TABLE Course
(
course_ID int(6) UNIQUE,
description varchar(50) NOT NULL,
cost dec(8,2) NOT NULL,
prerequisite int(6),
PRIMARY KEY (course_ID),
FOREIGN KEY (prerequisite) REFERENCES Course (course_ID)
);
CREATE TABLE Instructor
(
Instructor_ID int(6) UNIQUE,
salutation varchar(5),
first_name varchar(25) NOT NULL,
last_name varchar(25) NOT NULL,
street_address varchar(50),
zip int(11) NOT NULL,
PRIMARY KEY (Instructor_ID),
FOREIGN KEY (zip) REFERENCES zipcode (zip)
);
CREATE TABLE Sections
(
section_ID int(8) UNIQUE,
course_id int(6) NOT NULL,
course_section_num int(6) NOT NULL,
start_date_time datetime NOT NULL,
location varchar(10),
instructor_ID int(6) NOT NULL,
capacity int(3),
PRIMARY KEY (section_ID),
FOREIGN KEY (instructor_ID) REFERENCES Instructor (instructor_ID),
FOREIGN KEY (course_ID) REFERENCES Course (course_ID)
);
CREATE TABLE Enrollment
(
student_ID int(6) UNIQUE,
section_ID int(8) UNIQUE,
enroll_date datetime NOT NULL,
final_grade char(1),
PRIMARY KEY (section_ID, student_ID),
FOREIGN KEY (section_ID) REFERENCES sections (section_ID),
FOREIGN KEY (student_ID) REFERENCES student (student_ID)
);
/*Step Three: Insert Rows*/
INSERT INTO Zipcode VALUES
(7024, 'Ft. Lee', 'NJ'),
(7047, 'North Bergen', 'NJ'),
(10005, 'New York', 'NY'),
(10015, 'New York', 'NY'),
(10025, 'New York', 'NY'),
(10035, 'New York', 'NY'),
(11419, 'Richmond Hill', 'NY'),
(11435, 'Jamaica', 'NY');
INSERT INTO Student VALUES
(102, 'Mr.', 'Fred', 'Crocitto', '101-09 120th St.', 718-555-5555, 'Albert Hildegard Co.', 1/22/2007, 11419),
(103, 'Ms.', 'J.', 'Landry', '7435 Boulevard East #45', 201-555-5555, 'Albert Hildegard Co.', 1/22/2007, 7047),
(104, 'Ms.', 'Laetia', 'Enison', '144-61 87th Ave', 718-555-5555, 'Albert Hildegard Co.', 1/22/2007, 11435),
(105, 'Mr.', 'Angel', 'Moskowitz', '320 John St.', 201-555-5555, 'Alex. & Alexander', 1/22/2007, 7024),
(163, 'Ms.', 'Nicole', 'Gillen', '4301 N Ocean #103', 904-555-5555, 'Oil of America Corp.', 2/2/2007, 10025),
(223, 'Mr.', 'Frank', 'Pace', '13 Burlington Dr.', 203-555-5555, 'Board Utilities', 2/8/2007, 10025),
(399, 'Mr.', 'Jerry', 'Abdou', '460 15th St. #4', 718-555-5555, 'Health Mgmt.Systems', 2/23/2007, 10025);
INSERT INTO Course VALUES
(330, 'Network Administration', 1195, 130),
(310, 'Operating Systems', 1195, NULL),
(142, 'Project Management', 1195, 20),
(140, 'Systems Analysis', 1195, 20),
(130, 'Intro to Unix', 1195, 310),
(25, 'Intro to Programming', 1195, 140),
(20, 'Intro to Information Systems', 1195, NULL);
INSERT INTO Instructor VALUES
(101, 'Mr.', 'Fernand', 'Hanks', '100 East 87th', 10015),
(102, 'Mr.', 'Tom', 'Wojick', '518 West 120th', 10025),
(103, 'Ms.', 'Nina', 'Schorin', '210 West 101st', 10025),
(104, 'Mr.', 'Gary', 'Pertez', '34 Sixth Ave', 10035),
(105, 'Ms.', 'Anita', 'Morris', '34 Maiden Lane', 10015),
(106, 'Rev.', 'Todd', 'Smythe', '210 West 101st', 10025),
(107, 'Dr.', 'Marilyn', 'Frantzen', '254 Bleeker', 10005);
INSERT INTO Sections VALUES
(81, 20, 2, 7/24/2007 9:30, 'L210', 103, 15),
(86, 25, 2, 6/10/2007 9:30, 'L210', 107, 15),
(89, 25, 5, 5/15/2007 9:30, 'L509', 103, 25),
(92, 25, 8, 6/13/2007 9:30, 'L509', 106, 25),
(104, 330, 1, 7/14/2007 10:30, 'L511', 104, 25),
(119, 142, 1, 7/14/2007 9:30, 'L211', 103, 25),
(155, 122, 4, 5/4/2007 9:30, 'L210', 107, 15);
INSERT INTO Enrollment VALUES
(102, 86, 1/30/2007, NULL, 'B'),
(102, 89, 1/30/2007, 92,'A'),
(103, 81, 1/30/2007, NULL),
(104, 81, 1/30/2007, NULL, 'A'),
(163, 92, 2/10/2007, NULL),
(223, 104, 2/16/2007, NULL,'C'),
(223, 119, 2/16/2007, NULL);
/*Step Four: Select Statements*/
SELECT * FROM Student;
SELECT * FROM Zipcode;
SELECT * FROM Instructor;
SELECT * FROM Course;
SELECT * FROM Sections;
SELECT * FROM Enrollment;
The issue identified by the error message is the datetime literal, here:
INSERT INTO Sections VALUES
(81, 20, 2, 7/24/2007 9:30, 'L210', 103, 15)
^^^^^^^^^^^^^^
To get a value assigned to a datetime column, you could do this:
(81, 20, 2, '2007-07-24 09:30', 'L210', 103, 15)
^^^^^^^^^^^^^^^^^^
Datetime literals should be enclosed in single quotes, and represented in a format like 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MI:SS'.
(It is also possible to pass numeric decimal values in. But no one really does that. Just use a string literal in the correct format. I believe newer versions of MySQL are more lenient than earlier versions, regarding the strictness of two characters for month, and two characters for day, and using a delimiter other than the dash. I think its also possible to omit the delimiter for a datetime
I think MySQL would also accept something like this:
'20070724093000'
But again, no one really does that. Just supply the values as strings in the standard 'YYYY-MM-DD HH:MI:SS' format.

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.

Error 1136 in 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.