Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 26th, 2009, 03:44 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

I understood the first part but not the second. In any case thank you for your help. I am going to go with my design for now as I don't understand your suggestions. I will definatly do my best to have a go at what you suggested when I have finished this.

And read up on normalisation. Also is there a way to store just a date in a database? I tried creating a user defined data type but I can't seem to get it too work.. It said it was created successfulyl in SQL Server 2005 but when I attached the database back into visual studio it doesn't recognise the new datatype.

I was using this as a template to have a go..
 
Old May 26th, 2009, 05:17 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
Originally Posted by Will View Post
Ok thank you I will check that out? Is it a website? I will google it. Incidently What is the best way to store just a date in a database? I had put datetime but that doesn't work as I am just wanting to store the date. I also just want to store the time in another field. I can't get a datatype for this though?
Sorry, missed the question...

Use the DATETIME datatype to store just the date with a "midnight" time. That way, you'll not need to do any conversions for any of the date math that you will eventually do.
__________________
--Jeff Moden
 
Old May 26th, 2009, 05:38 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

I did just have it as a datetime but is caused an overflow mathmatical error or something to that effect?
 
Old May 26th, 2009, 06:06 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Including that problem I have 2 others which aren't really problems but questions.

1)
How do I associate a ChildId from the Children table with all the other information in the other tables? Example I want the Emergency Contact table to have a ChildID which relates to the Child in the Children table.

Currently I have done a diagram and I have created Foreign Key constraints from the ChildId in the Children table to the ChildId column in the relevant tables so it has data integrity and you can't delete stuff from the other tables without deleting the child from the children table first. However I am unsure if this will also make the ChildId automatically fill itself and create the relationship?

2)
I am inserting data using parameters seen in this code:
Code:
'Declare Variables
Dim strNPFName As String
        Dim strNPLName As String
        Dim strNPR As String
        Dim strNFName2 As String
        Dim strNPLName2 As String
        Dim strNPR2 As String
        Dim strNPFName3 As String
        Dim strNPLName3 As String
        Dim strNPR3 As String
        Dim strNPFName4 As String
        Dim strNPLName4 As String

       'CollectionDisallowed
        strNPFName = txtNotPermittedFName1.Text
        strNPLName = txtNotPermittedLName1.Text
        strNPR = txtRelationNotPermitted1.Text
        strNFName2 = txtNotPermittedFName2.Text
        strNPLName2 = txtNotPermittedLName2.Text
        strNPR2 = txtRelationNotPermitted2.Text
        strNPFName3 = txtNotPermittedFName3.Text
        strNPLName3 = txtNotPermittedLName3.Text
        strNPR3 = txtRelationNotPermitted3.Text
        strNPFName4 = txtNotPermittedFName4.Text
        strNPLName4 = txtNotPermittedLName4.Text
        strNPR4 = txtRelationNOtPermitted4.Text

'Insert into collection disallowed
        sqlComm.CommandText = "INSERT INTO CollectionDisallowed VALUES (@LastName, @FirstName, @RelationshipToChild);"
        sqlComm.Parameters.AddWithValue("@LastName", strNPFName)
        sqlComm.Parameters.AddWithValue("@FirstName", strNPLName)
        sqlComm.Parameters.AddWithValue("@RelationshipToChild", strRelationship)
        'Find out about multiple insertions
So basically this code will insert LastName, FirstName and RelationshipToChildValues into the database creating one entry. However I also want to insert the other 9 Values into the same fields but as seperate entires however still relevant to the same child.

Example:

Entry One

ChildID: 2
LastName: Blogs
FirstName: Joe
RelationshipToChild: Parent

Entry Two
ChildId: 2
LastName: Smith
FirstName: Bob
RelationshipToChild: Friend

So two different entries related to the same child. Is this possible? If so the only way I can think to do this is with another 3 seperate insert statements, is there a better way?

And the third problem is the one in the previous post a mathmatical error overflow, why would that be occuring. I think it is because the field on the form is only inserting a date and not a time aswell how do I get around this?

Thank you for your help so far, I have tried to descirbe my problems as completley as I can and what I think I can do to solve them I just don't know if they would work or not?
 
Old May 26th, 2009, 08:00 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi Jeff,

What exactly do you mean by a mapping table between the contact and address table?

Thanks,
Richard
 
Old May 26th, 2009, 08:34 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
Originally Posted by rstelma View Post
Hi Jeff,

What exactly do you mean by a mapping table between the contact and address table?

Thanks,
Richard
That type of table goes by many names... cross reference table is another name and there are others but don't remember them all because I don't use those other names.

Think of it as in the following example... you have a Customer table with CustomerID's and you have a Address table with Address ID's. The Customer table has no columns in it that identify which addresses belong to the customer. Likewise, the Address table has no columns in it to identify which customers live at a given address. The two tables know absolutely nothing about each other.

