0

I have a table for users that includes a default user_id column. Additionally, there is a donator_id column that is null by default, but I want it to auto-increment in case the user_type is set to "donator". Can someone help me achieve this? **this is the expected results, I wanted a way to make "donator_id" only count users of type donator if else it should be null.

| user_id | name     | user_type | donator_id|
| ------- | -------- | --------- | ----------|
| 1       | user     | user      | null      |
| 2       | donator  | donator   | 1         |
| 3       | user2    | user      | null      |
| 4       | donator2 | donator   | 2         |
5
  • 2
    Wouldn't it make more sense for donator_id to contain the user_id of the donor as a foreign key?
    – Barmar
    Commented Jun 17, 2023 at 18:32
  • yes a normalisation redesign seems a good idwa
    – nbk
    Commented Jun 17, 2023 at 18:35
  • we used another table for donators but it seemed complex for us to work with because we aren't working with sql that much we're just doing it for our graduation project Commented Jun 17, 2023 at 18:36
  • Sample data is great, but we also need the expected result - i.e. a complete minimal reproducible example.
    – jarlh
    Commented Jun 17, 2023 at 18:40
  • this is the expected results actually, I wanted a way to make "donator_id" only count users of type donator if else it should be null. Commented Jun 17, 2023 at 20:52

1 Answer 1

0

as barmar said a restructring of your table, will helpo you keeptrack, ou can update all tabales and add row when needed

CREATE TABLE user_type 
    (`user_type` varchar(9), `usertype_id` int PRIMARY KEY )
;
    
INSERT INTO user_type 
    (`user_type`, `usertype_id`)
VALUES
    ('user', '1'),
    ('donator', '2')
;
CREATE TABLE users
    (`user_id` varchar(7), `name` varchar(8), `id_user_type` int,
    FOREIGN KEY (id_user_type)
        REFERENCES user_type(usertype_id)
        ON Update CASCADE)
;
    
INSERT INTO users
    (`user_id`, `name`,`id_user_type`)
VALUES

    ('1', 'user',1),
    ('2', 'donator',2),
    ('3', 'user2',1),
    ('4', 'donator2',2)
;

SELECT
  u.`user_id`, u.`name`, ut.user_type
FROM users u JOIN user_type ut  ON ut.usertype_id = u.id_user_type
user_id name user_type
1 user user
3 user2 user
2 donator donator
4 donator2 donator

fiddle

Much more intresting gets it, when a user can have multiple usertypes then you would also add a bridge, that will need to be inserted as well but has many benefits.

CREATE TABLE user_type 
    (`user_type` varchar(9), `usertype_id` int PRIMARY KEY )
;
    
INSERT INTO user_type 
    (`user_type`, `usertype_id`)
VALUES
    ('user', '1'),
    ('donator', '2')
;
Records: 2  Duplicates: 0  Warnings: 0
CREATE TABLE users
    (`user_id` varchar(7) Primary key , `name` varchar(8))
;
    
INSERT INTO users
    (`user_id`, `name`)
VALUES

    ('1', 'user'),
    ('2', 'donator'),
    ('3', 'user2'),
    ('4', 'donator2')
;

Records: 4  Duplicates: 0  Warnings: 0
CREATE TABLE users_user_type(
`user_id` varchar(7)
  , `id_user_type` int,
    FOREIGN KEY (id_user_type)
        REFERENCES user_type(usertype_id),  
    FOREIGN KEY (user_id)
        REFERENCES users(user_id)
        )
INSERt INTO users_user_type VALUEs (1,1),(2,2),(3,1),(3,2),( 4,2)
Records: 5  Duplicates: 0  Warnings: 0
SELECT
  u.`user_id`, u.`name`, ut.user_type
FROM users u 
  JOIN users_user_type uu ON uu.user_id = u.user_id
  JOIN user_type ut  ON ut.usertype_id = uu.id_user_type
ORDER BY u.user_id
user_id name user_type
1 user user
2 donator donator
3 user2 user
3 user2 donator
4 donator2 donator

fiddle

2
  • i'm not sure. but, I think the idea is not really clear to me Commented Jun 17, 2023 at 20:55
  • zser _taoe has only unique values and a promary key on its id, in the user table you add only the id of the user type. Problem could arrose, if someone is user and donator, then we would us a bridge table, i will add it to the example
    – nbk
    Commented Jun 17, 2023 at 21:15

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.