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 127167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:26:01+00:00 2026-05-11T05:26:01+00:00

My database has a table that keeps track of the department and user id.

  • 0

My database has a table that keeps track of the department and user id. The catch here is that while the department and user_id column can have duplicates, the combination of them can’t. This is to say,

DepartmentA    0001 DepartmentA    0002 DepartmentB    0001 DepartmentB    0002 

are valid data in my table. But

DepartmentA   0001 DepartmentA   0001   

is not valid.

The combination of department and user_id forms a unique identifier of a record. And in another table, I need to use this unique identifier to track the users activities, like what’s the time they enter a building, what time they leave the building etc.

What I am thinking is that I create the following tables

CREATE TABLE user (  user_id INT( 4 ),  department VARCHAR( 25 ) NOT NULL ,  combined_id int(4) ,  UNIQUE ( combined_id ) ,  Primary key(user_id, department)  );    CREATE TABLE user_activity( combined_id int(4), activity varchar(25), Foreign Key (combined_id) references user(combined_id) ); 

So I am thinking about using a double primary key for my purpose. This, IMHO, is the best way to guarantee data integrity. But from what I know using double primary key can be quite hard to work with ORM, i.e., they are not fully supported and one has to write customized queries for it.

Is the above design the best, given my scenario?

  • 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-11T05:26:02+00:00Added an answer on May 11, 2026 at 5:26 am

    I would use surrogate primary keys, and put a unique constraint on the combination of those 2 fields that should remain unique.

    That is, I would not consider to be my employee-number as a primary key. This facilitates a lot of scenarios.

    So, this is what I would do

    CREATE TABLE Employee (   EmployeeId INT ,   EmployeeNumber VARCHAR(4),   DepartmentId INT,   EmployeeName )  CREATE TABLE Department (    DepartmentId INT,    DepartmentName ) 

    In the Employee table, EmployeeId is the Primary Key. This column just contains a number, which has no meaning in the problem-domain. It has just an administrative meaning in the database (uniquely identify an Employee).

    The EmployeeNumber contains the ‘business / domain number that is used to identify the employee). Note that I’ve made this field a VARCHAR field, since you LPAD that number with zeroes in your example. So in fact, it should not be a numeric field. 🙂

    Next to that, there’s also a DepartmentId column in the Employees table which is a foreign key to the Department table. In the Employee table, you should put a Unique constraint on the EmployeeNumber/DepartmentId fields. (So, one constraint on the combination of both columns).

    Then, the User_Activity table can just look like this:

    CREATE TABLE User_Activity (   EmployeeId INT,   ActivityId INT ) 

    (I know that the CREATE TABLE statements are not really actual correct SQL Statements, but this is just for illustration purposes offcourse).

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

Sidebar

Related Questions

I have an SQL Server 2008 database with a table that has a column
I have a database that keeps track of books. The books table looks like
I have a table that has a column called Ordinal used to keep track
I'm working on an Access database that has a main table that keeps track
I have a database that has a table email_eml that stores 3 attributes name_eml,
I have a database table that has a Unique Key constraint defined to avoid
I have a SQL database that has a table with a field set to
I have a ms access database that has one table for each photo album
I am designing a database for a project. I have a table that has
I have a SQL database that has the following table: Table: PhoneRecords -------------- ID(identity

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.