Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-24-2002, 12:57 AM   PM User | #1
Recoil UK
New to the CF scene

 
Join Date: Oct 2002
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Recoil UK is an unknown quantity at this point
Database schema question

Hi guys

Just starting out on the database design process, and I thought I would post on here so you can verify my thinking.

I,ve been designing a customer database, with the schema below file attached.

I,ll post a second message with the question, as it requires 2 files.

L8r
Attached Thumbnails
Click image for larger version

Name:	database1.gif
Views:	328
Size:	6.9 KB
ID:	369  
Recoil UK is offline   Reply With Quote
Old 10-24-2002, 01:03 AM   PM User | #2
Recoil UK
New to the CF scene

 
Join Date: Oct 2002
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Recoil UK is an unknown quantity at this point
Here,s the second file.

My question is this....

Because not all customers will require an address, and those that do can only have one, i've change the pk in addresses to customer_id and changed the table name to customer addresses.

But I want to keep my fk filed in the address_lines table as address_id, am I correct in thinking I can do this so long as my address_id field in customer_addresses remains an auto increment field?

Also can anyone spot any flaws in the overall design?

Any thoughts, tips, suggestions would be most welcome

Thx guys
Attached Thumbnails
Click image for larger version

Name:	database2.gif
Views:	293
Size:	7.1 KB
ID:	370  
Recoil UK is offline   Reply With Quote
Old 10-24-2002, 02:35 AM   PM User | #3
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,233
Thanks: 4
Thanked 81 Times in 80 Posts
Spookster will become famous soon enough
I would get rid of the customer types table and just add type to the customer table since you only have one attribute for it. Normalization is good to reduce data redundancy however but on the other hand too much normalization can also reduce overall system performance.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 10-24-2002, 03:02 AM   PM User | #4
Recoil UK
New to the CF scene

 
Join Date: Oct 2002
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Recoil UK is an unknown quantity at this point
Hi

Thx for the reply.

I think I will do that, as it will only have 3 possible values and what goes in that field is dependant on which webpage the account is created from, and can be controlled by PHP.

My second question regards mySQL Primary Keys and is this...

Do primary keys have to auto-increment? is it not possible to insert a primary key value from a primary key from another table.
I know you would not normally do this, as it describes a one-one relationship, but as you can see some customers dont have an address at all.

Or is the address_id required?

Thx again
Attached Thumbnails
Click image for larger version

Name:	database3.gif
Views:	241
Size:	8.1 KB
ID:	372  
Recoil UK is offline   Reply With Quote
Old 10-24-2002, 03:33 AM   PM User | #5
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,233
Thanks: 4
Thanked 81 Times in 80 Posts
Spookster will become famous soon enough
The Primary key does not need to be an auto-increment field but it must contain unique values which is why auto-increment is often used. If you are not going to use auto-increment then using some kind of number that will be unique for a particular person such as the persons ID number, drivers license number, etc.


If a person does not have an address then there is no point in creating a record for them in the address table.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 10-24-2002, 03:47 AM   PM User | #6
Recoil UK
New to the CF scene

 
Join Date: Oct 2002
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Recoil UK is an unknown quantity at this point
Hi again

Yes your right, maybe I didnt explain.

In the 3rd schema for the addresses table, i,m using customer_id as the primary key, which is in turn the primary key from the customers table (a not null, auto increment value).

Therefore if a customer doesnt require an address then no entries will be input into the address tabel.

I just needed to verify that this was possible.

Thx very much.
Recoil UK is offline   Reply With Quote
Old 10-27-2002, 04:05 AM   PM User | #7
kestrel7
New Coder

 
Join Date: Aug 2002
Location: Cape Town, South Africa
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
kestrel7 is an unknown quantity at this point
Hi

This type of situation arises often in relational database design. What you are describing is called a foreign key. Foreign - because it is a primary key of one table that you include as an attribute in a second table.

Most database systems allow you to specify foreign keys when you create the tables (much the same as you would declare a primary key for a particular table). The advantage of doing this, is that the system will ensure that any key inserted as a foreign key is in fact a valid attribute value (i.e it does appear as a primary key in the table that you are referencing). In other words, it will not allow you to put a customer_id, (say 007), as a foreign key if there is no such customer_id (007) appearing as a primary key.

I am not sure of the sql to declare foreign keys in mysql (it should be in the manual) but it should not be too dissimiliar to the way you delcare primary keys ( try something like... foreign key(customer_id) referencing [name_of_table_that_contains_customer_id_as_a_primary_key] or something similiar)

Hope this helps
kestrel7 is offline   Reply With Quote
Old 10-31-2002, 12:00 AM   PM User | #8
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,233
Thanks: 4
Thanked 81 Times in 80 Posts
Spookster will become famous soon enough
mysql does not support foreign keys. Although mysql is considered a relational database system it is not a true RDBMS though. To ensure data integrity all cascading of data insertion or deletion or updating must be hardcoded with whatever server side language you are using.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 10-31-2002, 05:07 AM   PM User | #9
kestrel7
New Coder

 
Join Date: Aug 2002
Location: Cape Town, South Africa
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
kestrel7 is an unknown quantity at this point
Hi

ooops ... sorry about that! Thanks for clarifying Spookster. I learn something new everyday.... that'll teach me to reply to something I haven't actually tried myself!

cheers
kestrel7 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:11 PM.


Advertisement
Log in to turn off these ads.