Friday, February 25, 2011

SQL Outer Joins

You'll generally use the OUTER JOIN form that asks for all the rows from one table or result set and any matching rows from a second table or result set. To do this, you specify either a LEFT OUTER JOIN or a RIGHT OUTER JOIN. What's the difference between LEFT and RIGHT? In INNER JOIN on two tables, you name the first table, include the JOIN keyword, and then name the second table. When you begin building queries using OUTER JOIN, the SQL Standard considers the first table you name as the one on the "left," and the second table as the one on the "right." So, if you want all the rows from the first table and any matching rows from the second table, you'll use a LEFT OUTER JOIN. Conversely, if you want all the rows from the second table and any matching rows from the first table, you'll specify a RIGHT OUTER JOIN.

 SELECT Recipe_Classes.RecipeClassDescription,  
Recipes.RecipeTitle
FROM Recipe_Classes
LEFT OUTER JOIN Recipes
ON Recipe_Classes.RecipeClassID =
Recipes.RecipeClassID


If you do outer join and then inner join, it ignores the nulls. If you want all the information use all outer joins.

Practice 4 Visual Basic Loops

1. Put a textbox, a listbox, and a button on the form. The user must enter a number and press the button. When the button is pressed, it will add that many items to the listbox on the form.


   Private Sub theButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles theButton.Click  
Dim times As Double
times = CDbl(TextBox1.Text)
Dim i, sum As Integer
sum = 0
For i = 1 To times
sum = sum + 1
'show items in listbox
ListBox1.Items.Add(sum & " items")
Next
End Sub
End Class


2. If you were to put away a certain amount of money every month, how many years would it take you to save up $10,000. Use a textbox to gather the amount that is to be put away every month. Use a label to display the number of years it would take.

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pay, months, total, goal As Integer
Dim years As Decimal
pay = CInt(txtPay.Text)
goal = 10000
For total = 1 To goal
total += pay
months = months + 1
years = months / 12
Label3.Text = ("It will take " & years.ToString("f1") & " years to make the $10,000 goal.")
Next
End Sub
End Class


3. Write a program that will create a times table.

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
…..
You could use labels or buttons to hold the numbers. Generate these dynamically and add them to the Me.Controls collection.
You will have to use two loops, one inside of the other to get this to work.

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x, y As Integer
'Button = 20 pixels
x = 1
Button1.Visible = False
For x = 1 To 10
For y = 1 To 10
Dim newbutton As New Button
newbutton.Location = New Point(50 * x, 50 * y)
newbutton.Width = 40
newbutton.Text = x * y
Me.Controls.Add(newbutton)
Next
Next
End Sub
End Class


A loop within a loop operates in the inside loop until that loop is finished, then goes into the outer loop to get to the next level to run the inner loop again till it is finished.

Friday, February 18, 2011

SQL Inner Join Movie Databases

Inner Join Practice 3

Movie Quote Database.
You are one of those weird people who thinks in movie quotes. If it wasn't for movies, you would probably never speak at all. It's time to capture that master mind of yours and put it into electronic form. Create a database which will store movie quotes, the movie it is heard in, and the actor who said it.
•Find all the quotes for a particular movie and list the actor/actress - Jo

•Find a specific movie and actor/actress by entering in a phrase from a quote - Ben

•Find all the movies and quotes that were uttered by a particular actor/actress - Melissa

You should be able to find the following:
This database should be in 3rd normal form. Always be thinking: "If this information were to change, how many places would I have to make that change?" This doesn't apply for primary keys since they never change.


 --drop table Movies  
