+ All Categories
Home > Software > Sql for beginners Slideshare for my Udemy Course Coupon Code inside.

Sql for beginners Slideshare for my Udemy Course Coupon Code inside.

Date post: 16-Jul-2015
Category:
Upload: billteaches
View: 193 times
Download: 0 times
Share this document with a friend
57
SQL For Beginners By Bill Sims
Transcript

SQL For BeginnersBy Bill Sims

What is a Database

A Place To Store Your Stuff

A Place to Retrieve your Stuff

A Way to Analyze Your Stuff

What Elements Make Up A Database

Tables

Fields

Char

Varchar

Int

Decimal

Views

Stored Procedures

Functions

Consider This Use Case

You want to be the Next Udemy What Questions Do You Ask

What kind of data do I need at its very basic

CustomersTeachersClassesVideos

Anatomy Of A Table

Identity ColumnsAlways Unique In A Table

Foreign Key ColumnsLink To Data in another Table

Data ColumnsThe data that you are storing in this

table

A Tables Important Datatypes

Int

Varchar

Decimal

Designing Your First Database

What Makes Up A Customer ?

Identity Column

Email Address

Password

Name

Address

Introducing The Customers Table

ID Email Password Name Address

1 [email protected] Abc123 Bob 1234 Wes G Street

2 [email protected] Abc123 Sally 1234 Wes G Street

3 [email protected] Abc123 Steve 1234 Wes G Street

4 [email protected] Abc123 Joe 1234 Wes G Street

What Makes Up A Teacher?

Identity Column

Email Address

Password

Name

Address

What to do when you have 2 Tables that are similar

Rename Customer to Person

And add a Type Column

Identity Column

Email Address

Password

Name

Address

Type

How Do You Use The TypeID Column

We Have 2 Types Of Users

1) A Customer

2) A Teacher

On Customer Records we will put a 1 in the TypeID Column

On Teacher Records we will put a 2 in the TypeID Column

Introducing The Perons Table

ID Email Password Name Address Type

1 [email protected] Abc123 Bob 1234 Wes G Street

1

2 [email protected] Abc123 Sally 1234 Wes G Street

2

3 [email protected] Abc123 Steve 1234 Wes G Street

2

4 [email protected] Abc123 Joe 1234 Wes G Street

1

Is Less Tables Better?

The Answer Is The Simpler Your Data The Better

You want a database that is easy to

Use

Maintain

Report On

You Don’t Want To Over Simplify Your Data

Lets Make Our Video Table

Question to ask: Who Owns The Videos

The Teacher Owns them!

Identity Column

VideoName

VideoPath

PersonID(Teacher)

Video Table

ID Name Path PersonID

1 What is a Database C:\blah\video1.avi 2

2 How Do you Create One

C:\blah\video2.avi 3

3 Why Do I Create One C:\blah\video3.avi 2

What Makes up a Class?

NameDescriptionTeacherVideosStudents

What Makes up a Class?

NameDescriptionTeacherVideos (1 to many Relationship)Students (1 to Many

Relationship)

Questions!Can The Same Video Be In 2 Classes?

We Can’t Store More Than 1 Student In A

Record?

Lets Use A Different Table To Make The Links

Class Table

ID Name Description PersonID

1 Teach You SQL I Will Teach You ….

2

2 Teach You Java

I Will Teach You ….

3

3 Teach You C# I Will Teach You ….

3

Foreign Keys

A foreign key is a key used to link two tables together. This is sometimes called a referencing key.

Foreign Key is a column or a combination of columns whose values match a Primary Key in a different table.

StudentClass Table

The Goal Is To Link Table People to Table Classes

These are the fields we need to do that

Identity

Foreign Key to Person (personID)

ForeignKey to Class (classID)

StudentClass

ID PersonID ClassID

1 1 1

2 4 2

3 1 3

Why Does This Work?

VideoClass Table

Identity

ForeignKey to Video (videoID)

Foreign Key to Class (classID)

VideoClass Table

ID VideoID ClassID

1 1 1

2 4 2

3 1 3

Lets Talk About The Foreign Keys

ID VideoID ClassID

1 1 1

2 4 2

3 1 3

Anatomy Of A Select Statement

Select

This is the command

*

This is the what

From [Table]

This is the bin that the Data is Stored In

Where ID = 5

These are the rules that have to be followed to get the RIGHT data

