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 :D

th3an0maly
  • 3,360
  • 8
  • 33
  • 54
  • 2
    Are you constrained to using MyISAM engine, or can you use InnoDB? Trying to do this using case statements isn't going to work very well in a concurrent environment. – PinnyM Oct 12 '12 at 18:27
  • 1
    Simply switch to InnoDB and define proper foreign keys. Everything else will be a error-prone and ugly workaround. –  Oct 12 '12 at 18:47
  • I can use InnoDB, no probs :) But I still dont have a complete solution :( – th3an0maly Oct 12 '12 at 18:53
  • Please look at http://stackoverflow.com/questions/12877732/mysql-trigger-for-updating-same-table-after-insert as my problem is still unsolved – th3an0maly Oct 13 '12 at 23:58

1 Answers1

1

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.

Kermit
  • 33,827
  • 13
  • 85
  • 121
  • Switch to InnoDB. And for requirement 3 - either use a trigger, or a constraint on a calculated field. – PinnyM Oct 12 '12 at 18:26
  • @PinnyM: MySQL parses but ignores check constraints. – eggyal Oct 12 '12 at 18:28
  • your point 1 is fine, but regarding 2, i also need to make sure that the `user_id` is same too. Also, what if the value of `edit_on` is null? – th3an0maly Oct 12 '12 at 18:32
  • 1
    @eggyal, how sad. Perhaps just switch to another DBMS then. Just kidding, you can emulate check constraints using one of several harebrained methods. Google is your friend. – PinnyM Oct 12 '12 at 18:32
  • @th3an0maly When you mean `same`, do you mean that the inserted `user_id` matches the parent `user_id`? – Kermit Oct 12 '12 at 18:33
  • exactly. in short, it means that the person editing the account is the original owner – th3an0maly Oct 12 '12 at 18:34
  • @njk Could you pls see the question again? I've edited requirement# 2 slightly, and have still not got a clue as to what to do about 3. I've defined the foreign keys – th3an0maly Oct 13 '12 at 19:23
  • Here's a link to how the problem looks now http://stackoverflow.com/questions/12877732/mysql-trigger-for-updating-same-table-after-insert – th3an0maly Oct 13 '12 at 23:58