--drop table Actors
--drop table Quotes
--drop table xrefMovieActor
--drop table xrefActorQuote
--drop table xrefMovieQuotes
use DragonsQuotes
go
create table Quotes
(
QuoteID int identity (1,1) primary key
, Quote varchar(100)
)
Insert into Quotes (Quote) values ('Hakuna Matata')
Insert into Quotes (Quote) values ('Dat-da-dah! We''re dead.')
Insert into Quotes (Quote) values ('Well you shouldn''t have been trying to steal our Ozone.')
Insert into Quotes (Quote) values ('I am Iron Man. The suit and I are one.')
Insert into Quotes (Quote) values ('This is some rescue. When you came in here, didn''t you have a plan for getting out?')
select *
from Quotes
create table xrefActorQuote
(
xrefAQID int identity (1,1) primary key
, ActorID int
, QuoteID int
);
insert into xrefActorQuote (ActorID, QuoteID) values (1,1)
insert into xrefActorQuote (ActorID, QuoteID) values (2,2)
insert into xrefActorQuote (ActorID, QuoteID) values (3,3)
insert into xrefActorQuote (ActorID, QuoteID) values (4,4)
insert into xrefActorQuote (ActorID, QuoteID) values (5,5)
select *
from xrefActorQuote
create table Movies
(
MovieID int identity(1,1) not null primary Key
,MovieName varchar(100)
)
Insert into Movies
(
MovieName
)
values
(
'The Lion King'
)
Insert into Movies
(
MovieName
)
values
(
'How to Train Your Dragon'
)
Insert into Movies
(
MovieName
)
values
(
'Men in Black 2'
)
Insert into Movies
(
MovieName
)
values
(
'Iron Man 2'
)
Insert into Movies
(
MovieName
)
values
(
'Star Wars'
)
Select *
from Movies
create table xrefMovieQuotes
(
xrefMQID int identity(1,1) not null primary Key
,MovieID int
,QuotesID int
)
insert into xrefMovieQuotes(MovieID,QuotesID) values (1,1)
insert into xrefMovieQuotes(MovieID,QuotesID) values (2,2)
insert into xrefMovieQuotes(MovieID,QuotesID) values (3,3)
insert into xrefMovieQuotes(MovieID,QuotesID) values (4,4)
insert into xrefMovieQuotes(MovieID,QuotesID) values (5,5)
select * from xrefMovieQuotes
create table Actor
(
ActorID int identity (1,1) PRIMARY KEY,
ActorName varchar (40)
);
insert into Actor
(ActorName)
Values
('Rafiki')
insert into Actor
(ActorName)
Values
('Hiccup')
insert into Actor
(ActorName)
Values
('Will Smith')
insert into Actor
(ActorName)
Values
('Robert Downey Jr.')
insert into Actor
(ActorName)
Values
('Carrie Fisher')
select *
from Actor
create table xrefMovieActor
(
xrefMAID int identity (1,1) Primary Key,
MovieID int,
ActorID int
);
insert into xrefMovieActor
(MovieID, ActorID)
Values
('1', '1')
insert into xrefMovieActor
(MovieID, ActorID)
Values
('2', '2')
insert into xrefMovieActor
(MovieID, ActorID)
Values
('3', '3')
insert into xrefMovieActor
(MovieID, ActorID)
Values
('4', '4')
insert into xrefMovieActor
(MovieID, ActorID)
Values
('5', '5')
select *
from xrefMovieActor
select *
from xrefMovieActor
Select A.ActorName, M.MovieName
from xrefMovieActor xMA
inner join Actor A
on A.ActorID = xMA.ActorID
inner join xrefActorQuote xAQ
on A.ActorID = xAQ.ActorID
inner join Movies M
on M.MovieID = xMA.MovieID
where MovieName = 'Men in Black 2'
select *
from Movies M
inner join xrefMovieActor xrefMA
on M.MovieID = xrefMA.MovieID
inner join xrefMovieQuotes xrefMQ
on xrefMA.MovieID = xrefMQ.MovieID
inner join Quotes Q
on Q.QuoteID = xrefMQ.QuotesID
inner join Actor A
on A.ActorID = xrefMA.ActorID
where QUOTE like '%suit%'
select a.ActorName,m.MovieName
from Actor a
inner join xrefMovieActor x on x.ActorID = a.ActorID
inner join Movies m on m.MovieID = x.MovieID
where a.ActorName = 'Will Smith'

