cpap.com introduction to sql: create, read, update, delete

31
SQL Basics Create, Read, Update, Delete

Upload: johnnygoodman

Post on 07-Jul-2015

573 views

Category:

Self Improvement


1 download

DESCRIPTION

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

TRANSCRIPT

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

SQL BasicsCreate, Read, Update, Delete

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

Like This:

SELECT ID FROM OrderTbl;

What Does SQL Look Like?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Delete A Given OrderTbl Row

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

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

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

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

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

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

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

That's All For Today!

Next time:

● JOINs ● SQL modifiers (OR, AND, IN)● SQL symbols (%)● COUNT● Indexes and PKs● UNIONs (the free market variety...)