Jump to content

Need Help to generate primary keys!


elioncho

Recommended Posts

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.