VB notes 2-18-11 Loops and Do Whiles

To count the number of check boxes checked on the form. 3 Check boxes on the form.


Dim nCount as integer = 0
for each element as object in me.controls
If TypeOf element Is CheckBox Then
If CType(element, CheckBox).Checked = True Then
nCount +=1
End If
End If
Next
txtCount.Text = nCount

Dim nControls As Integer = me.Controls.Count
Dim nCurrent As Integer = 0
Dim obj As Object
Do While nCurrent < nControls

obj = Me.Controls.Item(nCurrent)
If TypeOf ojb Is CheckBox Then
If CType(obj, CheckBox).Checked Then
nCount +=1
End If
End If
nCurrent +=1
txtCount.Text = nCount
If nCount > 1 Then
Exit Do
Loop

Wednesday, February 16, 2011

Web Design Notes 2-16-11

Classes are reusable, id's are not.

You must use a
for a different paragraph or different line.

Dreamweaver double spaces paragraphs automatically.

Become

Span tag on it's own does nothing.

External, internal, imbedded - three ways to do styles in a webpage.

Screen readers use the strong tag for bold text.

Monday, February 14, 2011

Inner Join 6 tables in SQL - YummyTables

 --drop table Brand  
--drop table Flavor
--drop table xrefFlavorBrand
--drop table sales
--drop table distributors
--drop table xrefDistFlavorBrand
--drop table xrefDistSales
use Yummys
go
create table Brand
(
BrandID int identity(1,1) primary key
,BrandName varchar(50)
,BrandLocation varchar(50)
)
insert into Brand (BrandName, BrandLocation) values ('BlueBunny', 'San Francisco')
insert into Brand (BrandName, BrandLocation) values ('PurpleMonkey', 'Detroit')
insert into Brand (BrandName, BrandLocation) values ('PinkHippo', 'Malibu')
insert into Brand (BrandName, BrandLocation) values ('GreenRabbit', 'Tucson')
insert into Brand (BrandName, BrandLocation) values ('RainbowLamb', 'Minneapolis')
select *
from Brand
create table flavor
(
Flavor_ID int identity (1,1) PRIMARY KEY,
flavor_name varchar (40),
descraption varchar (100),
quantity int
)
insert into flavor
(flavor_name, descraption, quantity)
values
('choclate', 'classic choclate', 2)
insert into flavor
(flavor_name, descraption, quantity)
values
('Vanilla', 'Plain Vanilla', 4)
insert into flavor
(flavor_name, descraption, quantity)
Values
('Neopolatin', '1/3 Choclate, 1/3 Vanilla. 1/3 Strawberry', 1)
insert into flavor
(flavor_name, descraption, quantity)
Values
('Coffee', 'Fresh Cround Coffee Flavor', 0)
insert into flavor
(flavor_name, descraption, quantity)
Values
('Sardine', 'Self-Explanatory', 7)
select *
from flavor
create table xrefFlavorBrand
(
xrefID int identity (1,1) primary key
, BrandID int
, FlavorID int
)
Insert into xrefFlavorBrand (BrandID, FlavorID) values (1,1)
Insert into xrefFlavorBrand (BrandID, FlavorID) values (2,2)
Insert into xrefFlavorBrand (BrandID, FlavorID) values (3,3)
Insert into xrefFlavorBrand (BrandID, FlavorID) values (4,4)
insert into xrefFlavorBrand (BrandID, FlavorID) values (5,5)
create table sales
(
SalesID int identity (1,1) primary key
, xrefID int
, ScoopNo int
, Price decimal(4,2)
, SalesDate int
)
insert into sales
(xrefID, ScoopNo, Price, SalesDate) values (1, 2, 2.00, 1/2/10)
insert into sales
(xrefID, ScoopNo, Price, SalesDate) values (2, 3, 3.00, 1/3/10)
insert into sales
(xrefID, ScoopNo, Price, SalesDate) values (3, 2, 2.00, 1/3/10)
insert into sales
(xrefID, ScoopNo, Price, SalesDate) values (2,3,3.00, 1/3/10)
insert into sales
(xrefID, ScoopNo, Price, SalesDate) values (4,2,2.00, 1/4/10)
insert into sales
(xrefID, ScoopNo, Price, SalesDate) values (5, 4, 4.00, 1/4/10)
insert into sales
(xrefID, ScoopNo, Price, SalesDate) values (1, 5, 5.00, 1/5/10)
insert into sales
(xrefID, ScoopNo, Price, SalesDate) values (4, 3, 3.00, 1/5/10)
select * from sales
create table xrefDistFlavorBrand
(
xrefDistSalesID int identity (1,1) primary key
,BrandID int
,FlavorID int
,distributor_ID int
)
Insert into xrefDistFlavorBrand(BrandID,FlavorID,distributor_ID) values(1,1,1)
Insert into xrefDistFlavorBrand(BrandID,FlavorID,distributor_ID) values(2,2,2)
Insert into xrefDistFlavorBrand(BrandID,FlavorID,distributor_ID) values(3,3,3)
insert into xrefDistFlavorBrand(BrandID,FlavorID,distributor_ID) values(4,4,4)
insert into xrefDistFlavorBrand(BrandID,FlavorID,distributor_ID) values (5,5,5)
select *
from xrefDistFlavorBrand
--here is the second table
create table xrefDistSales
(
xrefDistSalesID int identity (1,1) primary key
,BrandID int
,SalesID int
)
insert into xrefDistSales(BrandID,SalesID) values (1,1)
insert into xrefDistSales(BrandID,SalesID) values (2,2)
insert into xrefDistSales(BrandID,SalesID) values (3,3)
insert into xrefDistSales(BrandID,SalesID) values (4,4)
insert into xrefDistSales(BrandID,SalesID) values (5,5)
select *
from xrefDistSales
create table distributors
(
distributor_ID int identity (1,1) PRIMARY KEY,
DistributorName varchar (30)
);
insert into distributors
(DistributorName)
Values
('ND Icecream')
insert into distributors
(DistributorName)
Values
('GF Grocery')
insert into distributors
(DistributorName)
Values
('Henreys Foods')
insert into distributors
(DistributorName)
Values
('Mayville Grocery')
insert into distributors
(DistributorName)
Values
('Red River Valley Icecream')
select *
from distributors
--keep track of the number of tubs of ice cream on hand
Select *
from flavor FL
inner join xrefDistFlavorBrand xDFB
on FL.Flavor_ID = xDFB.FlavorID
where quantity <=1
--search distributors by flavor and brand, get a list of distributors
select *
from distributors D
inner join xrefDistFlavorBrand xDFB
on d.Distributor_ID = xDFB.Distributor_ID
inner join flavor F
on f.Flavor_ID = xDFB.FlavorID
inner join Brand B
on B.BrandID = xDFB.BrandID
where flavor_name = 'choclate'
and
BrandName = 'BlueBunny'
--Keep track of which flavors and brands were sold
select *
from sales S
inner join xrefDistSales xDS
on s.SalesID = xDS.SalesID
inner join xrefDistFlavorBrand xDFB
on xDS.xrefDistSalesID = xDFB.xrefDistSalesID
inner join flavor F
on f.Flavor_ID = xDFB.FlavorID
where f.flavor_name = 'vanilla'

