Jump to content

Auto Increment


nottoolate

Recommended Posts

Hello,

 

I am creating a job setup form and have a need for the job id field to increment based on the last inserted job id into our Oracle database.

So far I have created a function that selects the maximum job id from the job table and adds 1 to the number.

 

However, I run into the problem of when a user is filling out the form and another user opens it and begins filling out the form they both will receive the same job id in the field. I'm assuming this could possibly be solved with sessions, but I'm not sure where to start.

 

Also, the form is not inserted into the table on submit (instead an excel sheet is created with the information and someone will enter the data into the database manually). Since the form data can be entered anytime from completion to next week/two weeks, how can I ensure that that job id is not used again.

 

Thanks

Link to comment
Share on other sites

 

If the "job id" is the primary field with an auto increment you don't even have to do that, as it will auto increment itself:

 

$query_insert_new = mysql_query("INSERT INTO jobTable  (jobID, name, date)VALUES   (NULL,'$new_name',CURDATE())");

 

He said he's using Oracle, which doesn't support that.

Link to comment
Share on other sites

He said he's using Oracle, which doesn't support that.

 

Oracle supports sequences.  It's been a long time since I used it, but if these google results are accurate still it'd be something like:

 

first create the sequence

CREATE SEQUENCE jobAutoInc INCREMENT BY 1 START WITH 1;

 

Then, whenever you insert:

INSERT INTO jobs (jobId, Name, whateverElse) VALUES (jobAutoInc.NEXTVAL, 'blah', 'bleh');

 

If you need the id for later use in your code, you can first do a SELECT jobAutoInc.NEXTVAL FROM DUAL and grab the results, then use it in your insert and wherever else you need it.

 

Link to comment
Share on other sites

If you use ORACLE (or PostgreSQL), the syntax of INSERT could be different from MySQL's INSERT. Also, if id field is a sequence, don't worry about it! Just insert you data and return a new id.

 

INSERT INTO jobs (Name, whateverElse) VALUES ('blah', 'bleh') RETURNING jobID;

 

You get jobID as if you start SELECT query.

Link to comment
Share on other sites

scootstah - Well currently I'm using the Oracle database to test the job numbering system, but we are planning to change to a new system in how we number our jobs. So we will start from the beginning using 0000, I figure I could just create a new table in MySQL and work with that. I'm assuming it would be easier?

 

SergeiSS - I noticed that. Our Oracle database contains all of our companies info (employee, jobs, invoices, etc.). This is what we utilize for mostly everything. I am not allowed to use inserts to get form data into this database, so everything is usually manually entered.

The MySQL database is only used by our intranet for administration (report, page, and tool access) purposes. None of the information you will find in the oracle database would be found in the mysql database. I do have full rights with this database so I can create and insert any data I would like...

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.