Head First SQL

A Brain-Friendly Guide

Lynn Beighley

Publisher: O'Reilly, 2007, 571 pages

ISBN: 978-0-596-52684-9

Keywords: Databases

Last modified: May 4, 2019, 10:47 p.m.

What will you learn from this book?

In today's world, data is power, but the real secret to success is having power over your data. Head First SQL take you to the heart of the SQL language, from basic syntax queries using INSERT and SELECT to hard core database manipulation with subqueries, joins and transactions. By the time you finish reading, you will not only understand effective and efficient database design and creation, you'll be querying, normalizing and joining data like a pro. And you'll be a true master of your data.

  • Intro
    Your brain on SQL
  1. Data and Tables:
    A place for everything
    • Defining your data
    • Look at your data in categories
    • Your database viewed through x-ray specs…
    • Databases contain connected data
    • Tables Up Close
    • Take command!
    • Setting the table: the CREATE TABLE statement
    • Creating a more complicated table
    • Look how easy it is to write SQL
    • Create the my_contacts table, finally
    • Your table is ready
    • Take a meeting with some data types
    • Your table, DESCribed
    • You can't recreate an existing table or database!
    • Out with the old table, in with the new
    • To add data to your table, you'll use the INSERT statement
    • Create the INSERT statement
    • Variations on an INSERT statement
    • Columns without values
    • Peek at your table with the SELECT statement
    • SQL Exposed: Confessions of a NULL
    • Controlling your inner NULL
    • NOT NULL appears in DESC
    • Fill in the blanks with DEFAULT
    • Your SQL Toolbox
  2. The SELECT Statement:
    Gifted data retrieval
    • Date or no date?
    • A better SELECT
    • What the * is that?
    • How to query your data types
    • More punctuation problems
    • Unmatched single quotes
    • Single quotes are special characters
    • INSERT data with single quotes in it
    • SELECT specific columns to limit results
    • SELECT specific columns for faster results
    • Combining your queries
    • Finding numeric values
    • (not) Smooth Comparison Operators
    • Finding numeric data with Comparison Operators
    • Text data roping with Comparison Operators
    • To be OR not to be
    • The difference between AND and OR
    • Use IS NULL to find NULLs
    • Saving time with a single keyword: LIKE
    • The call of the Wild(card)
    • Selecting ranges using AND and comparison operators
    • Just BETWEEN us… there's a better way
    • After the dates, you are either IN…
    • …or you are NOT IN
    • More NOT
    • Your SQL Toolbox
  3. DELETE and UPDATE:
    A change will do you good
    • Clowns are scary
    • Clown tracking
    • How our clown data gets entered
    • Bonzo, we've got a problem
    • Getting rid of a record with DELETE
    • Using our new DELETE statement
    • DELETE rules
    • The INSERT-DELETE two step
    • Be careful with your DELETE
    • The trouble with imprecise DELETE
    • Change your data with UPDATE
    • UPDATE rules
    • UPDATE is the new INSERT-DELETE
    • UPDATE in action
    • Updating the clown's movements
    • UPDATE your prices
    • All we need is one UPDATE
    • Your SQL Toolbox
  4. Smart Table Design:
    Why be normal?
    • Two fishy tables
    • A table is all about relationships
    • Atomic data
    • Atomic data and your tables
    • Atomic data rules
    • Reasons to be normal
    • The benefits of normal tables
    • Clowns aren't normal
    • Halfway to 1NF
    • PRIMARY KEY rules
    • Getting to NORMAL
    • Fixing Greg's table
    • The CREATE TABLE we wrote
    • Show me the table
    • Time-saving command
    • The CREATE TABLE with a PRIMARY KEY
    • 1, 2, 3… auto incrementally
    • Adding a PRIMARY KEY to an existing table
    • ALTER TABLE and add a PRIMARY KEY
    • Your SQL Toolbox
  5. ALTER:
    Rewriting the past
    • We need to make some changes
    • Table altering
    • Extreme table makeover
    • Renaming the table
    • We need to make some plans
    • Retooling our columns
    • Structural changes
    • ALTER and CHANGE
    • Change two columns with one SQL statement
    • Quick! DROP that column
    • A closer look at the non-atomic location column
    • Look for patterns
    • A few handy string functions
    • Use a current column to fill a new column
    • How our UPDATE and SET combo works
    • Your SQL Toolbox
  6. Advanced SELECT:
    Seeing your data with new eyes
    • Dataville Video is reorganizing
    • Problems with our current table
    • Matching up existing data
    • Populating the new column
    • UPDATE with a CASE expression
    • Looks like we have a problem
    • Tables can get messy
    • We need a way to organize the data we SELECT
    • Try a little ORDER BY
    • ORDER a single column
    • ORDER with two columns
    • ORDER with multiple columns
    • An orderly movie_table
    • Reverse the ORDER with DESC
    • The Girl Sprout® cookie sales leader problem
    • SUM can add them for us
    • SUM all of them at once with GROUP BY
    • AVG with GROUP BY
    • MIN and MAX
    • COUNT the days
    • SELECT DISTINCT values
    • LIMIT the number of results
    • LIMIT to just second place
    • Your SQL Toolbox
  7. Multi-table Database Design:
    Outgrowing your table
    • Finding Nigel a date
    • All is lost… But wait
    • Think outside of the single table
    • The multi-table clown tracking database
    • The clowntracking database schema
    • How to go from one table to two
    • Connecting your tables
    • Constraining your foreign key
    • Why bother with foreign keys?
    • CREATE a table with a FOREIGN KEY
    • Relationships between tables
    • Patterns of data: one-to-one
    • Patterns of data: when to use one-to-one tables
    • Patterns of data: one-to-many
    • Patterns of data: getting to many-to-many
    • Patterns of data: we need a junction table
    • Patterns of data: many-to-many
    • Finally in 1NF
    • Composite keys use multiple columns
    • Shorthand notations
    • Partial functional dependency
    • Transitive functional dependency
    • Second normal form
    • Third normal form (at last)
    • And so, Regis (and gregslist) lived happily ever after
    • Your SQL Toolbox
  8. Joins and Multi-table Operations:
    Can't we all just get along?
    • Still repeating ourselves, still repeating…
    • Prepopulate your tables
    • We got the "table ain't easy to normalize" blues
    • The special interests (column)
    • Keeping interested
    • UPDATE all your interests
    • Getting all the interests
    • Many paths to one place
    • CREATE, SELECT and INSERT at (nearly) the same time
    • CREATE, SELECT and INSERT at the same time
    • What's up with that AS?
    • Column aliases
    • Table aliases, who needs em?
    • Everything you wanted to know about inner joins
    • Cartesian join
    • Releasing your inner join
    • The inner join in action: the equijoin
    • The inner join in action: the non-equijoin
    • The last inner join: the natural join
    • Joined-up queries?
    • Table and Column Aliases Exposed: What are you hiding from?
    • Your SQL Toolbox
  9. Subqueries:
    Queries Within Queries
    • Greg gets into the job recruiting business
    • Greg's list gets more tables
    • Greg uses an inner join
    • But he wants to try some other queries
    • Subqueries
    • We combine the two into a query with a subquery
    • As if one query wasn't enough: meet the subquery
    • A subquery in action
    • Subquery rules
    • A subquery construction walkthrough
    • A subquery as a SELECT column
    • Another example: Subquery with a natural join
    • A noncorrelated subquery
    • SQL Exposed: Choosing the best way to query
    • A noncorrelated subquery with multiple values: IN, NOT IN
    • Correlated subqueries
    • A (useful) correlated subquery with NOT EXISTS
    • EXISTS and NOT EXISTS
    • Greg's Recruiting Service is open for business
    • On the way to the party
    • Your SQL Toolbox
  10. Outer Joins, Self Joins, and Unions:
    New maneuvers
    • Cleaning up old data
    • It's about left and right
    • Here's a left outer join
    • Outer joins and multiple matches
    • The right outer join
    • While you were outer joining…
    • We could create a new table
    • How the new table fits in
    • A self-referencing foreign key
    • Join the same table to itself
    • We need a self-join
    • Another way to get multi-table information
    • You can use a UNION
    • UNION is limited
    • UNION rules in action
    • UNION ALL
    • Create a table from your union
    • INTERSECT and EXCEPT
    • We're done with joins, time to move on to…
    • Subqueries and joins compared
    • Turning a subquery into a join
    • A self-join as a subquery
    • Greg's company is growing
    • Your SQL Toolbox
  11. Constraints, Views, and Transactions:
    Too many cooks spoil the database
    • Greg's hired some help
    • Jim's first day: Inserting a new client
    • Jim avoids a NULL
    • Flash forward three months
    • CHECK, please: Adding a CHECK CONSTRAINT
    • CHECKing the gender
    • Frank's job gets tedious
    • Creating a view
    • Viewing your views
    • What your view is actually doing
    • What a view is
    • Inserting, updating, and deleting with views
    • The secret is to pretend a view is a real table
    • View with CHECK OPTION
    • Your view may be updatable if…
    • When you're finished with your view
    • When bad things happen to good databases
    • What happened inside the ATM
    • More trouble at the ATM
    • It's not a dream, it's a transaction
    • The classic ACID test
    • SQL helps you manage your transactions
    • What should have happened inside the ATM
    • How to make transactions work with MySQL
    • Now try it yourself
    • Your SQL Toolbox
  12. Security:
    Protecting your assets
    • User problems
    • Avoiding errors in the clown tracking database
    • Protect the root user account
    • Add a new user
    • Decide exactly what the user needs
    • A simple GRANT statement
    • GRANT variations
    • REVOKE privileges
    • REVOKING a used GRANT OPTION
    • REVOKING with precision
    • The problem with shared accounts
    • Using your role
    • Role dropping
    • Using your role WITH ADMIN OPTION
    • Combining CREATE USER and GRANT
    • Greg's List has gone global!
    • Your SQL Toolbox
    • How about a Greg's List in your city?
    • Use SQL on your own projects, and you too could be like Greg!
  • Leftovers
    The Top Ten Topics (we didn't cover)
    1. Get a GUI for your RDBMS
    2. Reserved Words and Special Characters
    3. ALL, ANY, and SOME
    4. More on Data Types
    5. Temporary Tables
    6. Cast your data
    7. Who are you? What time is it?
    8. Useful numeric functions
    9. Indexing to speed things up
    10. 2-minute PHP/MySQL
  • MySQL installation
    Try it out for yourself
    • Get started, fast!
    • Instructions and Troubleshooting
    • Steps to Install MySQL on Windows
    • Steps to Install MySQL on Mac OS X
  • Tools roundup
    All your new SQL tools
    • Symbols
    • A–B
    • C–D
    • E–I
    • L–N
    • O–S
    • T–X

Reviews

Head First SQL

Reviewed by Roland Buresund

Decent ****** (6 out of 10)

Last modified: Nov. 23, 2010, 2:13 p.m.

A decent take on teaching SQL to dummies. The only irritating thing (except the extreme verbosity, that is) is the focus on MySQL and its quirks, as it degrades from the experience.

To sum it up, this is more a Absolute Dummies book than the books labelled as such.

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required