Wednesday, February 9, 2011

Random comments

Each object has methods, properties and events.

Ctrl KC = comment out lines
Ctrl KU = un-comment lines

Do (while first)-
x = 1
Do while x < 10
x += 1
Loop

Do (while last) -
x = 1
Do while x < 10
x += 1
Loop while x < 10

For -
Dim n as Integer
for n = 2 to 10 step 2 - counts by 2, you can also do -2 to count backwards

Next

For each

While x < 10

End while

Friday, February 4, 2011

Inner Join 3 tables in SQL

 --drop table Brand  
--drop table Flavor
--drop table xrefFlavorBrand
use Yummys
go
create table Brand
(
BrandID int identity(1,1) primary key
,BrandName varchar(50)
,BrandLocation varchar(50)
)
insert into Brand (BrandName, BrandLocation) values ('BlueBunny', 'San Francisco')
insert into Brand (BrandName, BrandLocation) values ('PurpleMonkey', 'Detroit')
insert into Brand (BrandName, BrandLocation) values ('PinkHippo', 'Malibu')
insert into Brand (BrandName, BrandLocation) values ('GreenRabbit', 'Tucson')
insert into Brand (BrandName, BrandLocation) values ('RainbowLamb', 'Minneapolis')
select *
from Brand
create table flavor
(
Flavor_ID int identity (1,1) PRIMARY KEY,
flavor_name varchar (40),
descraption varchar (100)
)
insert into flavor
(flavor_name, descraption)
values
('choclate', 'classic choclate')
insert into flavor
(flavor_name, descraption)
values
('Vanilla', 'Plain Vanilla')
insert into flavor
(flavor_name, descraption)
Values
('Neopolatin', '1/3 Choclate, 1/3 Vanilla. 1/3 Strawberry')
insert into flavor
(flavor_name, descraption)
Values
('Coffee', 'Fresh Cround Coffee Flavor')
insert into flavor
(flavor_name, descraption)
Values
('Sardine', 'Self-Explanatory')
select *
from flavor
create table xrefFlavorBrand
(
xrefID int identity (1,1) primary key
, BrandID int
, FlavorID int
)
Insert into xrefFlavorBrand (BrandID, FlavorID) values (1,1)
Insert into xrefFlavorBrand (BrandID, FlavorID) values (2,2)
Insert into xrefFlavorBrand (BrandID, FlavorID) values (3,3)
Insert into xrefFlavorBrand (BrandID, FlavorID) values (4,4)
insert into xrefFlavorBrand (BrandID, FlavorID) values (5,5)
select *
from xrefFlavorBrand
select FL.*, xFB.BrandID, B.BrandName
from flavor FL
inner join xrefFlavorBrand xFB
on FL.Flavor_ID = xFB.xrefID
inner join Brand B
on xFB.BrandID = B.BrandID
where FL.flavor_name = 'Coffee'