Enter the mapping or cross reference table. It'll have as little as two columns and both will be a part of the primary key. The two columns will be CustomerID and AddressID. Customers can have more than one Address and Addresses can have more than one Customer. Each actual combination is simply stored in the mapping table as a pair of ID's. You can add two other columns to make it the most efficient to handle when customers move and keep a history of their moves. Add a StartDate and EndDate column to the mapping table.

Adding the date columns is a technique know as a Type 2 SCD (Slowly Changing Dimension). Please see the following URL for a more detailed explanation of SCD's...

http://en.wikipedia.org/wiki/Slowly_...mension#Type_2

Needless to say, I strongly recommend the addition of the date columns.
__________________
--Jeff Moden
 
Old May 26th, 2009, 08:38 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
Originally Posted by Will View Post
I am going to go with my design for now as I don't understand your suggestions.
Not trying to be a smart guy here but every person I know who has ever said that has suffered greatly at the hand of their own database somewhere down the line. I seriously urge you not to proceed until you have a full understanding of normalization.
__________________
--Jeff Moden
 
Old May 26th, 2009, 08:57 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi Jeff,

Thanks for your answer. I don't know what they're 'supposed' to be called. In my little shop we call them intermediate tables. That's what I thought you meant but wasn't sure since you were suggesting for a table between the Contact and Address table. We are not usually interested in previous addresses. This can get really knit picky but I probably still wouldn't put a mapping table there. Since I would need a whole other row in the address table I'd probably just keep my Contact ID's in the address table and have an option somewhere in the application to look at previous addresses. That column would never be called 'ID' either. I hope Will is listening. ID is a reserved word in SQL and can lead to all kinds of trouble. Besides you won't know which ID you are dealing with after a while. I usually use a word from the table name like ContactID.

I do use mapping tables quite a bit though and I understand why you would use one there.

I just wanted to make sure you were talking about what I thought you might be talking about.... by another name.

Here are a couple of links, Will, to normalization tutorials. It may look daunting at first but the concepts are really not all that difficult and the amount of time you put into learning even the basic techniques will save you untold hours or frustration, anger and self mutilation.

http://www.phlonx.com/resources/nf3/#invoice_order

http://en.wikipedia.org/wiki/Database_normalization

Richard
 
Old May 26th, 2009, 09:45 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
Originally Posted by rstelma View Post
Hi Jeff,

Thanks for your answer. I don't know what they're 'supposed' to be called. In my little shop we call them intermediate tables. That's what I thought you meant but wasn't sure since you were suggesting for a table between the Contact and Address table. We are not usually interested in previous addresses. This can get really knit picky but I probably still wouldn't put a mapping table there. Since I would need a whole other row in the address table I'd probably just keep my Contact ID's in the address table and have an option somewhere in the application to look at previous addresses. That column would never be called 'ID' either. I hope Will is listening. ID is a reserved word in SQL and can lead to all kinds of trouble. Besides you won't know which ID you are dealing with after a while. I usually use a word from the table name like ContactID.

I do use mapping tables quite a bit though and I understand why you would use one there.

I just wanted to make sure you were talking about what I thought you might be talking about.... by another name.

Here are a couple of links, Will, to normalization tutorials. It may look daunting at first but the concepts are really not all that difficult and the amount of time you put into learning even the basic techniques will save you untold hours or frustration, anger and self mutilation.

http://www.phlonx.com/resources/nf3/#invoice_order

http://en.wikipedia.org/wiki/Database_normalization

Richard
Heh... first, I never suggested calling a column just "ID". I suggested calling columns CustomerID and AddressID.

Although I agree with you that I'd never call a column just "ID", it's not for the reasons you stated. Show me just one piece of code involving a column called "ID" that causes a failure.

Code:
Since I would need a whole other row in the address table I'd probably just keep my Contact ID's in the address table
No you wouldn't... that's the whole purpose of the mapping table. If a contact and a customer where the same, you'd only need one address. The way you suggest, you'd need two rows in the Address table or you'd need an extra denormalized column... one for the CustomerID and one for the ContactID with the understanding that ContactID would likely be null in such a case. That's a form of denormalization. And what do you do if you have more than one customer at the same address the way you want to do it? 2 Address rows... well, unless you go for some serious denormalization.
__________________
--Jeff Moden
 
Old May 26th, 2009, 09:58 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Will,

To someone new at it, Normalization seems like a pain and it seems unnecessary especially if you're in a hurry to design a database. Let's start you off with a very simple explanation... just lookup "Normalization" in Books Online 2k5. There're a couple of basic rules spelled out in plain English and, although it's not super comprehensive, they make a very good lead in to the Wikipedia article that Richard pointed out.
__________________
--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Big Desktop Database Application abhijitbk WinForms/Console Application Design 0 July 24th, 2008 07:54 AM
Loading Lots of Data bmains C# 2005 1 January 28th, 2008 12:57 PM
Lots of errors and no solutions.... i8226girl BOOK: Beginning Access VBA 3 September 20th, 2005 05:43 AM
How to display big sentences in tables/cells karib Dreamweaver (all versions) 2 November 1st, 2004 04:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.