Prepstellar

SQL Fundamentals · SQL Foundations

16 cards

Relational Model and SQL

Swipe, scroll or use ← →
  1. Tables, rows, and columns

    A relational database stores related facts in tables. A table has named columns, and each row holds one value for each column. The column definition gives those values a meaning and a type.

    For example, a customers table stores one row per customer:

    customer_number name
    10 Alice
    20 Bob

    Read across a row to describe one customer. Read down a column to compare the same fact across customers. This basic picture comes before keys.

    1 / 16
  2. Quick check

    Which row describes Bob?

    1. Acustomer_number 20, name Bob

      Right. Reading across that row gives Bob's stored values.

    2. BThe name column by itself

      A column holds the same kind of fact for many rows; it is not Bob's complete row.

    3. Ccustomer_number 10, name Alice

      That row describes Alice, not Bob.

    2 / 16

  3. Keys that identify rows

    A primary key identifies a row with one column or a group of columns. In standard SQL, its values must be unique and not null. A missing value cannot identify a row, and a duplicate would identify more than one row.

    Other databases: SQLite is the exception. In an ordinary SQLite table, a primary-key column can accept NULL unless you also declare it NOT NULL.

    For customers, customer_number is the primary key. The table already contains customer 10, so another row with customer_number 10 would create two rows with the same identity. The database rejects that insert.

    A table may have several unique constraints but only one primary key. A unique constraint can protect another business value, such as an email address.

    3 / 16
  4. Quick check

    The customers table already has primary-key value 10. What happens if a new row also uses 10?

    1. ABoth rows become customer 10

      Two matching values would no longer identify one row.

    2. BThe database changes the new value

      A primary-key rule checks the value; it does not silently replace it.

    3. CThe duplicate insert is rejected

      Right. A primary key must remain unique.

    4 / 16

  5. Foreign keys relate tables

    A foreign key stores a reference to a key in another table, or in the same table. A non-null foreign-key value must match a value in a referenced row. This protects referential integrity.

    The orders table can store the customer number for each order:

    order_id customer_number
    101 10

    Here, customer_number is a foreign key. The value 10 matches Alice in customers. An order with customer_number 99 fails because the customers table has no row with that key.

    Columns covered by a primary key or suitable unique constraint give the reference one row to match. NOT NULL alone is not enough because duplicate values are still possible.

    5 / 16
  6. Quick check

    The customers table has keys 10 and 20. Can a new order use non-null customer_number 99?

    1. AYes; 99 creates a customer automatically

      A foreign key does not create the missing customer row.

    2. BNo; the order violates the foreign-key rule

      Right. The value must match a referenced customer key.

    3. CYes; any non-null number is accepted

      NOT NULL only says a value is present; the foreign key also requires a match.

    6 / 16

  7. Keep your progress in the app

    That’s 3 of 8 quick checks. In the app they stay answered, and every lesson remembers where you left off.

    Opens in your browser. Nothing to install.

    Start studying

    On your phone, with the same account

    Get it on Google Play Download on the App Store
    Keep reading here
  8. SQL statements and clauses

    A SQL statement is a complete command. A clause is a named part of that command. This query is one statement:

    SELECT price * quantity AS total
    FROM order_items
    WHERE quantity > 1;
    

    The select list states the values that appear in the output. It is on the first line. The FROM clause names the input table. The WHERE clause keeps only rows that meet its condition. Together, FROM and its following clauses form the query's table expression.

    7 / 16
  9. Quick check

    Which line removes rows whose quantity is not greater than 1?

    1. AWHERE quantity > 1

      Right. WHERE applies the condition to each input row.

    2. BSELECT price * quantity AS total

      The select list calculates an output value after rows are selected.

    3. CFROM order_items

      FROM supplies the input table but does not apply this condition.

    8 / 16

  10. Expressions and table expressions

    An expression calculates a value. A column reference, a literal such as 2, and price * quantity are value expressions. A value expression produces one scalar value. In everyday words, a scalar is one value, such as 5 or 30.

    In SELECT 2 + 3;, the expression produces 5, and the query returns that value in one column:

    2 + 3
    5

    A table expression produces rows and columns that the rest of the query can use. It describes a set of data rather than one calculated value.

    9 / 16
  11. Quick check

    What result does SELECT 2 + 3; return?

    1. ATwo result rows, one with 2 and one with 3

      The plus sign adds the two values; it does not make a row for each literal.

    2. BAn error because the statement names no table

      This value-only SELECT needs no input table in the SQLite/PostgreSQL lab.

    3. COne result cell containing 5

      Right. The expression produces 5, and the query returns it in one cell.

    10 / 16

  12. Queries and result sets

    A query retrieves data, and SQL uses SELECT to specify a query. The select list contains the output expressions. These are the rows in order_items:

    item quantity price
    A 2 15
    B 1 20
    C 5 10

    Run the same statement:

    SELECT price * quantity AS total
    FROM order_items
    WHERE quantity > 1;
    

    WHERE removes item B because its quantity is 1. The select-list expression multiplies the remaining rows: 15 * 2 gives 30, and 10 * 5 gives 50.

    The rows and columns returned by a query form its result set:

    total
    30
    50

    In this command, FROM and WHERE are clauses, the multiplication and comparison are expressions, and the returned total values belong to the result set.

    11 / 16
  13. Quick check

    Which totals does the query return for the three input rows?

    1. A30 and 50

      Right. Items A and C pass WHERE, then the select-list expression returns 30 and 50.

    2. B20 only

      Item B has price 20, but quantity 1 fails the WHERE condition.

    3. C30, 20, and 50

      This includes item B even though its quantity is not greater than 1.

    12 / 16

  14. Designing a relationship

    Suppose customer_number is the stable identity for each customer, email must also reject duplicates, and every order must name an existing customer.

    Make customer_number the primary key. Add a separate unique constraint to email. Store customer_number in orders, declare that column NOT NULL, and add a foreign key that references the customer key.

    One primary key supplies the declared identity, and a unique constraint protects another business value. Together, NOT NULL and the foreign key do the third job: NOT NULL requires a customer value, and the foreign key checks that this customer exists. A foreign key on its own would still let an order arrive with no customer at all, because a null reference has nothing to check.

    13 / 16
  15. Quick check

    Which design applies all three requirements?

    1. ADeclare customer_number and email as two primary keys

      A table can declare only one primary key; email can use a separate unique constraint.

    2. BMake customer_number the primary key, email unique, and orders.customer_number both NOT NULL and a foreign key

      Right. NOT NULL requires a customer value and the foreign key checks that this customer exists.

    3. CMake customer_number the primary key, email unique, and orders.customer_number a foreign key

      The foreign key alone still accepts an order whose customer column is null, which names no customer.

    14 / 16

  16. Key takeaways

    Tables contain rows and named columns. In standard SQL, primary-key values must be unique and not null. In an ordinary SQLite table, add NOT NULL to enforce the second rule. A table can declare at most one primary key, while separate unique constraints can protect other values. A foreign key connects a row to an existing referenced key.

    The select list states the output values. A value expression calculates one scalar value. A table expression supplies rows and columns for the query to use. A SELECT query is a statement that contains clauses and expressions and produces a result set.

    15 / 16
  17. Quick check

    For one order row, which combination identifies its customer and calculates an output total?

    1. AA result set used as a primary key

      A result set is query output; it does not identify the customer row in this design.

    2. BA semicolon and a NOT NULL rule

      A semicolon ends a statement, and NOT NULL alone neither connects rows nor calculates a total.

    3. CA matching foreign key and the value expression price * quantity

      Right. The foreign key connects the order to a customer, and the expression calculates one result value.

    16 / 16

  18. 8 quick checks · then the test

    In the app, finishing the quick checks opens this lesson’s 10-question test, and the ones you miss come back exactly when you’re about to forget them.

    Take this lesson’s test →

    Opens in your browser. Nothing to install.

    Start studying

    On your phone, with the same account

    Get it on Google Play Download on the App Store

The whole course, on your computer and your phone

Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.

Opens in your browser. Nothing to install.

Start studying

On your phone, with the same account

Get it on Google Play Download on the App Store

“This app is the ‘One Lake’ for preparing the certification exams. Really worth it.”

Abdelhak Y. Google Play review