Adding code for controls in Visual Basic

# Research and blog the following:

* add a button to a page programmatically
* add a textbox to a page programmatically
* add a label to a page programmatically
* add an item list to a page programmatically
* understand how to place these in a certain location on the form
* understand how to change the width and height
* understand how to set readonly and disabled
* understand how to set the text value


 Public Class Form1  
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' create button
Dim btn As New Button
' set some button properties
btn.Text = "go"
btn.Top = 45
btn.Left = 190
'add button to form
Me.Controls.Add(btn)
'create textbox
Dim dynamicText As TextBox = Nothing
dynamicText = New Windows.Forms.TextBox
dynamicText.Name = "TimeTextBox"
dynamicText.Location = New System.Drawing.Point(8, 8)
dynamicText.Size = New System.Drawing.Size(232, 20)
dynamicText.TabIndex = 0
Me.Controls.Add(dynamicText)
'create label
Dim lbl As New Label
lbl.SetBounds(10, 50, 100, 25)
lbl.Text = "Hello World!"
Me.Controls.Add(lbl)
'create listbox
Dim lstEmails As ListBox
lstEmails = New System.Windows.Forms.ListBox()
'
'lstEmails
'
lstEmails.IntegralHeight = False
lstEmails.Location = New Point(16, 75)
lstEmails.Name = "lstEmails"
lstEmails.Size = New System.Drawing.Size(264, 224)
lstEmails.TabIndex = 0
Me.Controls.Add(lstEmails)
'
End Sub
End Class

Wednesday, February 2, 2011

Word Problems 2 Spacely's Sprockets, Heat problem, Socks

