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

  • Home
  • SEARCH
  • 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 8666787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:48:59+00:00 2026-06-12T17:48:59+00:00

I have 2 tables – ACCOUNTS and USERS : ACCOUNTS: CREATE TABLE `ACCOUNTS` (

  • 0

I have 2 tables – ACCOUNTS and USERS:

ACCOUNTS:

CREATE TABLE  `ACCOUNTS` (
  `pk` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` varchar(9) NOT NULL,
  `primary` varchar(1) NOT NULL DEFAULT 'N',
  `edit_on` bigint(10) unsigned DEFAULT NULL,
  `status` varchar(1) NOT NULL DEFAULT 'A',
  PRIMARY KEY (`pk`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=1234567890 DEFAULT CHARSET=latin1

USERS:

CREATE TABLE  `USERS` (
  `user_id` bigint(9) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1

if edit_on contains a non-null value, it means the entry is an edit on an existing entry, whose pk is contained in edit_on.

Here’s what I want to do:

  1. For every user_id in ACCOUNTS, there should be a matching user_id in USERS.
  2. If edit_on is not null, there should exist an entry in ACCOUNTS with it’s pk equal to this (input) edit_on and user_id equal to this (input) user_id. And for that (old) entry, status should be set to E.
  3. If primary = 'Y', then for other rows in ACCOUNTS with the particular user_id, primary should be set to 'N'

After experimenting a hell lot with different queries (using ON DUPLICATE UPDATE etc.), I still cant find a solution. Please help, as I’ve been working on this for 2 full days. Thanks in advance.

EDIT

Here’s something along the lines of what i’ve tried so far:

insert into ACCOUNTS
case
    when edit_on is null
        then
            select null, '222222222', 'N', null)
    else
        select null, '222222222', 'N', pk)
        from ACCOUNTS
            where '222222222' = user_id and '2147483680' = pk
        select pk, '222222222', 'N', pk)
        on duplicate key update status='E'
end

I haven’t done anything about primary yet PLUS this one’s not the solution 😀

  • 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-12T17:49:00+00:00Added an answer on June 12, 2026 at 5:49 pm

    It sounds like you need two constraints:

    1) FOREIGN KEY constraint. This will ensure that any user_id inserted into ACCOUNTS exists in USERS.

    ALTER TABLE ACCOUNTS
    ADD FOREIGN KEY
    accounts_user_user_id (user_id)
    REFERENCES USERS (user_id)
    

    2) Self-referencing FOREIGN KEY constraint. This will ensure that any value inserted into edit_on is a valid value from pk in ACCOUNTS.

    ALTER TABLE ACCOUNTS
    ADD FOREIGN KEY
    accounts_accounts_edit_on (edit_on)
    REFERENCES ACCOUNTS (pk)
    

    Optionally, you can define what actions you want ON UPDATE and ON DELETE of the referenced key. You can read more about FOREIGN KEY constraints here.

    You need to make sure that user_id on both tables is of the same type, and that both tables are InnoDB.

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

Sidebar

Related Questions

I have tables foo and bar: create table foo(a int, b varchar(10), primary key
I have tables as below. Table Messages message_id parent_id forum_id user_id Table Users user_id
I have tables with following structure: -- Table structure for table `temp_app` -- CREATE
I have tables in a MySQL database like this... CREATE TABLE `someUserTable` ( userId
I have tables linked by FK, I query on the first table using entity
I have tables: Teachers and Students. We have a SpecialProjects Table where Teachers and
I have tables like tbl_biodata: id : int value=>1 name : varchar(50) value =>
have tables like this Table checklist idCardno name permAddress datetime Table persons name idcardno
I have tables item and store (it's a store management system). item table has
I have tables Users, Widgets and Layouts. Users have many-to-many relationship with Widgets via

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.