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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:24:21+00:00 2026-06-02T05:24:21+00:00

I am quite the database noob :-( If I have a simple table designed

  • 0

I am quite the database noob 🙁 If I have a simple table designed like so:

CREATE TABLE IF NOT EXISTS picture
(
ID INT NOT NULL AUTO_INCREMENT,
userID INT NOT NULL,
name VARCHAR(150),

PRIMARY KEY(ID),
FOREIGN KEY (userID) REFERENCES user(ID)
)ENGINE=InnoDB

and described, for example, like so:

+----+--------+------+
| ID | userID | name |
+----+--------+------+
|  1 |      1 | john |
|  2 |      1 | jack |
|  3 |      2 | amir |
|  4 |      2 | chan |
|  5 |      2 | jose |
|  6 |      3 | jane |
|  7 |      3 | buba |
+----+--------+------+

How would I design a database which meets the following two constraints:

  1. Users 1, 2 and 3 can add new entries to the pictures table.
  2. Users 1, 2 and 3 can modify only those rows which have userIDs 1, 2 and 3, respecitvely

I know this might be non trivial, so feel free to ask any additional questions and I will edit this question accordingly.

  • 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-02T05:24:23+00:00Added an answer on June 2, 2026 at 5:24 am

    Drawing off of this answer I put this together:

    delimiter //
    CREATE TRIGGER trig_picture BEFORE UPDATE ON picture
    FOR EACH ROW
    BEGIN
        DECLARE dummy INT;
        IF NOT EXISTS (select ID from users where ID = OLD.userID and name = (SELECT SUBSTRING_INDEX(USER(), '@', 1))) THEN
            SET NEW=(SELECT CONCAT("Access Denied for user ", SUBSTRING_INDEX(USER(), '@', 1), "; uid ", OLD.userID));
        END IF;
    END;//
    delimiter ;
    

    This assumes you have a users table with some entries:

    CREATE TABLE IF NOT EXISTS users ( ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(150) );
    insert into users values (1, 'adam');
    insert into users values (2, 'bob');
    insert into users values (3, 'clark');
    

    This assumes the names in the users table matches the database user name as returned by SELECT SUBSTRING_INDEX(USER(), '@', 1).

    Also, note that this only prevents updates from happening. You’d have to create additional insert/delete triggers as well to prevent creation/deletion of entries.

    Finally, this doesn’t prevent someone from changing the userID of their entry in the pictures table, essentially locking them out from further edits. If this matters to you, you can add an additional check in the trigger for that.

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

Sidebar

Related Questions

I have quite a few situations where I have database structures similar to: TABLE
I quite like Rails' database migration management system. It is not 100% perfect, but
I have a quite simple database query: Query q = new Query(person); q.addFilter(name, Query.FilterOperator.EQUAL,
I have quite a large and complex table in a MySQL database that would
I have a web application project that works with a quite large database (over
I have quite a problem concerning the use of relational database concepts in Delphi
My Database was quite large with lots of test data in tables like watchdog,
I have a database that have quite a bit relational user information that would
I quite like that WMD is behaving nicely with my app. However, I have
In the web application I am currently developing, I have quite a few database

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.