Your SlideShare is downloading. ×
CPAP.com Introduction To SQL: Create, Read, Update, Delete
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

CPAP.com Introduction To SQL: Create, Read, Update, Delete

511
views

Published on

Ever wonder how a database works and how to interact with it? Learn about INSERTs, SELECTs, UPDATEs and DELETEs in this intro presentation.

Ever wonder how a database works and how to interact with it? Learn about INSERTs, SELECTs, UPDATEs and DELETEs in this intro presentation.

Published in: Self Improvement

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
511
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. SQL BasicsCreate, Read, Update, Delete
  • 2. What Does SQL Look Like?Like This:SELECT ID FROM OrderTbl;
  • 3. Where Do I Go To Type SQLStatements?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 toolWe are going to be using a web baseddatabase 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, ColumnsColumns: ID, Name, Address, ProductThe 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, RowsRow 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 KeywordsSELECT - Read From TableUPDATE - Edit Table aka Edit A RowDELETE - Delete Row From TableINSERT - Write To Table aka Add Rows
  • 8. Reading All Data From OrderTblReading = SELECT statementExample Format:SELECT <columns> FROM <table>;
  • 9. Reading All Data From OrderTblHere is the SQL to get all row data from thewhole 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
  • 10. Breaking Down SQL Statement PartsOriginal Statement:SELECT ID, Name, Address, ProductFROM OrderTbl;SELECT - "Read From Table"ID, Name, Address, Product - "These Columns"FROM - "Which Table?"OrderTbl - "This Table"
  • 11. Reading Only 2 ColumnsSELECT ID, ProductFROM OrderTbl;Result: ID Product 1 Mask A 2 Mask B 3 Mask C
  • 12. The ID, A Very Special ColumnThe ID row can be used to talk about the wholerow...ID 1s 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
  • 13. The WHERE commandStructure:WHERE <column name> = <value>Examples:WHERE ID = 1WHERE Street = 111 StreetWHERE Product = Mask A;
  • 14. SELECT + WHERE CommandSELECT ID, ProductFROM OrderTblWHERE ID = 1;In English:Read the ID and Product column values in anyrow of OrderTbl where the ID columns value is1
  • 15. Reading Only 2 ColumnsSELECT ID, ProductFROM OrderTblWHERE ID = 1;Result: ID Product 1 Mask A
  • 16. SELECTing All Columns EasilyInstead of:SELECT ID, Name, Address, ProductFROM OrderTbl;SQL gives you the shortcut:SELECT * FROM OrderTbl;
  • 17. SELECTing All Columns EasilyWarning:If you use the * command on large tables, it willresult in very slow processing times.It is a shortcut you will see used, but it isclearer to the reader and a better practice totype out only the column names you need
  • 18. Editing An Existing Row Of OrderTblEdit = UPDATE statementExample Format:UPDATE <table name>SET <column1> = <value1>WHERE <column2> = <value2>;
  • 19. Editing An Existing Row Of OrderTblHere is the SQL to update the row 2s namecolumn to Johnny:UPDATE OrderTbl SET Name = JohnnyWHERE ID = 2;English:Update the Name column of the OrderTbl tablerow with an ID of 2 to Johnny
  • 20. Editing An Existing Row Of OrderTblBefore: ID Name 2 BarryUPDATE OrderTbl SET Name = JohnnyWHERE ID = 2;After: ID Name 2 Johnny
  • 21. Delete A Given OrderTbl RowDelete = DELETE statementExample Format:DELETE FROM <table name>WHERE <column> = <value>;
  • 22. Delete A Given OrderTbl RowHere is the SQL to delete the third row:DELETE FROM OrderTbl WHERE ID = 3;English:Delete all the rows from OrderTbl where the IDis 3.
  • 23. Delete A Given OrderTbl RowBefore: ID Name 2 Barry 3 CharlieDELETE FROM OrderTbl WHERE ID = 3After: ID Name 2 Barry
  • 24. Delete A Given OrderTbl Row
  • 25. Delete A Given OrderTbl RowWhen Crafting DELETE Statements:● There is no undo button● Always give your delete statement a WHERE clause
  • 26. Delete A Given OrderTbl RowBad:DELETE FROM OrderTbl;Better:DELETE FROM OrderTbl WHERE ID = 3;
  • 27. Writing A New Row To OrderTblCreate = INSERT statementExample Format:INSERT INTO <table name>(<column1>, <column2>, <column3>)VALUES(<value1>, <value2>, <value3>);
  • 28. Writing A New Row To OrderTblINSERT INTO OrderTbl(Name, Address, Product)VALUES(Danny, Street 444, Mask D);Create a new row where the column Name isDanny, Address is Street 444 and Product isMask D
  • 29. Writing A New Row To OrderTblBefore, nothing.INSERT INTO OrderTbl (Name, Address,Product) VALUES (Danny, Street 444, MaskD);After ID Name Address Product 4 Danny Street 444 Mask D
  • 30. Auto Incrementing IDsNotice that we did not define the ID column inour INSERT statement, yet the value after itwas run was 4.Each column of your table can be defined as anumber or text. If you define it as a number,you can add an attribute to it called autoincrement.This makes every next row increment by 1. TheID row of next INSERT will be 5.
  • 31. Thats All For Today!Next time:● JOINs● SQL modifiers (OR, AND, IN)● SQL symbols (%)● COUNT● Indexes and PKs● UNIONs (the free market variety...)