CPAP.com Introduction To SQL: Create, Read, Update, Delete
-
Upload
johnnygoodman -
Category
Self Improvement
-
view
570 -
download
1
description
Transcript of CPAP.com Introduction To SQL: Create, Read, Update, Delete
![Page 1: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/1.jpg)
SQL BasicsCreate, Read, Update, Delete
![Page 2: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/2.jpg)
Like This:
SELECT ID FROM OrderTbl;
What Does SQL Look Like?
![Page 3: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/3.jpg)
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.
![Page 4: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/4.jpg)
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
![Page 5: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/5.jpg)
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
![Page 6: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/6.jpg)
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
![Page 7: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/7.jpg)
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
![Page 8: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/8.jpg)
Reading All Data From OrderTbl
Reading = SELECT statement
Example Format:
SELECT <columns> FROM <table>;
![Page 9: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/9.jpg)
Reading All Data From OrderTbl
Here is the SQL to get all row data from the whole table:
SELECT ID, Name, Address, ProductFROM OrderTbl;
Result:1 Alf 111 Street Mask A
2 Barry 222 Street Mask B
3 Charlie 333 Street Mask C
![Page 10: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/10.jpg)
Breaking Down SQL Statement Parts
Original Statement:
SELECT ID, Name, Address, ProductFROM OrderTbl;
SELECT - "Read From Table"ID, Name, Address, Product - "These Columns"FROM - "Which Table?"OrderTbl - "This Table"
![Page 11: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/11.jpg)
Reading Only 2 Columns
SELECT ID, ProductFROM OrderTbl;
Result:
ID Product
1 Mask A
2 Mask B
3 Mask C
![Page 12: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/12.jpg)
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
The ID, A Very Special Column
ID Name Address Product
1 Alf 111 Street Mask A
2 Barry 222 Street Mask B
3 Charlie 333 Street Mask C
![Page 13: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/13.jpg)
The WHERE command
Structure:
WHERE <column name> = <value>
Examples:
WHERE ID = 1WHERE Street = '111 Street'WHERE Product = 'Mask A';
![Page 14: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/14.jpg)
SELECT + WHERE Command
SELECT ID, Product FROM OrderTblWHERE ID = 1;
In English:
Read the ID and Product column values in any row of OrderTbl where the ID column's value is 1
![Page 15: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/15.jpg)
Reading Only 2 Columns
SELECT ID, ProductFROM OrderTblWHERE ID = 1;
Result:
ID Product
1 Mask A
![Page 16: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/16.jpg)
SELECTing All Columns Easily
Instead of:
SELECT ID, Name, Address, ProductFROM OrderTbl;
SQL gives you the shortcut:
SELECT * FROM OrderTbl;
![Page 17: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/17.jpg)
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
![Page 18: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/18.jpg)
Edit = UPDATE statement
Example Format:
UPDATE <table name> SET <column1> = <value1>WHERE <column2> = <value2>;
Editing An Existing Row Of OrderTbl
![Page 19: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/19.jpg)
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'
Editing An Existing Row Of OrderTbl
![Page 20: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/20.jpg)
Before:
UPDATE OrderTbl SET Name = 'Johnny' WHERE ID = 2;
After:
Editing An Existing Row Of OrderTbl
ID Name
2 Barry
ID Name
2 Johnny
![Page 21: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/21.jpg)
Delete A Given OrderTbl Row
Delete = DELETE statement
Example Format:
DELETE FROM <table name> WHERE <column> = <value>;
![Page 22: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/22.jpg)
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.
Delete A Given OrderTbl Row
![Page 23: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/23.jpg)
Before:
DELETE FROM OrderTbl WHERE ID = 3
After:
Delete A Given OrderTbl Row
ID Name
2 Barry
3 Charlie
ID Name
2 Barry
![Page 24: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/24.jpg)
Delete A Given OrderTbl Row
![Page 25: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/25.jpg)
When Crafting DELETE Statements:
● There is no undo button
● Always give your delete statement a WHERE clause
Delete A Given OrderTbl Row
![Page 26: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/26.jpg)
Bad:
DELETE FROM OrderTbl;
Better:
DELETE FROM OrderTbl WHERE ID = 3;
Delete A Given OrderTbl Row
![Page 27: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/27.jpg)
Writing A New Row To OrderTbl
Create = INSERT statement
Example Format:
INSERT INTO <table name> (<column1>, <column2>, <column3>)VALUES(<value1>, <value2>, <value3>);
![Page 28: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/28.jpg)
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
![Page 29: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/29.jpg)
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
![Page 30: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/30.jpg)
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.
![Page 31: CPAP.com Introduction To SQL: Create, Read, Update, Delete](https://reader033.fdocuments.in/reader033/viewer/2022052910/559b85841a28ab29458b475a/html5/thumbnails/31.jpg)
That's All For Today!
Next time:
● JOINs ● SQL modifiers (OR, AND, IN)● SQL symbols (%)● COUNT● Indexes and PKs● UNIONs (the free market variety...)