0

Consider you have table USER which has primary key on field USER_ID. And every table in your db should contain MODIFIED_BY field with id of actual user. My aim is to specify default value on that field which equals id of user with appropriate name. F.E. you have user with name system and you want every record of every table to have id of this user in MODIFIED_BY field. As well you want all the new records to satisfy this.

6
  • You want USER(). Commented Dec 12, 2016 at 13:31
  • didn't understand what do you mean Commented Dec 12, 2016 at 13:34
  • Does every user have their own login to the database? Or are you using a single Id for a connection string and have a "user" table which contains application user credentials? or something else? Commented Dec 12, 2016 at 13:55
  • I'm a little confused. Do you have a question? Commented Dec 12, 2016 at 14:22
  • @antongarakh . . . In many situations, you can use the database security mechanisms to identify users. The USER() function provides information about the current connected user. If you are managing security and logins yourself, then this might not be the right solution. Commented Dec 12, 2016 at 14:38

1 Answer 1

2

Assuming that you are referring to other sorts of users than database users (e.g. 'registered users'), maybe triggers are the solution to your problem. With two triggers, one fired when inserting and one when updating, you can specify the value of a modified_at-column using custom code, e.g. select user_id from ... where ...

Please check the following sample code, which should directly answer your question; hope it solves your problem. Otherwise, please give us more information / some more context.

DELIMITER |

drop table if exists sometable
|
CREATE TABLE `sometable` (
  `somecolumn` int(11) DEFAULT NULL,
  `modified_by` int(11) DEFAULT NULL
)
|
drop table if exists registered_user
|
CREATE TABLE registered_user (
  user_id integer primary key,
  name varchar(50)
)
|
drop trigger if exists sometable_inserted
|
drop trigger if exists sometable_modified
|
CREATE TRIGGER sometable_inserted BEFORE INSERT ON sometable
FOR EACH ROW
BEGIN
SET new.modified_by := (select user_id from registered_user where name=@name);
END;
|
CREATE TRIGGER sometable_modified BEFORE UPDATE ON sometable
FOR EACH ROW
BEGIN
SET new.modified_by := (select user_id from registered_user where name=@name);
END;
|

DELIMITER ;

insert into registered_user values (5, 'joe');
insert into registered_user values (6, 'sam');

set @name='sam';
insert into sometable(somecolumn) values (1);
insert into sometable(somecolumn) values (2);
set @name='joe';
update sometable set somecolumn = 3 where somecolumn = 2;
Sign up to request clarification or add additional context in comments.

2 Comments

Yes this is exactly what I need! Thanks a lot!
You're welcome. If it solved your problem, please be so kind to mark the answer as 'accepted'.

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.