0

I have two tables. 'users' and 'paying_users'

Paying_users have a bit more information regarding payments of course.

I am looking for a way on which upon insertion I will be able to force a UNIQUE username that is common to both tables. So that if 'Shelly' is on 'users', I will not be able to INSERT 'Shelly' to 'paying_users'. Is that possible on MYSQL?

4 Answers 4

3

Why don't you use users as main table and relate the paying_users (which are a part of the users, I reckon) to the id of users? Then, you would not have a problems with duplicates.

users: 
  id: ...
  name: ...
  other information

paying_users:
  id: ...
  users_id: ...
  other information:
2
  • But what if Shelly is in paying_users ? I might accidently insert her in the users and create two users with the same name
    – Ted
    Aug 14, 2011 at 20:24
  • I would put every user into users, or if users or not paying users, than creating a table user_data, where you store all the data of the user and user the others only for additional info. If payment is the only difference you could also store it in one table and use a boolean field 'paying' to separate them from each other. Make up your mind which which version works best for you ...
    – Andreas
    Aug 14, 2011 at 21:22
2

Paying_users are still users, right? Then this would solve your problem:

users: 
  id: ...
  name: ...
  other information that relates to a user
  PRIMARY KEY: id
  UNIQUE KEY: name

paying_users:
  id: ...
  other information that relates a paying user only
  PRIMARY KEY: id
  FOREIGN KEY: id
      REFERENCES users(id)
  • With this structure, when you want to add a paying_user, you do it in two steps:
    • 1 add him to table users (so his name is unique across all users)
    • 2 add his id to table paying_users, along with other data related to paying users only.

Further advantages:

  • When you have a (simple) user that decides to pay, you just do step 2.

  • When you have a paying_user that his paying period expired, you remove his record from paying_users table only, so he still remains as a simple user.

1

I don't think it is possible to do this in MySQL. It would be better to design a single users table if possible. You would then create an auxillary table containing the user id and pyament info for those customers that are paying and you'd be able to join when necessary

0

Try combining your INSERT with a SELECT. e.g.:

INSERT INTO paying_users
SELECT 'Shelly', 'other', 'column', 'values'
WHERE (SELECT COUNT(1) FROM users WHERE username = 'Shelly') = 0

I'm not sure, having not tried this particular construct, but you may need to create a 1 row dummy table to select your data from:

INSERT INTO paying_users
SELECT 'Shelly', 'other', 'column', 'values'
FROM (SELECT 1) AS dummy
WHERE (SELECT COUNT(1) FROM users WHERE username = 'Shelly') = 0
2
  • What is happening here really ? What is the WHERE returning ? I am not familiar with that kind of syntax. What does SELECT 1 return ? What does it mean to have a (SELECT COUNT(1) FROM users WHERE username = 'Shelly') = 0 .... could you explain please? Thanks
    – Ted
    Aug 15, 2011 at 16:20
  • The statement SELECT COUNT(1) FROM users WHERE username = 'Shelly' will return the number of users which have a username of 'Shelly'. The brackets around this statement will insert that result into the surrounding where clause, making it essentially WHERE %matching_users% = 0. This will have the effect of only returning a row when there isn't a matching user, and therefore only inserting when there isn't a matching user. This isn't really the best way to do it though, if changing your underlying database design is an option. In that case, I would go with ypercube's answer.
    – Gus
    Aug 15, 2011 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.