A quick overview of SQL: History, Concepts, and Syntax
SQL == Structured Query Language
source: http://en.wikipedia.org/wiki/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
There are a number of RDBMS that you will run into regularly
Commercial / Proprietary
Open Source
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
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 |
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
There are three basic types of relationships:
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:
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 |
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:
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 |
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
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 |
The syntax of SQL can be broken into constructs:
image: CC-BY-SA by Ferdna http://en.wikipedia.org/wiki/File:SQL_ANATOMY_wiki.svg
SQL statements can be thought of as belonging to one of several subsets
CREATE TABLE "jos_groups" ( "group_id" character varying(32) NOT NULL, "name" character varying(255) NOT NULL, "description" text NOT NULL )
SQL statements can be thought of as belonging to one of several subsets
UPDATE people SET first_name='Bill' WHERE id=4;
SQL statements can be thought of as belonging to one of several subsets
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;