9lessons programming blog
Loading Search
Wednesday, April 21, 2010

Database Design Create Tables and Relationships with SQL

This post explains how to design typical relationaships database for socialmedia web application. Today I'm presenting my labs.9lessons application database relations design and SQL with diagrams. I hope you like this post.

9lessons Labs Database Design.

labs.9lessons.info Data Modeling Design
Here you can see the five tables between relation flow user, friends, updates, comments and vote. This following image generated by using Mysql Workbench tool.
Database Design Flow Diagram

Here users table relation with three tables friends, updates and comments.

users table
Parent table contains all users data, while registration data storing in this table. Eg : labs.9lessons.info/9lessons
CREATE TABLE  `users` (
`user_id` INT NOT NULL primary key AUTO_INCREMENT ,
`username` VARCHAR(45) NULL unique,
`password` VARCHAR(45) NULL ,
`email` VARCHAR(45) NULL ,
`twitter_token` VARCHAR(99) NULL ,
`twitter_token_secret` VARCHAR(99) NULL
);

Data storing like this here password stored in encrypted formate tutorial link.
user table database design

friends table
Contains friend relation data. Here fri_one is FOREIGN KEY to REFERENCES users.user_id Eg : labs.9lessons.info/friends/srinivas
CREATE TABLE `friends` (
`friend_id` INT NOT NULL primary key AUTO_INCREMENT ,
`friend_one` INT NULL ,
`friend_two` INT NULL ,
`role` VARCHAR(5) NULL ,
FOREIGN KEY (friend_oneREFERENCES users(user_id)
);

Friends table data.
Friends Database Design Flow Diagram

updates table relation
Here updates table relation with three tables users, comments and vote.
Database Design Flow Diagram

updates table
Contains all users status updates data. Here user_id_fk is FOREIGN KEY to REFERENCES users.user_id Eg : labs.9lessons.info/9lessons
CREATE TABLE `updates` (
`update_id` INT NOT NULL primary key AUTO_INCREMENT ,
`update` TEXT NULL ,
`time` INT NULL ,
`host` VARCHAR(45) NULL , // Client IP address
`vote_up` INT NULL , // Up votes data
`vote_down` INT NULL , // Down votes
`user_id_fk` INT NULL ,
FOREIGN KEY (user_id_fkREFERENCES users(user_id)
);

Status updates:
Status Updates database design

comments table
Contains status updates comments/replies. Here two relations update_id_fk is FOREIGN KEY to REFERENCES updates.update_id and user_id_fk is FOREIGN KEY to REFERENCES users.user_id Eg:click here
CREATE TABLE `comments` (
`comment_id` INT NOT NULL primary key AUTO_INCREMENT ,
`comment` TEXT NULL ,
`time` INT NULL ,
`host` VARCHAR(45) NULL ,
`update_id_fk` INT NULL ,
`user_id_fk` INT NULL ,
FOREIGN KEY (update_id_fkREFERENCES updates(update_id ),
FOREIGN KEY (user_id_fkREFERENCES users(user_id)
);
Status comments database Design Flow Diagram

vote table
Contains voting clients IP address data (Allows one time voting). Here update_id_fk is FOREIGN KEY to REFERENCES updates.update_id Reference tutorials link click here
CREATE TABLE `vote` (
`vote_id` INT NOT NULL primary key AUTO_INCREMENT ,
`vote_host` VARCHAR(45) NULL ,
`update_id_fk` INT NULL ,
FOREIGN KEY (update_id_fkREFERENCES updates(update_id)
);
voting system database Design Flow Diagram

Sponsored Links

Share this post

Comments
{ 36 comments }
john said...

Good stuff!

Sava Intl said...

Great Job Very stuff work & Logic....

satyam said...

very very thanks to your quality information.satyam

Anonymous said...

Where's the next part?

Anil Kumar Panigrahi said...

Nice

9neo said...

thank.

Anonymous said...

wow! amazing thank you

Dharmveer Motyar said...

Really useful article, i cant wait for the next.
Keep rocking!!!!!!

Onur said...

thank.

AnisDesignz said...

Amazing Tutorial.

Praveen said...

Can i know what tool are you using for mysql for generating such diagrams.. Thank you.

Anonymous said...

good

arif said...

amazing..!!

V said...

waiting for next lesson :) thanks!

Abdullah Al Mamun said...

Awesome!
When the next part will come?!

rogeliorabbit said...

Thanks for this great tutorial!

Adel said...

Yes .. It's good stuff!

Anonymous said...

nice

Amit said...

Very well explained.. thanks for your hard work.

Chandu said...

Excellent. Thank you very much

Neha Bhatla said...

Wonderfull piece of information.Keep it up Sir!

csn-mm-davidobrien said...

Your a genious!
There should be more information like this on the web! Shame there is isnt

Thanks!

Anonymous said...

great tutorial!

Anonymous said...

How do I create a script when the input to a table of friends it will be friends_one, friends_two, role?

Muhammad Assegaf said...

thanks...........

Anonymous said...

very good ....help for database design..

moso said...

nice++++

Anonymous said...

dear friend.
i m not getting the use of friend_two?

Karan said...

Nice work. friend. i was stuck into this problem,and now it's solved. thanx :)

Chumhienk said...

hi srinivas..
this is friends table
SELECT a.username, a.email
FROM users a, friends b
WHERE a.user_id = b.friend_two
AND b.friend_one = '1'
AND b.role = 'fri'
ORDER BY b.friend_id DESC
LIMIT 30 ;
pertanyaan saya adalah bagaimana cara menampilkan user yang bukan teman, dengan menggunakan mysql?

Home said...

Hi..some one,, can u help me??
how to display SUGGEST FRIENDS with php and MYsql statement??
for the FRIENDS TABLE , you can see above..

Anonymous said...

what do you use for creating db schemas? they're pretty good;)

Srinivas Tamada said...

MySQL Workbench

Kevin said...

Great stuff, I love how neat and tidy this article is, very well organised and loving the pictures.

guest said...

Hi..some one,, can u help me??
how to display SUGGEST FRIENDS with php and MYsql statement??
for the FRIENDS TABLE , you can see above..

Big_Evil said...

hi srinivas, your examples are brilliant. Thanks. Do you have an asp.net example of this by any chance? I want to implement all these features in my project and I only know asp.net

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Like Me

follow me
products

9lessons labs

9lessons clouds

Android application

Chrome Extension