Jump to content

Guys, how could I make my table layouts better?


Namtip

Recommended Posts

I've looked on Google but I can't find any real life examples of database layouts. I've looked at normalization, I think I've got the jist of it, so I've had a crack at creating my own database.

 

The site will have two types of users.

 

Seller: has 1 page(his own internet shop) that sells a number of items from that same page. The user will also have a his own profile and information bit.

 

Buyer: does not need a profile, he just has his own contact information stored so he can buy stuff.

 

$query = 'CREATE TABLE IF NOT EXISTS user (
			name_id						INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
			name						VARCHAR(50)	NOT NULL,
			password    				CHAR(41)    NOT NULL,
			admin_level TINYINT UNSIGNED NOT NULL DEFAULT 0
	PRIMARY KEY (name_id)
    )
    ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));

// create the user information table
$query = 'CREATE TABLE IF NOT EXISTS contact (
			name_id						INTEGER UNSIGNED NOT NULL,
			first_name  				VARCHAR(30) NOT NULL,
			last_name   				VARCHAR(30) NOT NULL,
			email						VARCHAR(50),
			address						VARCHAR(50) NOT NULL,
			city        				VARCHAR(30),
			county       				CHAR(25),
			post	     				VARCHAR(9),
			home						VARCHAR(13),
			mobile						VARCHAR(13),

	FOREIGN KEY (name_id) REFERENCES user(name_id)
    )
    ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));

$query = 'CREATE TABLE IF NOT EXISTS profile (
			name_id					INTEGER UNSIGNED NOT NULL,
			bio						VARCHAR(500),
			info					VARCHAR(500),

	FOREIGN KEY (name_id) REFERENCES user(name_id)
    )
    ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));

//create the images table
$query = 'CREATE TABLE images (
        item_id       		INTEGER      	NOT NULL AUTO_INCREMENT,
        item_title  		VARCHAR(255) 	NOT NULL,
        item_medium 		VARCHAR(255) 	NOT NULL,
        item_dimensions 	VARCHAR(255) 	NOT NULL DEFAULT "",
        item_date     		DATE         	NOT NULL,
	item_price			NUMERIC(9,2) 	NOT NULL,
	item_quantity		INTEGER 		NOT NULL,

        PRIMARY KEY (image_id)
    )
    ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));


$query = 'CREATE TABLE IF NOT EXISTS transactions (
        item_id       		INTEGER      	NOT NULL,
        name_id  			INTEGER      	NOT NULL,
        name_id 			INTEGER			NOT NULL,
        credit_card_type 	VARCHAR(255) 	NOT NULL DEFAULT "",
        card_no     		INTEGER         NOT NULL,
	name_on_card		VARCHAR(50)		NOT NULL,
	datetime			DATETIME		NOT NULL,
	image_quantity		INTEGER 		NOT NULL,

        FOREIGN KEY (image_id)
    )
    ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));

 

Is this a good database? How could I make it better? I know the values are abit off. Is it possible to store the two name_ids (buyer and seller) in the tranaction table like that? Do you have to store credit card details for transactions? If there are any sites of real examples of business databases I'd love to have a look if you could forward them. I'm not too concerned about indexes at this point.

 

Link to comment
Share on other sites

Hi

 

Several of your tables have no primary keys.

 

Your transactions table has name_id twice.

 

You have a field for credit card type on the transactions table which is a varchar(50). I would suggest your split off credit card type to a separate table and just use the id from that table in the transactions table (as I assume that credit card type will be the same for many rows).

 

Image dimensions I would split off to 2 separate columns, one for width and one for height to allow them to be easily searched.

 

Until fairly recent versions of MySQL the max length of a varchar field was 255, and a few of yours are longer than that.

 

Hope that helps.

 

All the best

 

Keith

Link to comment
Share on other sites

Thanks for the input I've replaced one of my name_ids with name, I will also incorporate the things you said about image dimensions and a seperate table for credit car type.

 

Hi

 

Probably a TEXT field, but depends on whether you need any compatibility with older versions of MySQL.

 

All the best

 

Keith

 

"VARCHAR is now up to 65K in v5 -- almost no need to ever use TEXT, which is *very* evil... ...TEXT is evil because it silently forces mysql to make a temporary table for group by / sorting operations... amongst other evil things." - Fenway - http://www.phpfreaks.com/forums/index.php?topic=189579.0

 

Not trying to be a smart ass, just trying to figer out what not to use.

 

My database would not be using an older sql database. So I guess Varchar would be best?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.