2. What Does SQL Look Like?
Like This:
SELECT ID FROM OrderTbl;
3. Where Do I Go To Type SQL
Statements?
You need a database.
Then you need one of these connected to it:
● A mySQL command prompt
● An installed app database query tool
● A web based database query tool
We are going to be using a web based
database query tool named phpMyAdmin.
4. An Example Database Table
OrderTbl
ID Name Address Product
1 Alf 111 Street Mask A
2 Barry 222 Street Mask B
3 Charlie 333 Street Mask C
5. Table Layout, Columns
Columns: ID, Name, Address, Product
The ID Column Contains Values: 1, 2, 3
ID Name Address Product
1 Alf 111 Street Mask A
2 Barry 222 Street Mask B
3 Charlie 333 Street Mask C
6. Table Layout, Rows
Row 1:
● ID is 1
● Name is Alf
● Address is 111 Street
● Product is Mask A
ID Name Address Product
1 Alf 111 Street Mask A
2 Barry 222 Street Mask B
3 Charlie 333 Street Mask C
7. Common SQL Keywords
SELECT - Read From Table
UPDATE - Edit Table aka Edit A Row
DELETE - Delete Row From Table
INSERT - Write To Table aka Add Rows
8. Reading All Data From OrderTbl
Reading = SELECT statement
Example Format:
SELECT <columns> FROM <table>;
9. Reading All Data From OrderTbl
Here is the SQL to get all row data from the
whole table:
SELECT ID, Name, Address, Product
FROM OrderTbl;
Result:
1 Alf 111 Street Mask A
2 Barry 222 Street Mask B
3 Charlie 333 Street Mask C
10. Breaking Down SQL Statement Parts
Original Statement:
SELECT ID, Name, Address, Product
FROM OrderTbl;
SELECT - "Read From Table"
ID, Name, Address, Product - "These Columns"
FROM - "Which Table?"
OrderTbl - "This Table"
11. Reading Only 2 Columns
SELECT ID, Product
FROM OrderTbl;
Result:
ID Product
1 Mask A
2 Mask B
3 Mask C
12. The ID, A Very Special Column
The ID row can be used to talk about the whole
row...
ID 1's row or row 1 has Alf, 111 Street, Mask A
ID Name Address Product
1 Alf 111 Street Mask A
2 Barry 222 Street Mask B
3 Charlie 333 Street Mask C
14. SELECT + WHERE Command
SELECT ID, Product
FROM OrderTbl
WHERE ID = 1;
In English:
Read the ID and Product column values in any
row of OrderTbl where the ID column's value is
1
15. Reading Only 2 Columns
SELECT ID, Product
FROM OrderTbl
WHERE ID = 1;
Result:
ID Product
1 Mask A
16. SELECTing All Columns Easily
Instead of:
SELECT ID, Name, Address, Product
FROM OrderTbl;
SQL gives you the shortcut:
SELECT * FROM OrderTbl;
17. SELECTing All Columns Easily
Warning:
If you use the * command on large tables, it will
result in very slow processing times.
It is a shortcut you will see used, but it is
clearer to the reader and a better practice to
type out only the column names you need
18. Editing An Existing Row Of OrderTbl
Edit = UPDATE statement
Example Format:
UPDATE <table name>
SET <column1> = <value1>
WHERE <column2> = <value2>;
19. Editing An Existing Row Of OrderTbl
Here is the SQL to update the row 2's name
column to Johnny:
UPDATE OrderTbl SET Name = 'Johnny'
WHERE ID = 2;
English:
Update the Name column of the OrderTbl table
row with an ID of 2 to 'Johnny'
20. Editing An Existing Row Of OrderTbl
Before:
ID Name
2 Barry
UPDATE OrderTbl SET Name = 'Johnny'
WHERE ID = 2;
After:
ID Name
2 Johnny
21. Delete A Given OrderTbl Row
Delete = DELETE statement
Example Format:
DELETE FROM <table name>
WHERE <column> = <value>;
22. Delete A Given OrderTbl Row
Here is the SQL to delete the third row:
DELETE FROM OrderTbl WHERE ID = 3;
English:
Delete all the rows from OrderTbl where the ID
is 3.
23. Delete A Given OrderTbl Row
Before:
ID Name
2 Barry
3 Charlie
DELETE FROM OrderTbl WHERE ID = 3
After:
ID Name
2 Barry
25. Delete A Given OrderTbl Row
When Crafting DELETE Statements:
● There is no undo button
● Always give your delete statement a
WHERE clause
26. Delete A Given OrderTbl Row
Bad:
DELETE FROM OrderTbl;
Better:
DELETE FROM OrderTbl WHERE ID = 3;
27. Writing A New Row To OrderTbl
Create = INSERT statement
Example Format:
INSERT INTO <table name>
(<column1>, <column2>, <column3>)
VALUES
(<value1>, <value2>, <value3>);
28. Writing A New Row To OrderTbl
INSERT INTO OrderTbl
(Name, Address, Product)
VALUES
('Danny', 'Street 444', 'Mask D');
Create a new row where the column Name is
Danny, Address is Street 444 and Product is
Mask D
29. Writing A New Row To OrderTbl
Before, nothing.
INSERT INTO OrderTbl (Name, Address,
Product) VALUES ('Danny', 'Street 444', 'Mask
D');
After
ID Name Address Product
4 Danny Street 444 Mask D
30. Auto Incrementing IDs
Notice that we did not define the ID column in
our INSERT statement, yet the value after it
was run was 4.
Each column of your table can be defined as a
number or text. If you define it as a number,
you can add an attribute to it called auto
increment.
This makes every next row increment by 1. The
ID row of next INSERT will be 5.
31. That's All For Today!
Next time:
● JOINs
● SQL modifiers (OR, AND, IN)
● SQL symbols (%)
● COUNT
● Indexes and PKs
● UNIONs (the free market variety...)