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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:53:11+00:00 2026-06-10T22:53:11+00:00

There are 4 columns in my relationship table: ID CID – course id LID

  • 0

There are 4 columns in my relationship table:

  1. ID
  2. CID – course id
  3. LID – related lesson unique id
  4. course_based_id – explanation below

There are bunch of lessons and courses with unique id’s in another tables called lessons and courses. This table is, asociation table between them. Basically it assigns unique lesson id to unique course id. But.. There is need for course based id of lesson. I mean, for example lets take a look at first and second rows from screenshot

enter image description here

Lid 1 is course 2’s first lesson, lid 3 is course 2’s second lesson… And so on. So if I insert cid 2, lid 128 it must third lesson of course 2.

Now, the problem is, I want to auto generate this number while insert:

$q="INSERT INTO `courses-lessons` (`cid`, `lid`) VALUES (?,?)";

but have no idea how to do it. Some MySQL function? or something inside query?

  • 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-10T22:53:13+00:00Added an answer on June 10, 2026 at 10:53 pm

    If you’re using MyISAM engine to store this table, this can be done relatively easy: you just define the fields this way:

    cid INT NOT NULL,
    course_based_id INT NOT NULL AUTO_INCREMENT,
    ...
    PRIMARY KEY (cid, course_based_id)
    

    Then any INSERT will automatically assign a new (+1) value for course_based_id based on cid. Here’s a Fiddle illustrating this concept.

    You can find more info about it in MySQL documentation.

    Unfortunately, there’s no such mechanism in InnoDB engine. So perhaps you can get away with the following routine (it’s a pseudocode, there are some nuances):

    1) lock table for writing; 
    2) @x = SELECT MAX(course_based_id) WHERE cid = %cid_to_be_inserted%;
    3) INSERT (..., course_based_id) VALUES (..., @x + 1);
    4) unlock table;
    

    I’d suggest checking this answer; it’s for PostgreSQL, not MySQL, but describes one possible implementation in some details.

    UPDATE: Here’s an example of actual set of queries implementing that behaviour:

    CREATE TABLE tx
      (id INT NOT NULL, cid INT NOT NULL, lid INT NOT NULL,
       course_based_id INT NOT NULL,
       PRIMARY KEY (id),
       UNIQUE KEY (cid, course_based_id)
    );
    
    LOCK TABLE tx WRITE, tx AS t READ;
    
    INSERT INTO tx (id, cid, lid, course_based_id)
      SELECT 2, 2, 1, 
             1 + COALESCE(
                   (SELECT MAX(course_based_id) 
                      FROM tx AS t 
                     WHERE cid = 2), 
                 0);
    
     INSERT INTO tx (id, cid, lid, course_based_id)
       SELECT 3, 2, 3, 
              1 + COALESCE(
                    (SELECT MAX(course_based_id) 
                      FROM tx AS t WHERE cid = 2),
                  0);
    
     UNLOCK TABLES;
    

    As you see, there’s no direct calculation of the next course_based_id value, yet we got the sequence here.

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

Sidebar

Related Questions

I have a table in which there are two columns : 1. import type,
I've got a table in which there are some columns with big text data.
i have a t_class table in mySql, in this table there are 3 columns,
How should columns in a database table that have a PK/FK relationship be named?
Suppose I have this database table (some sample code below) that stores the relationship
Suppose there are two tables. Table X-- Columns: id x_value Table Y-- Columns: id
I have 2 tables: Table A is a category table. Columns are cid, catname
I have a Page table and a View table. There is a many-many relationship
I have a table, two columns, user and group . It's a many-to-many relationship,
I have a grid in my GWT application there are columns and rows which

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.