views:

41

answers:

2

I have two pieces of data: 1) User and 2) Portfolio. Each user has only one portfolio. At the registration process user has to fill out the following fields:

  • First name
  • Last name
  • Porfolio title
  • Portfolio url
  • Email
  • Password

My DB structure at the moment looks like this:

CREATE TABLE `User` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(80) NOT NULL,
  `password` varchar(128) NOT NULL,
  `firstName` varchar(30) NOT NULL,
  `lastName` varchar(30) NOT NULL,
  `ip` varchar(15) NOT NULL,
  `lastVisit` int(10) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '2',
  `created` int(10) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  UNIQUE KEY `email_UNIQUE` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


CREATE TABLE `Portfolio` (
  `userId` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `url` varchar(45) NOT NULL,
  `theme` tinyint(2) NOT NULL DEFAULT '1',
  `font` tinyint(2) NOT NULL DEFAULT '1',
  `footer` varchar(255) NOT NULL,
  `isFeatured` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`userId`),
  UNIQUE KEY `id_UNIQUE` (`userId`),
  UNIQUE KEY `url_UNIQUE` (`url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

But now looking at the database I think there is no reason to have two separate tables. The other thing is I don't really want to mix user fields and portfolio fields.

What would you do in this case?

Any suggestions are greatly appreciated.

Thank you!

+3  A: 

Scenarios in which you might want to keep them separate:

  • A User can exist without having a Portfolio (do you have other types of users besides those who have portfolios?).

  • A new Portfolio can be created for a User who didn't have one.

  • Two users can swap portfolios (actually this would be tricky since the userid is the primary key of Portfolios).

  • You might someday support multiple Portfolios per User, or multiple Users per Portfolio, or both.

  • You want to manage SQL privileges to give some database user access to one table but not the other (and don't have a database that supports column-level privileges or view-level privileges).

  • You want to back up the two tables separately.

  • You want to monitor space consumed by the two tables separately.

  • You really, really like to use SELECT * instead of naming columns explicitly, and you still want to be able to select each subset of columns. And you don't want to use views for that.

  • You want to get the most out of your InnoDB buffer pool for performance, and buffering smaller rows means you store more rows in the same size buffer. Tip: declare ip as int unsigned instead of varchar(15).

Bill Karwin
+1 quite an extensive list of reasons why to keep those two tables separate, even if currently a fixed 1:1 relationship exists! Great work, Bill.
marc_s
+2  A: 

a user is a person, a portfolio is not.

two tables.

Steven A. Lowe
Oh sure, bring logical modeling into it! :-)
Bill Karwin
That's what I meant by saying "I don't really want to mix user fields and portfolio fields." :)
Josh T.
"Each user has only one portfolio"...until they don't. ;-)
Steven A. Lowe