Book description
Is your data dragging you down? Are your tables all tangled up? Well we've got the tools to teach you just how to wrangle your databases into submission. Using the latest research in neurobiology, cognitive science, and learning theory to craft a multi-sensory SQL learning experience, Head First SQL has a visually rich format designed for the way your brain works, not a text-heavy approach that puts you to sleep.
Maybe you've written some simple SQL queries to interact with databases. But now you want more, you want to really dig into those databases and work with your data. Head First SQL will show you the fundamentals of SQL and how to really take advantage of it. We'll take you on a journey through the language, from basic INSERT statements and SELECT queries to hardcore database manipulation with indices, joins, and transactions. We all know "Data is Power" - but we'll show you how to have "Power over your Data". Expect to have fun, expect to learn, and expect to be querying, normalizing, and joining your data like a pro by the time you're finished reading!
Publisher resources
Table of contents
- Head First SQL
- Dedication
- A Note Regarding Supplemental Files
- Advance Praise for Head First SQL
- Praise for other Head First books
- Praise for the Head First Approach
- Author of Head First SQL
-
How to use this Book: Intro
- Who is this book for?
- Who should probably back away from this book?
- We know what youâre thinking.
- And we know what your brain is thinking.
- Metacognition: thinking about thinking
- Hereâs what WE did
- Hereâs what YOU can do to bend your brain into submission
- Read me
- The technical review team
- Acknowledgments
-
1. Data and Tables: A place for everything
- Defining your data
- Look at your data in categories
- Whatâs in a database?
- Your database viewed through x-ray specs...
- Databases contain connected data
- 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
- 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?
- Making contact
- 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 data
- The old way
- SELECT specific columns to limit results
- SELECT specific columns for faster results
- Doughnut ask what your table can do for you...
- Ask what you can do for your doughnut
- Combining your queries
- Finding numeric values
- Once is enough
- Smooth Comparison Operators
- Finding numeric data with Comparison Operators
- Text data roping with Comparison Operators
- Selecting your ingredients
- To be OR not to be
- The difference between AND and OR
- Use IS NULL to find NULLs
- Meanwhile, back at Gregâs place...
- Saving time with a single keyword: LIKE
- The call of the Wild(card)
- Thatâs more LIKE it
- 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
- 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
- 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
- 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
- Why change anything?
- The query worked really well
- It worked too well
- Ignoring the problem isnât the answer
- Too many bad matches
- Use only the first interest
- A possible match
- Mis-matched
- Add more interest columns
- Starting over
- All is lost...
- ... But wait
- Think outside of the single table
- The multi-table clown tracking database
- The clown_tracking database schema
- An easier way to diagram your tables
- How to go from one table to two
- Linking your tables in a diagram
- Connecting your tables
- Foreign key facts
- 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
- Patterns of data: fixing gregs_list
- Not in first normal form
- Finally in 1NF
- Composite keys use multiple columns
- Even superheros can be dependent
- Shorthand notations
- Superhero dependencies
- Partial functional dependency
- Transitive functional dependency
- Second normal form
- We might be 2NF already...
- Third normal form (at last)
- And so, Regis (and gregs_list) lived happily ever after
- The End
- 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?
- 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
- Subquery rules
- Subquery rules
- A subquery construction walkthrough
- A subquery as a SELECT column
- Another example: Subquery with a natural join
- A noncorrelated subquery
- 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!
- A. Leftovers: The Top Ten Topics (we didnât cover)
- B. MySQL Installation: Try it out for yourself
- C. Tools Roundup: All your new SQL tools
- D. Â
- Index
- About the Author
- Copyright
Product information
- Title: Head First SQL
- Author(s):
- Release date: August 2007
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9780596526849
You might also like
book
Head First SQL, 2nd Edition
What will you learn from this book? Do you have an abundance of data but don't …
video
Learning SQL
Almost 3 Hours of Video Instruction Your complete SQL video course: Master hands-on, real-world skills for …
book
Learning SQL, 3rd Edition
As data floods into your company, you need to put it to work right away—and SQL …
book
Practical SQL
"Practical SQL is an approachable and fast-paced guide to SQL (Structured Query Language), the standard programming …