elioncho Posted September 2, 2006 Share Posted September 2, 2006 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 Connor2 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-062-063-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 this1-072-073-07...Any ideas please of how to do this. Thank you very much,Elías Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 2, 2006 Share Posted September 2, 2006 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. Quote Link to comment Share on other sites More sharing options...
elioncho Posted September 2, 2006 Author Share Posted September 2, 2006 Thanks for the help. The thing is that in the second table which is the number accompanied by the year: 1-062-063-06...When 2006 is through it should begin by 1 again:1-072-073-07Do you have a link where I can read more about reference_ids. Thank you very much,Elías Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 2, 2006 Share Posted September 2, 2006 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. Quote Link to comment Share on other sites More sharing options...
elioncho Posted September 4, 2006 Author Share Posted September 4, 2006 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.