Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti valued attributes in a table
    primarykey
    data
    text
    <p>I have designed a send message system in which users can send a message to more than one person at a time. so the recipient attribute of a message is multi valued. so i designed an other table called message_recipients. </p> <p>Here are my tables: </p> <pre><code>create table `message`( `message_id` INT(11) NOT NULL primary key auto_increment, `sender` VARCHAR(32) NOT NULL, `topic` VARCHAR(100) NOT NULL, `content` TEXT NOT NULL, `message_date` TIMESTAMP NOT NULL , `readed` VARCHAR(1) NOT NULL DEFAULT 0, `deleted_by_sender` VARCHAR(1) NOT NULL DEFAULT 0, `deleted_by_recipient` VARCHAR(1) NOT NULL DEFAULT 0, FOREIGN KEY(sender) REFERENCES users(user_name) )ENGINE=InnoDB DEFAULT CHARSET=utf8; create table `message_recipients`( `message_id` INT NOT NULL , `recipient` VARCHAR(32) NOT NULL , PRIMARY KEY(message_id,recipient), FOREIGN KEY(message_id) REFERENCES message(message_id) on delete cascade, FOREIGN KEY(recipient) REFERENCES users(user_name) )ENGINE=InnoDB default charset=utf8; </code></pre> <p>When i insert a message in <code>message</code> table i need to get its <code>message_id</code> and insert it with recipient or recipients into <code>message_recipients</code> table. </p> <p>Here is my script: </p> <pre><code>insert into `message`(`sender`,`topic`,`content`) values('me','need help','how to get last inserted row'); select message_id from `message` order by `message_id` desc limit 0,1; </code></pre> <p>When i got the <code>message_id</code>, i can now insert it into <code>message_recipients</code> with some users as recipient, and now question is that; is there any better or more optimized way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload