I’m trying to create a single table for private messaging on a website. I created the following table which I think is efficient but I would really appreciate some feedback.
CREATE TABLE IF NOT EXISTS `pm` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`to` int(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`subject` varchar(255) DEFAULT NULL,
`message` text NOT NULL,
`read` tinyint(1) NOT NULL DEFAULT '0',
`deleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
FOREIGN KEY (user_id) REFERENCES User(user_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I have 2 columns that determine the status of the message: read and deleted
If read = 1, the message has been read by the receiver. If deleted = 1, either the sender or the receiver deleted the message from the sent or received inbox. If deleted = 2 both users deleted the message, therefor delete the row from the database table.
A few comments:
Charset=latin1is going to piss some people of I’d suggestcharset=utf8.I’d suggest putting a foreign key check in not only on
user_id, but ontoas well.Also I’d put an index on
date, as you will be doing a lot of sorting on that field.You need to split deleted in two fields, otherwise you will not know which user has deleted the message. (
deleted_by_user,deleted_by_recipient)Note that
dateis a reserved word and you’ll need to change it intomessage_dateor`backtick`it in your queries.