SQL 101
SQL is a simple languadge with a Simple Syntax to manage Relational Databases
SQL Statements:
A statement is the unit of execution in SQL
An SQL statement usually includes 1 or more Clauses
This specifies which table the Statement will use
This specifies which rows will be selected
The WHERE clause requires an EXPRESSION: Logical Expression:
The 4 Fundamental Database functions are CRUD
Create
Read
Update
Delete
The SELECT
statement is used for ALL STATEMENTS that retrieve values. - (This is the R in CRUD)
The INSERT
statement is used to ADD A ROW to a TABLE - (This is the C in CRUD)
The UPDATE
statement is used to CHANGE DATA - (This is the U in CRUD)
The DELETE
statemnt is used to DELETE ROWS from a TABLE - (This is the D in CRUD)
The WHERE
clause is used to select which row or rows are to be UPDATED
Purpouse of a Database is to organize your data and make it available in convinient from. SQL is a Languadge for managing a RELATIONAL DATABASE. This is organized in 2 dimentional tables comprised of ROWS and COLUMNS
A Database has Tables. Can have 1 or more tables which are organized in ROWS and COLUMNS:
A ROW
is like an individual RECORD
in the table. A COLUMN
is like a FIELD
in the table. EACH ROW
in the TABLE
has a UNIQUE KEY
. Sometimes the KEY
is HIDDEN
but a TABLE
must have one.
When a COLUMN
is used as a UNIQUE KEY
it is often called the PRIMARY KEY
In the above example the DEPT_ID COLUMN
is used as the PRIMARY KEY
.
Note: Different Engines do this differently but there is ALWAYS a UNIQUE KEY
.
The TABLES
are RELATED
by the TABLES KEYS
In this example the ADMIN TABLE
has COLUMNS
for DEPT_ID
that make reference to the DEPT TABLE
This is called aFORIGN KEY
because it refers to the KEYS of OTHER TABLES
.
These relationships allow to make JOINT QUERIES
, to treat ROWS
and SEVERAL TABLSE
as JOINT UNITS
.
SQL Statement Analysis
SELECT Statement:
This uses a SELECT statement to retrieve information. Simple quotations
' '
are used to select a STRING.AS is a KEYWORD.
When you execute the Query it changes the COLUMN name to Result.
To SELECT all from a TABLE use:
If we want them in a Different order you can use ORDER BY:
If we don’t want all the rows you can specify the names denominated by a “,”
:
Example: This might not result right so you might want to give an alias for LifeExpectancy to be AS Life Expectancy
Note: You have to use Double Quote “ “ The Quotes are needed as the IDENTIFIER has SPACES in it, if it wouldn’t then you don’t even need the Quotes.
This selects the 3 columns NAME, CONTINENT, REGION from the Table Country where the Continent Column is Europe.
If we want them in a particular order we can use ORDER BY for example:
If we want to LIMIT the results we can use the LIMIT CLAUSE:
This LIMITS the results to the first 5 results. So the above, orders in Alphabetical order by NAME and then shows only the first 5 RESULTS as opposed to the 46 ROWS without LIMIT.
If we want to skip the first 5 but want the next 5 we can use the OFFSET CLAUSE:
This RENAMES the NAME Column as COUNTRY
You can also swap the order by changing the order of the COLUMNS after the SELECT STATEMENT:
COUNT function allows you to count the ROWS within a table.
You can limit the RESULTS by doing
This will COUNT the rows only where the population is greater then 1 mil.
To limit by other factors too you can use AND
COUNT (*) is a Special case as it counts all the rows, if we use COUNT (LifeExpectancy) it will have a different number as it will count ONLY THE ROWS WITH DATA IN THEM.
INSERT Statement:
To insert Values into a table you can use the insert statement:
AFTER:
This inserts the values in each field. When you specify the name of the column you have to order the values as well.
I.e. Name = Fred Flintstone.
You can also insert information just in some fields and the other ones will be denoted by NULL:
UPDATE Statement:
TO CHANGE DATA in a TABLE we use the UPDATE STATEMENT:
IF THE WHERE CLAUSE DOES NOT EXIST IT WILL DO IT TO THE ENTIRE TABLE! SO YOU HAVE TO LIMIT IT!
This will populate the Address field and Zip field with the strings provided where the ID of the entry is 5.
To update already populated field it is the same command but with new strings:
To add NULL values you will have to specify this in the UPDATE statement:
MAKE SURE NOT TO SET THEM AS STRINGS AS THESE WILL BE POPULATED AS SUCH.
DELETE Statement:
This deletes from the customer TABLE the entry with ID = 4 and then SELECTS everything to see if this has been deleted by the user.
The part between the () is the SCHEMA
Syntax: COULMN NAME = A and the TYPE of the COLUM = INTEGER
THE LAST ROWN WITHIN THE () CANNOT HAVE A COMMA (,) - This will generate a syntax error.
TO DELETE A TABLE we use the DROP TABLE Statement:
This deletes the table
This does not return an error if the table does not exist.
Inserting Rows:
Inserts values only in Column B and C leaving Column A with the NULL value.
If you want to insert a table with no values in it use:
This creates a empty row with the NULL values
TO ADD VALUES FROM A DIFFERENT TABLE YOU CAN DO:
This selects the data from the ITEM TABLE to place it in each column a = id b = name c = description THIS ADDS ALL ROWS FROM THE TABLE SPECIFIED.
DELETING ROWS:
This will delete ROW where the INTEGER value = 3 on the A COLUMN.
DELETING ROWS IS DESTRUCTIVE SO IT IS GOOD TO AUDITION THIS BEFORE WITH:
The NULL value – Is not a value, is the absence of a value. To test for NULL you have to use
The opposite of NULL is:
To CREATE a TABLE that does not accept NULL values use:
If you do:
It will result in a ERROR:
This is because we delimited the B COLUMN to not accept NULL and we are not inserting a value in it with the INSERT above
CONSTRAINING COLUMNS:
To add constraints when creating a TABLE you can do:
THIS WILL FAIL. It fails because we have set a constraint for the C COLUMN to be any value except NULL.
And then we are trying to insert values in A & B but NOT C and the C Column does not accept to be NULL.
DEFAULT VALUE:
NOW with this when we fail to add data to C it will set the DEFAULT value of the entry to ‘panda’.
UNIQUE VALUE:
THIS WILL FAIL because we have set the A COLUMN to accept only UNIQUE values and then we are trying to insert the same values 2 times. Fails with: UNIQUE constraint failed test.a
IF WE CHANGE THE VALUE FOR A IT WILL WORK:
NULL is Exempted from the UNIQUE constraint.
You can also combine CONSTRAINTS:
CHANGING A SCHEMA:
If you need to change the schema for example add a new COLUM you can do:
ID COLUMNS:
When creating a table:
The ID COLUMN will be used as a KEY and it will give SEQUENCIAL UNIQUE NUMBERS to then field. It does this whether you have it or not, these are very common in RELATIONAL DATABASES. It makes it easier to create database RELATIONS between tables and rows.
FILTERING DATA:
THE WHERE CLAUSE ALLOWS YOU TO FILTER DATA: KIND OF LIKE PIPE IN LINUX:
The SELECT STATEMENT will bring DATA from the COLUMNS Name, Continent, Population from the COUNTRY TABLE
WHERE Population is less then 100 000 and it will ORDER it BY DESCENDING order of the POPULATION COLUM
This will do the same as above but it will also include the POPULATION COLUMN where the NULL STATE is found.
OR is a powerful BOOLEAN operator that allows you to combine MULTIPLE EXPRESSIONS such as:
This looks for Population that is less the 100.000 OR the STATE in NULL
The other powerful operator is AND This is like piping a pipe in Linux:
This statement will check that both conditions are met. The Population has to be less then 100.000 AND the Continent has to be Oceania
THE LIKE OPERATIOR
% is a wildcard quivalant to * in Linux
Basically what it means * island* Anything before and after as long as it contains the STRIG ISLAND
You can also do:
Another wildcard is _ which is the equivalent of . In Linux
A is the second character followed by any other letter STRINGS.
The IN operator:
This will select the COLUMNS specified after SELECT from the Country Table WHERE the Continents are Europe or Asia
REMOVING DUPLICATES:
In order to check for Entries that are not duplicates you can use the DISTINCT statement:
ORDERING OUTPUT:
You can use the ORDER BY Statement to order things more specifically:
This sorts Alphabetically but Descendent (Last in First Out)
This sorts Alphabetically but Ascendent (First in Frist Out)
You can also order by continent first and then by name with
This orders by Continent first in Descending order, then Region and Name ASC
CONDITIONAL EXPRESSIONS:
In standard SQL 0 is considered FALSE and anything that IS NOT 0 is Considered TRUE
CASE is usefull to check for values. For example the above tests if the A COLUMN is Anything but 0 and then you will get the THEN clause = True
If the value is 0 then you will get the ELSE clause = False
UNDERSTANDING JOIN:
The JOIN command is useful for executing queries from multiple tables that have RELATIONS:
Most Common JOIN is the INNER JOIN that only takes data/values that are in both tables:
Less Common but still useful is OUTER JOIN (which can be left or right) This takes the values that are common and the values that are in the table specified (LEFT or RIGHT)
FULL OUTER JOIN combines the effects of a LEFT AND RIGHT OUTER JOIN:
Basically what it does is SELECTS the DESCRIPTION COLUMN from the RIGHT TABLE with an alias of L and then merges it VIA JOIN with the RIGHT table and then DISPLAYS THE ID VALUES THAT MATCH.
To make this an OUTER JOIN you have to specify LEFT JOIN:
This brings the values of the INNER JOIN above but also adds the values that do not match from the LEFT table.
STRINGS: If you need to use single Quotes in a string you will have to use 2 of them like this: SELECT ‘Here’’s a single quote mark’;
Length:
We can also specify the number of characters to retruns for example:
TRIM: Trims text:
You can also capitalize or lower case strings by using the LOWER or UPPER arguments:
INTEGER TYPES:
INTEGER (precision)
DECIMAL (precision, scale)
MONEY (precision, scale)
REAL TYPES:
REAL (precision)
FLOAT (precision)
PRECISION vs SCALE:
Last updated