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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:55:08+00:00 2026-06-16T14:55:08+00:00

Is it possible to auto-increment a non-primary key? Table "book_comments" book_id medium_int timestamp medium_int

  • 0

Is it possible to auto-increment a non-primary key?

Table "book_comments"

book_id     medium_int
timestamp   medium_int
user_id     medium_int
vote_up     small_int
vote_down   small_int
comment     text
comment_id  medium_int

Primary key -> (book_id, timestamp, user_id)

There will be no other indexes on this table. However, I would like to make the comment_id column autoincrement so that I can easily create another table:

Table "book_comments_votes"

comment_id  (medium_int)
user_id     (medium_int)

Primary key -> (comment_id, user_id)

Users would be able to vote only once per book comment. This table enforces this rule by the primary key.

Question:

Is it possible to auto-increment a non-primary key – as in, auto-increment the comment_id column in table "book_comments"?


Alternatives, Discussion.

I would like to do this for simplicity as explained above. The alternatives are not promising.

  • Make the commnet_id PK and enforce integrity through a unique index on book_id, timestamp, user_id. In this case, I would create an additional index.
  • Keep the PK and replace the comment_id in the book_comments_votes with the entire PK. This would more than triple the size of the table.

Suggestions? Thoughts?

  • 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-06-16T14:55:10+00:00Added an answer on June 16, 2026 at 2:55 pm

    Yes you can. You just need to make that column be an index.

    CREATE TABLE `test` (
      `testID` int(11) NOT NULL,
      `string` varchar(45) DEFAULT NULL,
      `testInc` int(11) NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`testID`),
      KEY `testInc` (`testInc`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
    
    
    insert into test(
      testID,
     string
    )
    values (
    1,
        'Hello'
    );
    
    
    insert into test( 
    testID,
     string
    )
    values (
    2,
        'world'
    );
    

    Will insert rows with auto-incrementing values for ‘testInc’. However this is a really dumb thing to do.

    You already said the right way to do it:

    “Make the comment_id PK and enforce integrity through a unique index on book_id, timestamp, user_id.”

    That’s exactly the way that you should be doing it. Not only does it provide you with a proper primary key key for the table which you will need for future queries, it also satisfies the principle of least astonishment.

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

Sidebar

Related Questions

Is It Possible to have a primary key and auto increment key associated to
I have a primary key auto increment attribute in my table. I want to
I have a Customer table with a Primary key (int auto increment) and an
I have an table which use an auto-increment field (ID) as primary key. The
Here is my table structure: table . id (auto increment; primary key) table .
I have a table with primary key auto increment ID column. In some cases,
Possible Duplicate: MySQL: Reorder/Reset auto increment primary key? I have an auto increment field
Possible Duplicate: Auto-increment primary key in SQL tables i have a question about how
I have a table 'posts' in database which has non-unique index on user_id (Key:
In our database we have tables which have no single auto-increment primary key but

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.