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

Post on 07-Jul-2015

573 Views

Category:

Self Improvement

1 Downloads

Preview:

Click to see full reader

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

SQL BasicsCreate, Read, Update, Delete

Like This:

SELECT ID FROM OrderTbl;

What Does SQL Look Like?

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.

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

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

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

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

Reading All Data From OrderTbl

Reading = SELECT statement

Example Format:

SELECT <columns> FROM <table>;

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

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"

Reading Only 2 Columns

SELECT ID, ProductFROM OrderTbl;

Result:

ID Product

1 Mask A

2 Mask B

3 Mask C

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

The WHERE command

Structure:

WHERE <column name> = <value>

Examples:

WHERE ID = 1WHERE Street = '111 Street'WHERE Product = 'Mask A';

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

Reading Only 2 Columns

SELECT ID, ProductFROM OrderTblWHERE ID = 1;

Result:

ID Product

1 Mask A

SELECTing All Columns Easily

Instead of:

SELECT ID, Name, Address, ProductFROM OrderTbl;

SQL gives you the shortcut:

SELECT * FROM OrderTbl;

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

Edit = UPDATE statement

Example Format:

UPDATE <table name> SET <column1> = <value1>WHERE <column2> = <value2>;

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'

Editing An Existing Row Of OrderTbl

Before:

UPDATE OrderTbl SET Name = 'Johnny' WHERE ID = 2;

After:

Editing An Existing Row Of OrderTbl

ID Name

2 Barry

ID Name

2 Johnny

Delete A Given OrderTbl Row

Delete = DELETE statement

Example Format:

DELETE FROM <table name> WHERE <column> = <value>;

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

Before:

DELETE FROM OrderTbl WHERE ID = 3

After:

Delete A Given OrderTbl Row

ID Name

2 Barry

3 Charlie

ID Name

2 Barry

Delete A Given OrderTbl Row

When Crafting DELETE Statements:

● There is no undo button

● Always give your delete statement a WHERE clause

Delete A Given OrderTbl Row

Bad:

DELETE FROM OrderTbl;

Better:

DELETE FROM OrderTbl WHERE ID = 3;

Delete A Given OrderTbl Row

Writing A New Row To OrderTbl

Create = INSERT statement

Example Format:

INSERT INTO <table name> (<column1>, <column2>, <column3>)VALUES(<value1>, <value2>, <value3>);

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

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

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.

That's All For Today!

Next time:

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

top related