0

Is it possible to add mysql user into a database as column?

Lets say we have a following table:

CREATE TABLE message(
   id INT AUTO_INCREMENT NOT NULL,
   title VARCHAR(255),
   message TEXT,
   last_edited TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   PRIMARY KEY (id)
) ENGINE=INNODB;

and we want to add 'editor' column which would get the current mysql user and insert it there and also updates it automatically similar to the behaviour of the TIMESTAMP datatype. Can this be done and if so what sort of datatype should I use (is there such a type as USER or should I use simple varchar) and what kind of syntax is involved?

1 Answer 1

1
CREATE TABLE message(
   id INT AUTO_INCREMENT NOT NULL,
   title VARCHAR(255),
   message TEXT,
   last_edited TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   editor VARCHAR(50) NOT NULL,
   PRIMARY KEY (id),
   UNIQUE INDEX `editor`(`editor`)
) ENGINE=INNODB;

And while inserting; use

INSERT INTO `message`(`columns..`, `editor`)
    VALUES( <YOUR VALUES>, CURRENT_USER() )
ON DUPLICATE KEY UPDATE
    `editor` = CURRENT_USER();
Sign up to request clarification or add additional context in comments.

1 Comment

And if I wanted to add this column during the table creation instead of altering it after the fact?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.