Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

customise primary Key

 
   Database Help (Home) -> General Discussions RSS
Next:  Access 2007 & 2002 (XP)  
Author Message
always gull

External


Since: Dec 22, 2008
Posts: 1



(Msg. 1) Posted: Mon Dec 22, 2008 4:27 pm
Post subject: customise primary Key
Archived from groups: comp>databases>ms-sqlserver (more info?)

Hi Guys
I have a problem. I am doing a project for my study in which in am
creating booking system for repair. and i have tables customer and
repair. for both tables i need to create a customize key for customer
table i need to create a primary key which has C at the biggning then
year and finally a unique intger for example

C0801,in which 08 is the year and 01 is the unique integer for each
customer and some thing same for reapair table. and i am using SQL
server 2005 is it possible to create these kind of keys. if yes can any
one help me please

*** Sent via Developersdex http://www.developersdex.com ***

 >> Stay informed about: customise primary Key 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 2) Posted: Mon Dec 22, 2008 6:25 pm
Post subject: Re: customise primary Key [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

always gull ( ) writes:
> I have a problem. I am doing a project for my study in which in am
> creating booking system for repair. and i have tables customer and
> repair. for both tables i need to create a customize key for customer
> table i need to create a primary key which has C at the biggning then
> year and finally a unique intger for example
>
> C0801,in which 08 is the year and 01 is the unique integer for each
> customer and some thing same for reapair table. and i am using SQL
> server 2005 is it possible to create these kind of keys. if yes can any
> one help me please

This strikes me as somewhat funny. If I register as a customer this
year, I get customer ID C081002. Then if I return next year, I get an
ID of C090012?

I think the best would to be make this column a computed and peristed column
and store the parts in different columns. (If the letter is constant it
could of course be part of the formula?)

What do you mean by "study"? We don't really like helping with class
assignments here.


--
Erland Sommarskog, SQL Server MVP, esquel.RemoveThis@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 >> Stay informed about: customise primary Key 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 3) Posted: Fri Dec 26, 2008 1:00 pm
Post subject: Re: customise primary Key [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> I have a problem. I am doing a project for my study in which in am creating booking system for repair. <<

Are we doing your homework in violation of basic academic ethics? So
far, I take great pride in knowing I had two students kicked out of
universities for cheating.

Have you researched industry standard identifiers? DUNS? tax ids?
Does the company already have a customer id?
 >> Stay informed about: customise primary Key 
Back to top
Login to vote
bill

External


Since: Oct 17, 2008
Posts: 17



(Msg. 4) Posted: Sun Dec 28, 2008 2:32 am
Post subject: Re: customise primary Key [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

agree that cheating is a major problem, but merely asking questions
on a usenet group hardly qualifies as cheating. In fact, most
universities unfortunately do a bad job of teaching database
concepts. I think the OP is doing a good thing by asking outside
sources for clarification of concepts. Asking someone else to do the
work for you is cheating, but that isn't what the OP is asking. As
far as I can see, the OP is just asking if it is possible to have the
database engine create keys in the format 'C'<last two digits of
year><local counter>. Where <local counter> is a number that is unique
within a year but resets each year, and is thus not unique across
years.

To the OP: The answer to your question is 'yes', but it I recommend
against it. Keys of this type (called 'intelligent' or 'hierarchical'
keys) have lots of problems. I'll give you some tips on where you
can look to get the tools needed for building the key the way you
want, then talk a little about better alternatives.

1 How to build the ugly (intelligent) key:
First, I don't think you need to build a stored procedure, you can do
it all with a SELECT statement and built in SQL Server functions. Do a
google search for "string concatenation" in SQL Server. Your key has
three pieces, and there is a simple operator in SQL Server that allows
you to concatenate the separate pieces and make a single string like
you want.

You still need to generate the different pieces of your key so that
you have something to concatenate. The first part (lettter C) appears
to be a literal. In just about every flavor of SQL, you can just put
a literal in single quotes and select it, like this: SELECT 'C
FROM . . .';

For the next piece, you have to get the system date (I assume), then
extract the year portion, and finally get to the last two digits of
the year. google sql server getdate() function to see about system
date. The DATEPART() function will help you get different parts of
the date. So say you get the integer 2008, you want to get to '08'.
There may be better ways, but one idea is to convert the integer to a
string (look up CAST or CONVERT) and then grab the last two characters
(look up SUBSTRING). You could also do modulus arithmetic on the full
year (lookup SQL SERVER MODULO OPERATOR), but the modulo operator
returns an integer. You would have to convert this to a string anyway
and conditionally pad it with a leading zero, depending on the size of
the number. That's easy with LPAD in Oracle, but a pain in the butt
(as far as I know) with SQL Server.

For the final piece, you should lookup the ROWNUMBER() function, which
is part of the window functions in sql server. In this case the word
'window' has nothing to do with the Microsoft O/S. 'Window' refers to
a particular partition of your data (nothingg to do with physically
partitioned tables) over which you want to perform an operation.
Works great for subtotals, averages on partitions, etc. You can use
ROWNUMBER() to create a number that would be unique with respect to a
partition, for instance unique within a year, etc.


2: Why you should take another approach:
Intelligent keys always lead to big trouble sooner or later, usually
sooner. By "intelligent" I mean a key that has any embedded
meaning.

Here's a great example of an intelligent key causing big problems: I
once did some work for a retail chain that had grown by acquisition
over the years. In cases like this, the original legal entity of each
store is important for regulatory and tax reporting purposes. So
let's say the chain had a total of 1,000 stores. Maybe 500 of the
stores belonged to entity 'X', 200 of the stores belonged to entity
'Y' and 300 of the stores belonged to entity 'Z'. You have to keep
track of this, because each legal entity pays taxes bases on its
revenues, etc. Someone had the horrible idea of relating the store
number to the legal entity. Thus stores with numbers in the range 1 -
500 were in legal entity X, numbers in the range 501 - 700 were in
legal entity 'Y' etc. As the chain grew, they found that they had
trouble assigning new store numbers. Example: The chain has one
thousand stores and acquires another one. Here is what would happen

Person 1: What should be the store number for the store we just
acquired? How about 1001?
Person 2: No, wait! Numbers in the range 1000 to 2500 are reserved
for stores that belong to entity 'AA', and this one came from entity
'Y'.
Person 1: Oh, okay, well then what is an available number for entity
'Y'.
Person 2: Oops! We're out of numbers in the range for entity 'Y'.
Person 1: Darn! What do I do now?
Person 2: It turns out we closed a store a year ago that originally
came from entity 'Y'. We can re-use that old store number!

They really did this, and it was a freakin' mess. It got worse: These
stores had physical characteristics that had to be tracked for
regulatory purposes for a very long time, sometimes for years after
the stores closed. However, some numbers had been re-used two and
even three times. Tracking the stores was a horrible mess.

This whole problem could have been avoided by just using an arbitrary
unique number with no hidden meaning to identify each store. The
legal entity info should have been stored in an "OWNING_LEGAL_ENTITY"
column in the store table.

Your customer data is an analogous issue. Make your customer number a
simple integer and put the date that the customer was created in its
own column. This info could be very useful for other purposes. The
database owner could see if there are any correlations between
customer age and customer sales, see if customers tend to leave after
a certain age, etc. Just make your customer key a simple number, and
put the other meaningful pieces in their own columns. With the
customer CREATE_DT in it's own column, it's extremely simple to use
the built in data arithmetic functions of SQL Server to calculate the
age of the customer in days, months, years or whatever, correlate
sales to age, etc.. Even if you had the entire date embedded in the
key, it would be a pain (and perform poorly) to extract the date from
the key and use it for queries.

As far as the 'C', I would not include that in the key. It's not
uncommon for business to have suppliers who are also customers, or who
used to be customers, or who may become customers in the future.
Let's say customer (Call it company 'XYZ') C0804 starts selling stuff
to the company. Will you make a vendor id for him (maybe V0912)? If
you do business with someone who is both a supplier and a customer,
its natural to want to know something akin to the "balance of trade"
between you and the other party. But now, the same company XYZ has
two different identifiers. How do you know that they they both point
to the same entity? You could make a cross reference table, but that
gets ugly. Why not just give the same type of key to anyone with whom
you do business (call it a "Party" or maybe a "Business Partner")?
Then it's easy to uniquely identify the Party. Maybe the business in
this scenario would say "our customers and suppliers never overlap".
That's a big statement, but for the sake of argument, let's assume
it's true. Even in this case, it's very possible that other parties
overlap. Most business set up their employees as vendors when they
reimburse them for business expenses. If you're stuck with a typical
accounting system, you have to cross reference the employee and vendor
to the same party in your own external database, but if you are
designing the system form the ground up, you can set up the system to
just have parties playing roles. Setting up a party system can get
pretty detailed, but I'd say for sure that including anything in the
key that specifies the role of the party in question is not a good
idea.

For repairs, consider something similar. Make the repair primary key
a simple integer. That integer could also printed on the repair
ticket. Don't try to encode the date or customer number into the
repair ticket number. Those pieces of info belong in their own
columns.

Thanks,

Bill
 >> Stay informed about: customise primary Key 
Back to top
Login to vote
DA Morgan

External


Since: Dec 14, 2007
Posts: 757



(Msg. 5) Posted: Sun Dec 28, 2008 12:56 pm
Post subject: Re: customise primary Key [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

bill wrote:
> agree that cheating is a major problem, but merely asking questions
> on a usenet group hardly qualifies as cheating. In fact, most
> universities unfortunately do a bad job of teaching database
> concepts.

Agree cheating is a problem.

Merely asking questions on a usenet group MAY BE cheating. And if you
were one of my students at the University of Washington you'd have
received unambiguous guidelines the first day of class.

Asking for a pointer ... where are the docs, what function should I use
is not cheating. Asking someone a question that leads to them responding
with the solution you turn in will get you dismissed, at least from my
class, without any chance of appeal.

Are "most universities doing a bad job of teaching database concepts" as
you suggest? I don't think you are qualified to make that statement as I
know I am not and suspect I have substantially more expertise in that
area than you.

What I can say though is that most universities consider their IT
curriculum to be theoretical ... in other words the concepts and
algorithms for working with linked lists ... rather than practical ...
as in how you should, or should not, design a Star Schema.
--
Daniel A. Morgan
Lecturer
University of Washington
damorgan RemoveThis @x.washington.edu (replace x with u to respond)
 >> Stay informed about: customise primary Key 
Back to top
Login to vote
bill

External


Since: Oct 17, 2008
Posts: 17



(Msg. 6) Posted: Sun Dec 28, 2008 4:43 pm
Post subject: Re: customise primary Key [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

To the OP: I am assuming that you have been given a file of customers
to load, and this can serve as grist for your SELECT statements. If
this isn't the case, and the user will be entering customers from
scratch via the U.I. then you should look into using triggers to
populate your columns. In either case, you'll be a lot better off to
avoid the intelligent key.

To Dan: I'm glad that you give unambigous guides to students,
everyone should be doing that, and I assume most do.

RE: "Most universities. . . bad job . . . database . . . " In
years past, I have taught commercial data modeling courses to many
students. At least 100 of those students had some form of computer
science (e.g. C.S. or Computer Info Systems) degree from schools all
around the country. These people were plenty smart, but the vast
majority of them had a horrible grasp of databases. It wasn't a
question of being "too theoretical", they had no grasp of the theory.
I talked to a many of them after hours and came to the conclusion that
the instruction they received in database theory was deficient. Many
of these people were unfamiliar with basic normalization rules,
domains, didn't understand joins clearly, etc. Like I say, these
people were smart, and quick learners. We weren't trying to design
star schemas or flatten schemas or anything like that. Once someone
explained the basics to them, they understood.

I will grant you that 100 people in data modeling classes does not
make a scientific sample (the people who were good at modeling may
have self-selected out of the class, etc), but it indicates
something. I have also noticed that many co-workers have a weak grasp
of databases as well. Combine my first hand experience with the near
universal deficient state of database design in commercial products.
Most of the architects of these products have some type of Comp Sci or
CIS degree. I agree that many schools tend to emphasize the science
of processing, I/O, O/S's, low level operations, and the like over
databases. However, that's not a distinction between the theoritical
and practical, it's a difference in area of emphasis.

Question: If I am not qualified to pass judgement on the job the
schools are doing, and you are not qualified (it sounds like you are a
prof and intimately familiar with the culture of schools), who is
qualified? It ought not to be the schools themselves, because nearly
every organizaiton has problems making honest introspective
assessments.

I don't have any axe to grind against schools. . In the interest of
full disclosure, my degree was in Finance, not Comp Sci or a related
field. On the one hand, this means I don't have the benefit of
experience regarding the inside workings of Comp Sci and CIS
departments. On the other hand, it also means that I am basing my
statements on observation of realities in the field.

Schools on the whole do a great job, and I believe in and support
higher education. However, schools must take some responsibility for
the woeful data of db knowledge in the business world.

Thanks,

Bill

On Dec 28, 1:56 pm, DA Morgan wrote:
> bill wrote:
> >  agree that cheating is a major problem, but merely asking questions
> > on a usenet group hardly qualifies as cheating.   In fact, most
> > universities unfortunately do a bad job of teaching database
> > concepts.  
>
> Agree cheating is a problem.
>
> Merely asking questions on a usenet group MAY BE cheating. And if you
> were one of my students at the University of Washington you'd have
> received unambiguous guidelines the first day of class.
>
> Asking for a pointer ... where are the docs, what function should I use
> is not cheating. Asking someone a question that leads to them responding
> with the solution you turn in will get you dismissed, at least from my
> class, without any chance of appeal.
>
> Are "most universities doing a bad job of teaching database concepts" as
> you suggest? I don't think you are qualified to make that statement as I
> know I am not and suspect I have substantially more expertise in that
> area than you.
>
> What I can say though is that most universities consider their IT
> curriculum to be theoretical ... in other words the concepts and
> algorithms for working with linked lists ... rather than practical ...
> as in how you should, or should not, design a Star Schema.
> --
> Daniel A. Morgan
> Lecturer
> University of Washington
> damor....DeleteThis@x.washington.edu (replace x with u to respond)
 >> Stay informed about: customise primary Key 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How to list tables with Primary keys - Hello, We imported a bunch of tables from a database and realized that the primary keys weren't copied to the destination db. In order to re- create the keys, we need to know which tables have them. Is there a command that I can use (on the source db) t...

Primary Key fails to copy in db Export - Currently, I'm exporting a database from production to local (test) machine. I've done this several times without problem, but during the last few days, the primary keys have failed to export. Would anyone know what options might keep the keys from..

Can't Auto-Start Agent - I have a SQL Server 2000 that failed to start after a Windows reboot. Agent properties are checked to auto start if stopped. Under the Agent Error Log, I have the following data: Information: SQL Agent stopping because of Windows shutdown. SQL Agent..

Seek method, table-direct, and sql server2005 - From what I've read in the docs, ado.net currently supports opening sql server ce tables in table-direct mode and performing Seek operations on them (using SqlCeDataReader), but not on the full-blown sql server. Is this (will this be) still true with...

pass hash (#) table with different structure to stored pro.. - Dear Techies, I making one stored procedure, which does some operation based on an interface hash (#) table ---- name #mydata. This stored has two section of code (seperated by parameter value 0 and 1) But hash table #mydata (same name) number/name of....
   Database Help (Home) -> General Discussions All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]