28 Matching Annotations
  1. Jan 2023
    1. referential integrity ensures that data is consistent between tables. Specifically, referential constraints ensure that a foreign key value in a child table (or child row in the case of a recursive constraint) always has a matching primary key value in the parent table (or parent row). In a physical design, a foreign key is the combination of one or more columns in a table that reference (match) the primary key in the parent table (or parent row in the case of recursive relationships). Primary keys and foreign keys are the fundamental building blocks of the relational database because these are the components that make the relationships work while allowing tables to be accessed independently. One-to-many relationships between primary keys and foreign keys are thus defined as referential constraints in the physical database.
    2. TRUNCATE Statement
      • The TRUNCATE statement is a fast and efficient way to empty a table of all rows. Unlike the DELETE statement that you will see shortly, TRUNCATE wipes the entire table without going through the table row by row
    3. CREATE INDEX Statement

      ABOUT INDEXES *indexes can make queries faster, much like looking for a term in the index of a textbook instead of skimming through all the pages in the book to find all the references to the term. However, indexes take storage space and the DBMS must maintain the index as rows are inserted, updated and deleted in the table.

    4. foreign key is the combination of one or more columns in a table that reference (match) the primary key in the parent table (or parent row in the case of recursive relationships)

      foreign key = the combination of one or more columns in a table that reference (match) the primary key in the parent table (or parent row in the case of recursive relationships)

    5. ALTER TABLE Statement

      ASPECTS CHANGED BY ALTER TABLE STATEMENT * Adding columns to the table * Removing columns from the table * Altering the data type for existing table columns * Changing physical storage properties of the table * Adding, removing, or altering constraints

    6. CREATE TABLE Statement

      STATEMENT FORMAT OVERVIEW * The statement starts with the keywords CREATE TABLE, followed by the name of the table, which must be unique within the database or schema. The remainder of the statement consists of a comma-separated lists of column definitions, enclosed in a pair of parentheses. Each column definition consists of the name of the column, which must be unique within the table, followed by a data type for the column and other property

    7. hat you need is to filter on the count of the number of races for each horse. The HAVING clause does exactly what we need here – it has syntax like the WHERE clause, but it is applied to each group after grouping has taken place

      Main diff between HAVING and WHERE

        • WHERE is applied before grouping, it is usually used to narrow the SELECT statement's output
        • HAVING is applied after grouping. It is usually used when grouping is needed to incorporate function generated values into a new table requested by the query
    8. SELECT HORSE_ID, SUM(PURSE), COUNT(*) AS NUM_RACES FROM RACE_RESULT GROUP BY HORSE_ID;

      okay what i'm missing here is that the race_result table I think groups rows by race, not by horse. So each horse can have multiple races.

      The above statements asks the dbms to find each race (row) with a specific horse_id, and then average certain column values from all the rows corresponding to the same horse_id. The average is then to be output as a new row containing a single horse id, the sum of that horses earnings across all race rows, the COUNT(*) statement is then used to count how many rows in the horse table had a specific horse id.So really the query is requesting a table with

      horse_id | (earnings sum from race_result rows with horse_id) | (number of race result rows featuring horse_id)

    9. SUM(PURSE), COUNT(*) AS NUM_RACES

      IMPORTANT!! : The group by clause is needed here because unlike in previous queries, we aren't simply taking a value in a prexisting table and adding it to a smaller table using the SELECT clause. We are using functions to create entirely new values. These values dont exist as part of the table, they kinda just float. Because these are floating unindexed values, the group by clause is needed to indicate how these new values should be presented in the output table for the query

    10. An inner join is best thought of as an exclusive join because the result set includes only rows where matches were found in both tables (unmatched rows are excluded from the results)

      TLDR inner join = exclusive to matches in both tables * Any row from the key table which has a value that is omitted from the key column (Horse table has key columns of sire_id and dam_id) is exlcuded from the final result. So if horse_id_x has a null value for its sire or dam id, then any information about the row corresponding to horse_id_X is entirely ommitted from the final result of the joined table.

    11. SIRE_HORSE_ID

      ohhh I get it. I thought all the sire and dam information was in each row for every horse, but I think all that is included on each horse row is the ids for their mom and dad. So if u wanted to get the horses parents names you would need to join name info from the id column for the parents with the info from the offspring column. The parent id would be used as the matching key.

    12. FROM HORSE A, OWNER B WHERE A.OWNER_ID = B.OWNER_ID;

      This process uses the FROM clause to create an alias for the horse/owner tables as A and B respectively. The format is simply

      FROM TableA Alias1, TableB Alias2

    13. Cartesian product

      Where every row from table a is joined with each row of table b ( totalling A*B rows), Cartesian products arise when tables are merged with no matching logic (ie a shared column like owner_id which can distinguish how each row in table A should be joined with each row of table B)