Create a View which spans multiple tables - mysql
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.
Related
Trying to insert data to multiple table with the last table `student` being the child table. Data is inserted to the other tables except the parent
BEGIN; INSERT INTO `address` (`postal address`, `email`, `phone_number`) VALUES ('1527', 'tommydd#gmail.com', '0766452152'); INSERT INTO `bank` (`bank_number`, `bank_name`,`branch`) VALUES ('050002345678', 'Equity', 'Haniwa'); INSERT INTO `fees` (`perannum`, `fee_balance`) VALUES ('27000', '1500.34'); INSERT INTO `institution` (`Institution_name`, `course`, `form_study_year`, `adm_upi`, `type`) VALUES ('KU', 'Computer Science', '3rd Year', 'STT/B/01-003/2009', 'Public Day'); INSERT INTO `student` (`address_id`, `fees_id`, `bank_id`, `institution_id`, `first_name`, `middle_name`, `last_name`, `gender`, `date_of_birth`, `bc_no`, `parent`) VALUES ('LAST_INSERT_ID()', 'LAST_INSERT_ID()', 'LAST_INSERT_ID()', 'LAST_INSERT_ID()', 'Tommy', 'Gas', 'John', 'Male', '11/04/1999', '3868686', 'Both Alive'); COMMIT;
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.
Query for select using where in [duplicate]
how can I get all usernames when I search "new1" .For eg: I should get A and B as userids 1,2 in tblC is 1,2 for row1 which has new1.What query should I use to get the above result? I really appreciate any help.Thanks in Advance. http://sqlfiddle.com/#!2/1ab8e/2 CREATE TABLE if not exists tblA ( id int(11) NOT NULL auto_increment , user varchar(255), category int(255), PRIMARY KEY (id) ); CREATE TABLE if not exists tblB ( id int(11) NOT NULL auto_increment , username varchar(255), userid int(255), PRIMARY KEY (id) ); CREATE TABLE if not exists tblC ( id int(11) NOT NULL auto_increment , nname varchar(255), userids varchar(255), PRIMARY KEY (id) ); INSERT INTO tblA (user, category ) VALUES ('1', '1'), ('1', '2'), ('1', '3'), ('1', '1'), ('2', '1'), ('2', '1'), ('2', '1'), ('2', '1'), ('3', '1'), ('2', '1'), ('4', '1'), ('4', '1'), ('2', '1'); INSERT INTO tblB (userid, username ) VALUES ('1', 'A'), ('2', 'B'), ('3', 'C'), ('4', 'D'), ('5', 'E'); INSERT INTO tblC (id, nname,userids ) VALUES ('1', 'new1','1,2'), ('2', 'new2','1,3'), ('3', 'new3','1,4'), ('4', 'new4','3,2'), ('5', 'new5','5,2'); Query so far: select * where nname="new1" from tblC CROSS JOIN tblB ON tblB.userid=(SELECT userids FROM substr(tblC.userids,','))
You should really look at Database normalization and first normalize your structure by adding a junction table and holds a relation from tablec each relation stored in tablec will be stored in new junction table but not as comma separated list each row will hold id of c and one user id per row ,if you can't alter your schema you can use find_in_set to find values in set select * from tblC c JOIN tblB b ON (find_in_set(b.userid,c.userids) > 0) where c.nname="new1" See demo Edit for normalize schema I have removed userids column from your tblC and instead i have created a new junction table as tblC_user with 2 columns c_id this will related to the id column of tblC and second one userid to store user relations users for tblC see sample schema for tblC CREATE TABLE if not exists tblC ( id int(11) NOT NULL auto_increment , nname varchar(255), PRIMARY KEY (id) ); INSERT INTO tblC (id, nname) VALUES ('1', 'new1'), ('2', 'new2'), ('3', 'new3'), ('4', 'new4'), ('5', 'new5'); And here is your junction table as tblC_user CREATE TABLE if not exists tblC_user ( c_id int, userid int ); INSERT INTO tblC_user (c_id,userid) VALUES ('1','1'), ('1','2'), ('2','1'), ('2','3'), ('3','1'), ('3','4'), ('4','3'), ('4','2'), ('5','5'), ('5','2'); In above if you notice i haven't stored any comma separated relations each relation of user for tblC is stored in new row ,for you concerned result set i have used junction table in join also new query will be like below select * from tblC c join tblC_user cu on(c.id = cu.c_id) join tblB b on (b.userid = cu.userid) where c.nname="new1" Demo 2 Now above query can can be optimized by using indexes you can maintain cascading relations easily
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.
Getting a list of records who's
I would like to have a list of all the players in a sports league who are on a team that only has a single member. Here is the sql: CREATE TABLE `formsfiles`.`Teams` ( `ID` INT NOT NULL AUTO_INCREMENT , `Name` VARCHAR(45) NULL , PRIMARY KEY (`ID`) ); INSERT INTO `Teams` (`Name`) VALUES ('Sharks'); INSERT INTO `Teams` (`Name`) VALUES ('Jets'); INSERT INTO `Teams` (`Name`) VALUES ('Fish'); INSERT INTO `Teams` (`Name`) VALUES ('Dodgers'); INSERT INTO `Teams` (`Name`) VALUES ('Pigs'); CREATE TABLE `Players` ( `ID` INT NOT NULL AUTO_INCREMENT , `Name` VARCHAR(45) NULL , `Team_ID` INT NULL , PRIMARY KEY (`ID`) ); INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Jim', '1'); INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Tom', '1'); INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Harry', '2'); INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Dave', '2'); INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Tim', '3'); INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Trey', '4'); INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Jay', '4'); INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Steve', '4'); INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Ziggy', '5'); INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Chris', '4'); The Result should give me: ID Name 5 Tim 9 Ziggy Not sure how to get them grouped up?
If you want to return the players on a team with only one member you can use: select p.id, p.name from players p where p.Team_ID in (select Team_ID from players p group by Team_ID having count(Team_ID) = 1); See SQL Fiddle with Demo Edit, you can also use (moving from comment): select max(id) id, max(name) name from players group by team_id having count(team_id) = 1; See SQL Fiddle with Demo
Thanks Bluefeet: this work wonderfully: I want to post it here in case someone serches itin the future: select max(id) id, max(name) name from players group by team_id having count(team_id) = 1;