Your The Boss Telling Your Secretary To Get Something Out Of The Folder

BOSS Yells Go Get Me A Piece Of Paper From The Drawer

This is the command Go Get Me = Select

Secretary Yells Back What Drawer (this is the From Statement

Think of the drawer as the table

Boss Yells Back “The Drawer With The Classes in it”

Secretary Yells Back Which Piece Of Paper Do You Want

Think of This Part as the Where Clause

Boss Yells Back ‘That Class That Steve Taught” Where PersonID = 3

Lets Do an Exercise

In Your SQL Management Studio I want you to do the following

1) Write the Select Statement that will Find all the classes that Sally teaches

2) Write the Select Statement That will Find all the classes that Steve teaches

3)Write a Select Statement that Returns Only The Email Address Of each Person

4) Write That Same Select Statement but Alias Email Address As UserName

Write the Select Statement that will Find all the classes that Sally teaches

What Do we Know?

We Know That Sally is ID = 2

The Query Would Look Like

Select * from Class where ID = 2

Write the Select Statement That will Find all the classes that Steve teaches

What Do we Know?

We Know That Steve is ID = 3

The Query Would Look Like

Select * from Class where ID = 3

Write a Select Statement that Returns Only The Email Address Of each Person

What Do we Know?

We Know That the Fieldname we want is Email

The Query Would Look Like

Select Email from Class

Write That Same Select Statement But Alias Email Address As UserName

What Do we Know?

We Know That the Fieldname we want is Email

We also Know We want to Alias it as “UserName”

The Query Would Look Like

Select Email as UserName from Class

Other Things You Can Do In a Select Statement

Add 2 columns

Multiply 2 columns

Divide 2 columns

Subtract 2 columns

Concatenate 2 columns

Substring – Get a specific set of characters from a string

Length of a string

Order By

Single Field Order By

Order by ID Asc

Order by ID Desc

Multiple Field Order By

Order By Type, Name

Order by PersonID, Name

Lets Dive Into The Where Clause

Where Is the Start of the Filter

Example Where Clause

Where Field = 1

Where Field <> 1

Where Field > 1

Where Field <1

Where Field IS NULL

Where Field Is Not Null

Lets go into detail on the Like statement

Like Statements are Used in the Where Clause

Like allows you to do very basic Regular Expressions

The In Statement

IN Statement

Takes A List

Where Field in (1, 2, 3)

Where Field in (‘Sally’, ‘John’)

Or Takes A SubQuery

Where Field In (select ID from Class)

Basic Analytics On Your Data

Count

Max

Min

Sum

Avg

Sample Analytics

Select Avg(ID) from Class

SElect Count(Address) from Person

SElect Sum(ID) from Person

SElect Max(ID) from Person

SElect Min(ID) from Person

When Would You Use These?

Boss

Hey Can you Tell Me How Many People We Have

SElect Count(*) from Person

Hey Can you Tell Me How Many Teachers We Have

SElect Count(*) from Person Where Type = 2

Group By

Sub Queries

What is a Sub – Query

Why Use a Sub Query

Lets Try A Sub Query

What Is A Sub-Query

A Subquery is a Select within a Select or a From or a Where

Examples:

Select ID, (Select Name From Person Where ID = Class.PersonID), PersonID From Class

Select ID, PersonID From (Select * from Class) C

Select ID, PersonID From Class Where PersonID in (Select ID from Person Where Name = ‘Sally’)

Why Use a Sub Query

To quickly get more data from another Table

To Optimize a SQL Statement

Lets Try Subqueries

Inner Joins

Inner Joins

Usually the Concept of Inner Joins and Foreign Keys have a lot to do with each other.

When Using an Inner Join you are telling the database I have these 2 tables

These 2 tables have a relationship Where Value1= Value2.

If Value1 = Value2 These 2 records are related to each other. And I want you to show the Row.

When using an Inner Join It will not show any Values From Either table for rows where this case is not True.

ExampleClass Person

If we want to Join these 2 tables and get the teachers for the ClassesWe would have to Join these 2 tables on Class.PersonID = Person.ID

When we do that we get access to all the Data where Class.PersonID = Person.ID

Lets type this in.

Select * from ClassInner Join Person on Person.ID = Class.PersonID

Left Outer Joins

Views

What Are Views Used For

You can create a view In place of a commonly run query.

Stored Procedures


Recommended