Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 187861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:53:57+00:00 2026-05-11T15:53:57+00:00

I have two tables in my SQL database: Company: ID (autoincrement) name address …

  • 0

I have two tables in my SQL database:

Company:

  • ID (autoincrement)
  • name
  • address
  • …

Employees:

  • ID (autoincrement)
  • Company_id
  • internal_id
  • name
  • lastname

The problem is that I would like to have a employee id (internal_id) that is relative to the company they belong to. I got this dilema since I’m really been searching what would be the cleanest way to implement it.

One option would be to just make a kind of SELECT MAX(internal_id) FROM employees WHERE company_id = X, but the problem would be that if I happen to delete the last employee the next one would be created with the ID of the next.

Any ideas or suggestions?

PD: The reason of why I want to do this is that i dont want a user from company X create an employee that is for example ID=2000, while the last employee created in his company was, say, 1532. this would normally happen in a system in wich Company Y and Z also create employees on the same system. I want this ID not to use as a foreign_key, but to have it for internal (even documents or reports) use.

PD2: In this case the employees will never have to change companies

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:53:58+00:00Added an answer on May 11, 2026 at 3:53 pm

    There are lots of questions here about creating ‘Business’ IDs or numbers that are unrelated to the primary keys.

    In your case I would create a column on the Company table ‘NextEmployeeID’ Then when creating a new employee simply retrieve the value and increment it.

    Now I leave it up to you to figure out what happens if the employee changes companies. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my SQL database: mysql> select *from crop; +------+-----------+----------+ |
Let's say I have two SQL tables: QYZ: id, uid<user id, FK*>, somedatacolumn USERS:
I have four Database Tables like these: Book ID_Book |ID_Company|Description BookExtension ID_BookExtension | ID_Book|
I have two tables question and answers. - questions{id,user_id,question,date} - answers {id,q_id,user_id,answer, date} I
I have two tables with columns of my interests as Table1.Column1 and Table2.Column2 Table1
I have questions which are represented like this in my SQL database : CREATE
I'm just started using SQL and ran into a problem. In my database, I
I am having problems setting up an association in entity framework between two tables
I know this is a low-level question but, not being a database person, I
With EF CTP5 Code-First I am trying to map a class model which contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.