Forum Moderators: coopster & phranque

Message Too Old, No Replies

ID syntax

Is a prefix needed?

         

russgri

7:02 pm on Mar 30, 2003 (gmt 0)

10+ Year Member



In a table...

I've seen this used:
Puppies
PuppyId
Puppyname

What are the advantages/disadvantages of using:

Puppies
ID
name

Fischerlaender

10:21 pm on Mar 30, 2003 (gmt 0)

10+ Year Member



It's hard to answer your question with just no background of what kind of information is stored in there.

So, just in general:
If you assign every Puppyname its unique ID, you are able to refer to the correct Puppyname even if you had an typo in there.

Look as these books as an example, which are stored in table1:

ID Bookname    Author 
--------------------------------
1 Learning MySQL Adam
2 Adanced MySQL Paul
3 MySQL for Beginners Mike

Now in table2 we save which amount of these books is available in which bookshop:
BookID Bookshop Amount 
------------------------
1 Chicago 3
2 New York 5
2 Dallas 1
3 Chicago 2

This tells us that there are two copies of the book "MySQL for Beginners" in the bookshop in Chicago.

If we now find the typo in table1 where it should read "Advanced MySQL" we now have just to edit this entry. The connection to table2 is not affected as it would be if we did not use an ID.

russgri

11:06 pm on Mar 30, 2003 (gmt 0)

10+ Year Member



I used table Puppies ...not Books

Are you saying that using PuppiesId or Puppyname would be better or worse?
Because?

Fischerlaender

11:30 pm on Mar 30, 2003 (gmt 0)

10+ Year Member



There isn't a big difference between puppies and books - at least if you just think of a relational database ...

You should always use an ID as a "Primary Key"; this means that its only purpose is identify this entry.

ShawnR

12:39 am on Mar 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My take on it is that everyone has their own favourite naming convention. Naming conventions are important to make code readable for you or whoever else needs to maintain it, so in choosing your naming convention, bear in mind who needs to maintain it. If just you, then (a) don't re-invent the wheel and (b) be consistent in your application. If you are working in an IT company or IT department then find out if they have a mandated naming convention.

The reason people get so hung up about these sorts of things is because of how we learn (to read, play chess, or anything else): We recognise patterns. So if you use a convention which is widely accepted, you and others can 'speed-read' your code much faster.

My rules are:

  • Name tables in the plural (Puppies, not Puppy)
  • Name table fields in the singular (PuppyId, not PuppiesId).
  • Have the primary key named <tablename converted to singular>Id (e.g. PuppyId)
  • Don't include the table name in non-primary keys

There are many other things to consider, that people get very hung up on, such as underscores (first_name) vs capitalisation (firstName). If you are free to choose, then hunt around for a naming convention you like (The good thing about standards is that there are so many to choose from). You can search for phrases like like:

  • "Leszynski Naming Convention"
  • "database naming convention"

To answer your question directly, the disadvantage of using 'ID' instead of puppyId is that in addition to the table's own primary key, you often need to include a foreign key as one of the table's fields. For example, in the table of puppies, you may wish to have a field for 'ownerId', where you have a separate table of owners.

Shawn

russgri

12:50 am on Mar 31, 2003 (gmt 0)

10+ Year Member



Thanks Shawn
I appreciate the specific reply...something hard to get these days.

tedster

1:12 am on Mar 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should always use an ID as a "Primary Key"; this means that its only purpose is identify this entry.

In the early 90's I took on a retail database that had extra information embedded in the product ID fields - or what has been called an "intelligent key". In this case, every product ID began with a three digit number to indicate department and class.

What a nightmare! As the product mix and merchandizing shifted, the company often created a second ID for the identical product -- and that made the total sales history unavailable. Even more, buyers were often placing new orders using either ID, pretty much at random. This had gone on for about 3 years before I got to it, and it was a nasty mess to untangle.

It can be tempting in an small database to create intelligent keys - but definitely, don't do it.

andreasfriedrich

1:22 am on Mar 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




>>something hard to get these days

I believe that was totally uncalled for. Fischerlaender was trying to help and your question is not as clear as to make it totally obvious that you were only interested in naming conventions.

If you had asked about the pros and cons of including the table name as a prefix to the column names there would have been no ambiguity as to what you want to know.

Andreas

ShawnR

1:42 am on Mar 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was going to write "My guess is that the 'something hard to get...' comment was a reference to life in general, not to any posting on this board", but I see Andreas beat me to it... I think one of the great things about this online community is that everyone learns, even if you don't perhaps learn the things you expected to (or wanted to) learn.

A case in point is Tedster's post. My take on that is that embedding extra information, such as department, in a field, would contravene textbook database design principles (3rd Normal Form, 4th Normal Form etc). But isn't it weird that it is such common practice to have a long int as the primary key, with its only purpose being to identify the entry, and yet this is not mandated by 4th Normal Form, nor taught in university courses/textbooks (although I am probably a bit out of date with the current university database course curriculum)

Shawn

russgri

2:33 am on Mar 31, 2003 (gmt 0)

10+ Year Member



My apologies to the forum.

The learning process only points up my lack of knowledge about how to ask the question.

I should have used the subject naming conventions.

But Shawn nailed the thrust of my question...and my elation at finally, after a couple of months trying, getting the questioned answered triggered the uncalled for response.

I have read up about and posted on various newsletters, even using naming conventions in the post and it just points up the difficult of getting on the same wavelength.

So, I promise to help in any way...when and if I get the knowledge and understanding.

jatar_k

3:05 am on Mar 31, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



My apologies to the forum

no worries russgri, no harm, no foul

after a couple of months trying

too bad you didn't find us sooner ;)