A great read in my quest on becoming a beter programmer: http://www.code-magazine.com/article.aspx?quickid=0805061&page=1
Thursday, March 31, 2011
Behavior-Driven Development
Wednesday, March 30, 2011
What is the difference between Left, Right, Outer and Inner Joins?
A Visual Explanation of SQL Joins
I thought Ligaya Turmelle's post on SQL joins was a great primer for novice developers. Since SQL joins appear to be set-based, the use of Venn diagrams to explain them seems, at first blush, to be a natural fit. However, like the commenters to her post, I found that the Venn diagrams didn't quite match the SQL join syntax reality in my testing.
I love the concept, though, so let's see if we can make it work. Assume we have the following two tables. Table A is on the left, and Table B is on the right. We'll populate them with four records each.
id name id name-- ---- -- ----1 Pirate 1 Rutabaga
2 Monkey 2 Pirate
3 Ninja 3 Darth Vader
4 Spaghetti 4 Ninja
Let's join these tables by the name field in a few different ways and see if we can get a conceptual match to those nifty Venn diagrams.
SELECT * FROM TableAINNER JOIN TableB ON TableA.name = TableB.nameid name id name-- ---- -- ----1 Pirate 2 Pirate3 Ninja 4 Ninja | |
SELECT * FROM TableAFULL OUTER JOIN TableB ON TableA.name = TableB.nameid name id name-- ---- -- ----1 Pirate 2 Pirate2 Monkey null null3 Ninja 4 Ninja4 Spaghetti null nullnull null 1 Rutabaga null null 3 Darth Vader | |
SELECT * FROM TableALEFT OUTER JOIN TableB ON TableA.name = TableB.nameid name id name-- ---- -- ----1 Pirate 2 Pirate2 Monkey null null3 Ninja 4 Ninja4 Spaghetti null null | |
SELECT * FROM TableALEFT OUTER JOIN TableBON TableA.name = TableB.nameWHERE TableB.id IS null id name id name-- ---- -- ----2 Monkey null null4 Spaghetti null null | |
SELECT * FROM TableAFULL OUTER JOIN TableBON TableA.name = TableB.nameWHERE TableA.id IS null OR TableB.id IS nullid name id name-- ---- -- ----2 Monkey null null4 Spaghetti null nullnull null 1 Rutabaga null null 3 Darth Vader |
There's also a cartesian product or cross join, which as far as I can tell, can't be expressed as a Venn diagram:
SELECT * FROM TableACROSS JOIN TableB
This joins "everything to everything", resulting in 4 x 4 = 16 rows, far more than we had in the original sets. If you do the math, you can see why this is a very dangerous join to run against large tables.
Another Explanation of SQL Joins
Simple Example: Lets say you have a Students table, and a Lockers table.
Each student can be assigned to a locker, so there is a "LockerNumber" column in the student table. More than one student could potentially be in a single locker, but especially at the begining of the school year, you may have some incoming students without lockers and some lockers that have no students assigned.
For the sake of this example, lets say you have 100 students, 70 of which have lockers. You have a total of 50 lockers, 40 of which have at least 1 student.
INNER JOIN is equivalent to "show me all students with lockers".
Any students without lockers, or any lockers without students are missing.
Returns 70 rows
Any students without lockers, or any lockers without students are missing.
Returns 70 rows
LEFT OUTER JOIN would be "show me all students, with their corresponding locker if they have one".
This might be a general student list, or could be used to identify students with no locker.
Returns 100 rows
This might be a general student list, or could be used to identify students with no locker.
Returns 100 rows
RIGHT OUTER JOIN would be "show me all lockers, and the students assigned to them if there are any".
This could be used to identify lockers that have no students assigned, or lockers that have too many students.
Returns 80 rows (list of 70 students in the 40 lockers, plus the 10 lockers with no student)
This could be used to identify lockers that have no students assigned, or lockers that have too many students.
Returns 80 rows (list of 70 students in the 40 lockers, plus the 10 lockers with no student)
FULL OUTER JOIN would be silly and probably not much use.
Something like "show me all students and all lockers, and match them up where you can"
Returns 110 rows (all 100 students, including those without lockers. Plus the 10 lockers with no student)
Something like "show me all students and all lockers, and match them up where you can"
Returns 110 rows (all 100 students, including those without lockers. Plus the 10 lockers with no student)
CROSS JOIN is also fairly silly in this scenario.
It doesn't use the linked "lockernumber" field in the students table, so you basically end up with a big giant list of every possible student-to-locker pairing, whether or not it actually exists.
Returns 5000 rows (100 students x 50 lockers). Could be useful (with filtering) as a starting point to match up the new students with the empty lockers.
It doesn't use the linked "lockernumber" field in the students table, so you basically end up with a big giant list of every possible student-to-locker pairing, whether or not it actually exists.
Returns 5000 rows (100 students x 50 lockers). Could be useful (with filtering) as a starting point to match up the new students with the empty lockers.
Labels:
SQL Joins
Friday, January 7, 2011
Setup Team Foundation Server 2010 with Sharepoint 2007 on Window Server 2008 SP2
Wow what a task!
If you have Office SharePoint 2007 no SP you need to follow these steps:
Next install SQL Server, SharePoint etc... steps:
Next install Team Foundation Server 2010 steps:
http://juliocasal.com/archive/2009/11/02/team-foundation-server-2010-beta-2-office-sharepoint-server-2007-single-server-installation-part-2.aspx
http://juliocasal.com/archive/2009/11/02/team-foundation-server-2010-beta-2-office-sharepoint-server-2007-single-server-installation-part-2.aspx
5Hours later and I am ready to roll!
Notes: Struggle on SSO Service Account: http://thorprojects.com/blog/archive/2008/08/02/moss-single-sign-on-setup-step-by-step.aspx
When creating a new TFS project and setting the SharePoint site up the user must be added to the SharePoint permissions:
- Using the TFS Setup account, navigate to http://myserver/sites/DefaultCollection
- Click on Site Actions and select Site settings.
- Under Users and Permissions, click the link Site collection administrators
- Added my account in the domain/account format and clicked ok
Excel Services was unable to load the workbook that you requested:
Give NT AUTHORITY\NETWORK SERVICE (db owner) on the database named "WSS_Content"
Give NT AUTHORITY\NETWORK SERVICE (db owner) on the database named "WSS_Content"
Tuesday, January 4, 2011
To change the data type of a column without dropping the table (SQL Server 2008):
SQL
ALTER TABLE [dbo].[tbl_001] ALTER COLUMN clm_001 nvarchar(50)
Labels:
SQL Server 2008,
type
Update data in one table with data from another table
Need to switch 2 columns that were wrongfully imported:
Step1
Create a copy of the table - CREATE TABLE [dbo].[Client2](....
Create a copy of the table - CREATE TABLE [dbo].[Client2](....
Step2
Inserted the data to the copied table -
INSERT INTO [dbo].[Client2]
Inserted the data to the copied table -
INSERT INTO [dbo].[Client2]
([UniqueClientID]
,[Latitude]
,[Longitude]
,[ClientName])
SELECT [UniqueClientID]
,[Latitude]
,[Longitude]
,[ClientName]
FROM [dbo].[Client]
Step3
Switch the 2 column data by updating
UPDATE [dbo].[Client]
Switch the 2 column data by updating
UPDATE [dbo].[Client]
set [Latitude] = [dbo].[Client2].[Longitude]
,[Longitude] = [dbo].[Client2].[Latitude]
From [dbo].[Client],[dbo].[Client2]
where [dbo].[Client].[UniqueClientID] = [dbo].[Client2].[UniqueClientID]
GO
Labels:
Update data
Subscribe to:
Comments (Atom)