1.) Mr. Spacely has been rethinking the company's shipping schemes. As an employee for Spacely's Sprockets, Spacely has asked you to write a quick program that will be used to determine shipping costs.
A total purchase of items totaling under $50, we charge $5 shipping.
A total purchase of items totaling $50 and over, there is no shipping charge.

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim purchase, shipping, total As Double
purchase = CDbl(txtpurchase.text)
shipping = 5
'Calculate total
If purchase >= 50 Then
total = FormatCurrency(purchase)
ElseIf purchase < 50 Then
total = purchase + shipping
End If
'Display total
MessageBox.Show("Your total is: " & FormatCurrency(total), "Please Pay")
End Sub
End Class


2.) You are looking for something to control your heat in your apartment and you discover there is NOT an app for that. It's about time that someone created one. You decide that you are the one to do it. Here's what you want to do.
-You want to turn the heat on when the temp has dropped below 72
-You also want to turn the AC on when the temp gets above 76
Your app should display in a message box if the heat is on, the AC is on, or if the system is idle.

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim temp As Double
temp = CDbl(txtpurchase.Text)
'Calculate total
If temp <= 72 Then
MessageBox.Show("The heat is on.", "Heat")
ElseIf temp > 72 And temp < 76 Then
MessageBox.Show("The system is idle", "Idle System")
ElseIf temp >= 76 Then
MessageBox.Show("The A/C is on.", "A/C")
End If
End Sub
End Class


3.) You are working on a clothing website where people can buy kids socks. It's really hard for the customers to know what size they should buy for what age. It would be a good idea for the customer to have a tool to input their child's age and have the website suggest a size for them. Write a tool where you can set the age as a variable and have it suggest on of the sizes below:
a.0-2 yrs -XS
b.3-4 yrs -S
c.5-8 yrs -M
d.9-12 yrs -L
e.13+ yrs -XL

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim age As Double
age = CDbl(txtpurchase.Text)
'Calculate total
If age <= 2 Then
MessageBox.Show("Your child's size is XS.", "XS")
ElseIf age >= 3 And age <= 4 Then
MessageBox.Show("Your child's size is Small", "Small")
ElseIf age >= 5 And age <= 8 Then
MessageBox.Show("Your child's size is Medium", "Medium")
ElseIf age >= 9 And age <= 12 Then
MessageBox.Show("Your child's size is Large", "Large")
ElseIf age >= 13 Then
MessageBox.Show("Your child's size is X-Large.", "X-Large")
End If
End Sub
End Class

Tuesday, February 1, 2011

Case Statement in Visual Basic

A case statement in Visual Basic:

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim num As Double
Select Case num
Case 5
txtResults.Text = "case 1"
Case 5 To 7
txtResults.Text = "case 2"
Case 7 To 12
txtResults.Text = "case 3"
End Select
End Sub
End Class

If Statements in Visual Basic

An if statement that makes one decision:

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim num1, num2, largerNum As Double
'Convert text box to double
num1 = CDbl(txtFirstNum.Text)
num2 = CDbl(txtSecondNum.Text)
'Find results
If num1 > num2 Then
largerNum = num1
Else
largerNum = num2
End If
'Display results
txtResults.Text = "The larger number is " & largerNum & "."
End Sub
End Class


An if statement where it's either or:


 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim message As String
message = "On a bad day, I have mood swings - but on a good day, I have the whole mood playground – Charles Rosenblum"
If txtmessage.Text.ToUpper = "Y" Then
MessageBox.Show(message, "Quote")
End If
MessageBox.Show("At first, I only laughed at myself. Then I noticed that life itself is amusing. I've been in a generally good mood ever since. – Marilyn vos Savant", "Bad Mood Quote")
End Sub
End Class


An if statement where many decisions are made:

 Public Class Form1  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declare variables
Dim gpa As Double = CDbl(txtGpa.Text)
Dim honors As String
If gpa >= 3.9 Then
honors = " summa cum laude."
ElseIf gpa >= 3.6 Then
honors = " magna cum laude."
ElseIf gpa >= 3.3 Then
honors = " cum laude."
ElseIf gpa >= 2 Then
honors = "."
End If
txtResults.Text = "You graduated" & honors
End Sub
End Class