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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:33:50+00:00 2026-05-19T23:33:50+00:00

I have a user table with userid and username columns, and both are unique.

  • 0

I have a user table with userid and username columns, and both are unique.

Between userid and username, which would be better to use as a foreign key and why?
My Boss wants to use string, is that ok?

  • 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. Editorial Team
    Editorial Team
    2026-05-19T23:33:51+00:00Added an answer on May 19, 2026 at 11:33 pm

    Is string or int preferred for foreign keys?

    It depends

    There are many existing discussions on the trade-offs between Natural and Surrogate Keys – you will need to decide on what works for you, and what the ‘standard’ is within your organisation.

    In the OP’s case, there is both a surrogate key (int userId) and a natural key (char or varchar username). Either column can be used as a Primary key for the table, and either way, you will still be able to enforce uniqueness of the other key.

    Here are some considerations when choosing one way or the other:

    The case for using Surrogate Keys (e.g. UserId INT AUTO_INCREMENT)

    If you use a surrogate, (e.g. UserId INT AUTO_INCREMENT) as the Primary Key, then all tables referencing table MyUsers should then use UserId as the Foreign Key.

    You can still however enforce uniqueness of the username column through use of an additional unique index, e.g.:

    CREATE TABLE `MyUsers` (
      `userId` int NOT NULL AUTO_INCREMENT,
      `username` varchar(100) NOT NULL,
      ... other columns
      PRIMARY KEY(`userId`),
      UNIQUE KEY UQ_UserName (`username`)
    

    As per @Dagon, using a narrow primary key (like an int) has performance and storage benefits over using a wider (and variable length) value like varchar. This benefit also impacts further tables which reference MyUsers, as the foreign key to userid will be narrower (fewer bytes to fetch).

    Another benefit of the surrogate integer key is that the username can be changed easily without affecting tables referencing MyUsers.
    If the username was used as a natural key, and other tables are coupled to MyUsers via username, it makes it very inconvenient to change a username (since the Foreign Key relationship would otherwise be violated). If updating usernames was required on tables using username as the foreign key, a technique like ON UPDATE CASCADE is needed to retain data integrity.

    The case for using Natural Keys (i.e. username)

    One downside of using Surrogate Keys is that other tables which reference MyUsers via a surrogate key will need to be JOINed back to the MyUsers table if the Username column is required. One of the potential benefits of Natural keys is that if a query requires only the Username column from a table referencing MyUsers, that it need not join back to MyUsers to retrieve the user name, which will save some I/O overhead.

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

Sidebar

Related Questions

I have a table User which has an identity column UserID , now what
We have a user table, every user has an unique email and username. We
I have a postgres database with a user table (userid, firstname, lastname) and a
I have a table UserAliases ( UserId, Alias ) with multiple aliases per user.
When designing user table what would be the must have fields from the security/user
Suppose that I have 2 tables: [User] - UserID - Username [Task] - TaskID
I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1)
I have a user table with userid (pk), password, usertype . I have another
Im modelling a database in MSSQL 2008. I have 4 tables. **User** userID userName
I have a user table in my mysql database that has a password column.

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.