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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:30:24+00:00 2026-05-24T22:30:24+00:00

Assuming I have a table like the one below: create table filetype_filestatus ( id

  • 0

Assuming I have a table like the one below:

create table filetype_filestatus (
  id             integer(11) not null auto_increment,
  file_type_id   integer(11) not null,
  file_status_id integer(11) not null,
)

I want to add a sequence column like so:

alter table filetype_filestatus add column sequence integer(11) not null;
alter table filetype_filestatus add unique key idx1 (file_type_id, file_status_id, sequence);

Now I want to add the column, which is straightforward, and populate it with some default values that satisfy the unique key.

The sequence column is to allow the user to arbitrarily order the display of file_status for a particular file_type. I’m not too concerned by the initial order since that can be revised in the application.

Ideally I would end up with something like:

FileType FileStatus Sequence
   1        1          1
   1        2          2
   1        3          3
   2        2          1
   2        2          2

The best I can think of is something like:

update filetype_filestatus set sequence = file_type_id * 1000 + file_status_id;

Are there better approaches?

  • 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-24T22:30:25+00:00Added an answer on May 24, 2026 at 10:30 pm

    Hmm, I believe this should work:

    UPDATE filetype_filestatus as a 
           SET sequence = (SELECT COALESCE(MAX(b.sequence), 0)  
                           FROM filetype_filestatus as b
                           WHERE b.file_type_id = a.file_type_id) + 1
    
    WHERE sequence = 0
    

    I’d recommend adding the new column to the table, running the alter table statement (and getting the default of 0), run the update statement, then add the constraint (well, you have to anyways). Anything that gets touched updates to a sequence greater than 0, so this can be safely run multiple times, too.


    EDIT:

    As @Dems has pointed out, the subquery is being run before the update, and so the above doesn’t actually work for this purpose. It does work on single-line inserts (which doesn’t help at all here).


    EDIT:

    Gah, you have an id column, this works just fine (and yes, I tested this one first).

    UPDATE filetype_filestatus as a
           SET sequence = (SELECT COALESCE(COUNT(*), 0)
                           FROM filetype_filestatus as b
                           WHERE b.file_type_id = a.file_type_id
                           AND b.id < a.id) + 1
    WHERE sequence = 0
    

    Don’t know about the performance implications, though.

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

Sidebar

Related Questions

Assuming I have a table like this one: CREATE TABLE user_delegates ( [id] INT
my question is: assuming we have a calendar_table with UNIX datestamp date_column, i want
Assuming I have a tree structure UL --LI ---INPUT (checkbox) And I want to
Assuming I have a column called A and I want to check if A
I have a database I'd like to create an entity from, and then generate
I have a table like : trans is the name of the table for
I have one bean like: public class Car{ String color; List<Wheel> wheels; .... }
I have a table (several actually) that I want to get results from with
I have a table like so: categoryID categoryName ---------------------------- 1 A 2 B 3
Assuming the following mappings are provided: <class name=A table=a_table> <id name=id/> <many-to-one name=entityB column=fk_B

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.