Date post: | 16-Jul-2015 |
Category: |
Software |
Upload: | billteaches |
View: | 193 times |
Download: | 0 times |
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
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 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?
NameDescriptionTeacherVideos (1 to many Relationship)Students (1 to Many
Relationship)
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)
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
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)
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
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’)
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