Jump to content


Photo

Need Help to generate primary keys!


  • Please log in to reply
4 replies to this topic

#1 elioncho

elioncho
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 02 September 2006 - 11:02 PM

Hello. I want to know is there is a way of generating primary keys automtically when a new register of information is added to a table. I am currently doing a proyect where all the information is added with a secuential number. For example...
Secuential Number(which will be the primary key), Firstname, Lastname...
1                                                                  John        Connor
2                                                                  Oscar      Wilde
.
.
So the idea is that the user fills up the first and last names and then the info is saved on the database with a secuential number. How can I do this automatically.

There is also another table where information will be added with a primary key that should consist of a secuential number and the year. For example:

1-06
2-06
3-06
.
.
.
The thing is that when the year is over it should begin adding new information to the database from number one again but changing the year. Like this

1-07
2-07
3-07
.
.
.

Any ideas please of how to do this. Thank you very much,

Elías

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 September 2006 - 11:09 PM

auto-increment on the first table (the one with names in it)

And use autoincrement on the second table as well but DON'T mess around with 1-07, 1-06 etc, just put that into a non primary key field and add a reference_id that points to the names file ... depending on just what you were planning to do with 1-06, 2-08 etc.
Legend has it that reading the manual never killed anyone.
My site

#3 elioncho

elioncho
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 02 September 2006 - 11:14 PM

Thanks for the help. The thing is that in the second table which is the number accompanied by the year:
1-06
2-06
3-06
.
.
.
When 2006 is through it should begin by 1 again:
1-07
2-07
3-07
Do you have a link where I can read more about reference_ids. Thank you very much,

Elías

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 September 2006 - 11:47 PM

A reference_id is just a piece of data in a database table that provides the record ID of more information about the same person, same thing, etc. in another table.  Simple example: if I have your name in table1 I don't need your name is tables 2, 3, 4 etc. I can just say 'if you want the name, look at record ID whatever in table whatever.  That's what a relational database is about - relations.

I'm not sure what you intend doing with 1-06, 2-08 etc but typically :"it's a bad idea" to keep two pieces of unrelated information in the same data cell.  I suspect that what's really important is the year part of that and that the prefixed number is for ... some other purpose that I can't imagine or that can be done as well or better in another way.
Legend has it that reading the manual never killed anyone.
My site

#5 elioncho

elioncho
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 04 September 2006 - 08:25 PM

Ok Thanks for the help. I will use a Date and a number as primary keys. I there is a way that after a year has passed the number column begins again at 1...?

Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users