{
    "href": "/post/2014/10/09/whats-the-difference-between-a-pivot-table-and-an-association-table/",
    "relId": "2014/10/09/whats-the-difference-between-a-pivot-table-and-an-association-table",
    "title": "What's The Difference Between A \"Pivot Table\" And An \"Association Table\"?",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/patterns/",
            "relId": "patterns",
            "title": "Patterns",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/programming/",
            "relId": "programming",
            "title": "Programming",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2014-10-09 11:43:29 UTC",
    "updated": [
        "2014-10-09 11:43:29 UTC"
    ],
    "html": "<p>An \u201cassociation table\u201d is a table that joins other tables in a many-to-many relationship. For example, if an Article can have more than one Tag, and each Tag can be placed on one or more Articles, then they are in a many-to-many relationship. To associate them to each other, we need a third table through which we can join them.</p>\n<pre><code class=\"sql\">-- one end of a many-to-many relationship\nCREATE TABLE article (\n    article_id INT,\n    title VARCHAR(255),\n    body TEXT\n);\n\n-- the other end of a many-to-many relationship\nCREATE TABLE tag (\n    tag_id INT,\n    name VARCHAR(255)\n);\n\n-- an association table mapping articles and tags to each other\nCREATE TABLE article_tag (\n    article_tag_id INT,\n    article_id INT,\n    tag_id INT\n);\n</code></pre>\n<p>In ORM terms, we might say each Article \u201chas many\u201d Tags \u201cthrough\u201d the ArticleTag association, that each Tag also \u201chas many\u201d Articles \u201cthrough\u201d the ArticleTag, and finally that each ArticleTag \u201cbelongs to\u201d an Article <em>and</em> that it \u201cbelongs to\u201d a Tag.</p>\n<p>When writing SQL to find the Tags for an Article, or to find all the Articles that use a specific Tag, we join the ArticlesTags table to get the associated entity IDs. The SQL looks something like the following:</p>\n<pre><code class=\"sql\">-- select all the tags for an article\nSELECT tag.*\nFROM tag\nJOIN article_tag ON article_tag.tag_id = tag.tag_id\nWHERE article_tag.article_id = ?\n\n-- select all the articles that use a tag\nSELECT article.*\nFROM article\nJOIN article_tag ON article_tag.article_id = article.article_id\nWHERE article_tag.tag_id = ?\n</code></pre>\n<p>This pattern is called an <a href=\"http://martinfowler.com/eaaCatalog/associationTableMapping.html\">association table mapping</a>.</p>\n<p>On the other hand, a \u201cpivot table\u201d is a cross-tabulation query, frequently used in spreadsheets. You can see more about <a href=\"http://www.google.com/search?q=pivot+table\">pivot tables through Google</a>. In short, the idea is to build a query, and convert the rows into columns by grouping the rows in a particular way. These kinds of queries generally involve some conditionals and calculations to group the query results; you can see some examples <a href=\"http://www.artfulsoftware.com/infotree/qrytip.php?id=78\">here</a>.</p>\n<p>In summary: if you are joining tables to each other in a many-to-many relationship, the table that maps the relationship is an association table. If you are doing a cross-tabulation to convert rows into columns, you are working with pivot table.</p>\n<hr>\n<p><strong>UPDATE:</strong> Apparently \"pivot\" is also a keyword in SQL Server to help generate true pivot tables; see <a href=\"https://blogs.msdn.microsoft.com/spike/2009/03/03/pivot-tables-in-sql-server-a-simple-sample/\">here</a>. See also <a href=\"https://en.wikipedia.org/wiki/Associative_entity\">Associative Entity</a>.</p>\n<hr>\n<p class=\"reddit-links\">Read the Reddit discussion about this post <a href=\"https://www.reddit.com/r/PHP/comments/2ir1k8/whats_the_difference_between_a_pivot_table_and_an/\">here</a>.</p>\n"
}
