search

Home  >  Q&A  >  body text

Example of relationship involving multiple entities

<p>I haven't found any examples of MYSQL many-to-many relationships here or in Google. What I'm looking for is a very simple example where php mysql displays results from a database. Can someone write a very simple example? </p>
P粉327903045P粉327903045474 days ago500

reply all(2)I'll reply

  • P粉288069045

    P粉2880690452023-08-28 18:15:19

    This is a simple and rough example of the SQL involved. I don't think it's necessary to confuse this concept with php. Just retrieve the collection like any other collection.

    In this example, there are many names and many colors. People can have more than one favorite color, and many people can have the same favorite color. Hence many-to-many.

    ***** Tables **********
    
    person
    --------
    id - int 
    name - varchar
    
    favColor
    -------------
    id - int 
    color - varchar
    
    person_color
    ------------
    person_id - int (matches an id from person)
    color_id - int (matches an id from favColor)
    
    
    
    ****** Sample Query ******
    
    SELECT name, color 
    FROM person 
        LEFT JOIN person_color ON (person.id=person_id)
        LEFT JOIN favColor ON (favColor.id=color_id)
    
    
    ****** Results From Sample Query *******
    
    Name - Color
    ---------------
    John - Blue
    John - Red
    Mary - Yellow
    Timmy - Yellow
    Suzie - Green
    Suzie - Blue
    etc...

    Is this helpful?

    reply
    0
  • P粉007288593

    P粉0072885932023-08-28 15:27:40

    Example scenario: Students and courses at a university. A given student may take multiple courses, and naturally, a course will often have many students.

    Sample table, simple design:

    CREATE TABLE `Student` (
        `StudentID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
        `FirstName` VARCHAR(25),
        `LastName` VARCHAR(25) NOT NULL,
        PRIMARY KEY (`StudentID`)
    ) ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_general_ci
    
    CREATE TABLE `Course` (
        `CourseID` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
        `Code` VARCHAR(10) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
        `Name` VARCHAR(100) NOT NULL,
        PRIMARY KEY (`CourseID`)
    ) ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_general_ci
    
    CREATE TABLE `CourseMembership` (
        `Student` INT UNSIGNED NOT NULL,
        `Course` SMALLINT UNSIGNED NOT NULL,
        PRIMARY KEY (`Student`, `Course`),
        CONSTRAINT `Constr_CourseMembership_Student_fk`
            FOREIGN KEY `Student_fk` (`Student`) REFERENCES `Student` (`StudentID`)
            ON DELETE CASCADE ON UPDATE CASCADE,
        CONSTRAINT `Constr_CourseMembership_Course_fk`
            FOREIGN KEY `Course_fk` (`Course`) REFERENCES `Course` (`CourseID`)
            ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=INNODB CHARACTER SET ascii COLLATE ascii_general_ci

    Find all students registered for a course:

    SELECT
        `Student`.*
    FROM
        `Student`
        JOIN `CourseMembership` ON `Student`.`StudentID` = `CourseMembership`.`Student`
    WHERE
        `CourseMembership`.`Course` = 1234

    Find all courses taken by a specified student:

    SELECT
        `Course`.*
    FROM
        `Course`
        JOIN `CourseMembership` ON `Course`.`CourseID` = `CourseMembership`.`Course`
    WHERE
        `CourseMembership`.`Student` = 5678

    reply
    0
  • Cancelreply