Basic SQL

A quick overview of SQL: History, Concepts, and Syntax

What is SQL

SQL == Structured Query Language

source: http://en.wikipedia.org/wiki/SQL

What is SQL

SQL, and Relational Database Management Systems (RDBMS) have been the de-facto standard for data persistence for 30+ years

Currently, there are more than 100 RDBMS available, both proprietary and open-source.

Most, if not all, include some implementation of SQL as their query language.

source: http://en.wikipedia.org/wiki/List_of_relational_database_management_systems

Big Players in SQL

There are a number of RDBMS that you will run into regularly

Commercial / Proprietary

Open Source

SQL/RDBMS Concepts - Tables

A table consists of rows (also called records) and columns

Each row/record represents a single item

Each column represents a data point

Most tables will have one column which is considered the primary key

This value will uniquely identify a single row out of all the rows in the table

An Example

Here is an example table which represents people in a system:

id username first_name last_name
1 wont_u_b Fred Rogers
4 neuroman William Gibson
5 race Roger Bannon
6 harrywho Harry Houdini
7 whitequeen Emma Frost
8 shadowcat Kitty Pryde

SQL/RDBMS Concepts - Relations

You can model things using tables like this. Adding columns for all sorts of different data points

But what happens when not all of the items in a table share the same data points?

Or what if some of the items need to have more than one of a particular data point?

Leaving columns empty in a row wastes memory and slows down querying. Use relations to solve these types of problems

Types of Relations

There are three basic types of relationships:

One-to-one relationships
Best used to represent aspects of an item which are not core to it. Like user (id, password) -> user_profile (preferences, name, address)
Many-to-one relationships
Used to represent relationships of ownership or belonging. Like product -> manufacturer or book -> author
Many-to-many relationships
Used to represent associations or membership. Like users -> groups or items -> orders

SQL Relations - ∞ -> 1

Many-to-one relationships are modelled using Foreign Keys

The many table has a column which holds the primary key of the row from the one table:

Consider the relationship of books to author:

Books -> Author

People:

id username first_name last_name
4 neuroman William Gibson
6 harrywho Harry Houdini

Books:

id title author
1 Miracle Mongers and their Methods 6
2 The Right Way to Do Wrong 6
3 Pattern Recognition 4

SQL Relations - 1 -> 1

One-to-one relationships are really just a special case of Many-to-one, and are also modelled with Foreign Keys

In this case, the column on the related table which holds the primary key of the target table has an additional unique constraint, so that only one related record can exist

The classic purpose is for data that doesn't need to be accessed often, and is unique per record

Consider the example of birth records:

Birth Record -> Person

People:

id username first_name last_name
1 wont_u_b Fred Rogers
4 neuroman William Gibson
5 race Roger Bannon

Birth Records:

id person date place
1 1 March 20, 1928 Latrobe, PA
2 4 March 17, 1948 Conway, SC
3 5 April 1, 1954 Wilmette, IL

SQL Relations - ∞ -> ∞

Many-to-many relations are a bit trickier.

You can't have a multi-valued field, so there's no way to define a foreign key-like construct that would work

Instead, this relationship is modelled using a join table, which has two foreign key fields, one for each side of the relation.

Beyond these two, other columns can add data points describing the qualities of the relation itself

Group Memberships

People:

id username first_name last_name
7 whitequeen Emma Frost
8 shadowcat Kitty Pryde

Groups:

id name
1 Hellfire Club
2 X-Men

Membership:

id person group active
1 7 1 False
2 7 2 True
3 8 2 True

SQL Syntax

The syntax of SQL can be broken into constructs:

img/sql_anatomy.png

image: CC-BY-SA by Ferdna http://en.wikipedia.org/wiki/File:SQL_ANATOMY_wiki.svg

SQL Syntax - Subsets

SQL statements can be thought of as belonging to one of several subsets

Data Definition
Statements in this subset concern the structure of the database itself:
CREATE TABLE "jos_groups" (
  "group_id" character varying(32) NOT NULL,
  "name" character varying(255) NOT NULL,
  "description" text NOT NULL
)

SQL Syntax - Subsets

SQL statements can be thought of as belonging to one of several subsets

Data Manipulation
Statements in this subset concern the altering of data within the database:
UPDATE people
    SET first_name='Bill'
    WHERE id=4;

SQL Syntax - Subsets

SQL statements can be thought of as belonging to one of several subsets

Data Query
Statements in this subset concern the retrieval of data from within the database:
SELECT user_id, COUNT(*) c
  FROM (SELECT setting_value AS interests, user_id
          FROM user_settings
          WHERE setting_name = 'interests') raw_uid
  GROUP BY user_id HAVING c > 1;