MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql

Welcome. Please Login, Register, Or Activate! 
type your username and password to login
Date: November 22, 2009, 08:17 AM
430697 members and 297832 Topics
Latest Member: Sam C
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Webmasters (Moderators: OmniPotens, yawa-ti-de)  |  MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql  (Read 902 views)
*dhtml
MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
« on: February 26, 2009, 08:12 PM »

Iight guys. This is supposed to be an article, but questions can be asked though.

Minimal requirement for this thread is that, you MUST be able to create mysql databases
and integrate them with php. It is actually  a thread meant for advanced users but
i will entertain any questions asked relating to it no matter how foolish the question is.

For starters you may want to read this foreign article - http://www.mikebernat.com/blog/Setting-Up_a_Relational_Database_in_MySQL

So to continue with the intro. I am just doing a repeat broad-cast in 9ja language.

Many of you developers just create database and just carry go like that, and dont even care whether the database type is MyIsam or InnoDb.
If you are one of this, please read on rather than get annoyed with me.

Yes, you can say database is database, but if you care about efficiency like this. If i create the database of nairaland, i can wipe off all the  topics and posts i have ever created on nairaland using a one line common command like this: delete from users where username=dhtml - familiar command huh?

So, i am not here to teach you basic SQL codes but rather to give you some insight about creating clean and efficient databases that will reduce
your php codes greatly - think about it as a smarter way to lay out your databases.
*dhtml
Re: Myisam Vs Innobdb - Featuring Relational Dbase With Php / Mysql
« #1 on: February 26, 2009, 08:22 PM »

There are what we call storage engines in mysql. They are rather like plug-ins that do the actual storage of your tables.

The common types of storage engines are as follow:
-> MyISAM
-> InnoDB
-> Merge
-> Heap
-> Memory
-> BDB
-> ISAM

Please dont be bored 'cos i know some of you may be wondering WTF i am talking about, while some are just impatient
to find codes to copy - codes will come eventually but explanation first so that you will know how to use the codes.
Codes are not the aim of this thread - but rather table creation and optimization.

So MyISAM and InnoDb are the most commonly used storage engines - read all about it - http://www.mikebernat.com/blog/MySQL_-_InnoDB_vs_MyISAM
*dhtml
Re: Myisam Vs Innobdb - Featuring Relational Dbase With Php / Mysql
« #2 on: February 26, 2009, 08:37 PM »

Now our relational database at last using InnoDb.

So here is how i go about it.

Create a dbase - classDemo
Create a table called users.
fields:
id - autoincrement primary key
username - varchar(255)

users.sql
Code:
CREATE TABLE  `users` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 255 ) NOT NULL
) TYPE = MYISAM ;

Then you will now go to operations on phpmyadmin,
that place we usually rename database and all that.
You will now find the option to change your table to innodb.

The sql code directly will be
Quote
CREATE TABLE `users` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) TYPE=InnoDB AUTO_INCREMENT=1 ;

So next table will now be the profile table.
Notice that while creating tables in phpmyadmin
it will usually give you the option to change the
dbase storage engine. Default is usually MyISAM.


profile.sql
Quote
CREATE TABLE  `profile` (
 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
 `user_id` INT NOT NULL ,
 `comment` TEXT NOT NULL
) TYPE = INNODB COMMENT =  'profile';


So here is what i have done so far.


* db1.gif (43.78 KB, 760x728 )
*dhtml
Re: Myisam Vs Innobdb - Featuring Relational Dbase With Php / Mysql
« #3 on: February 26, 2009, 08:48 PM »

Now the tables have been created, let us now make them relational.

Explanation: id on users is autoincremented, so let us now register the following users:
omnipotens, yawatide

users.sql
Code:
CREATE TABLE `users` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) TYPE=InnoDB AUTO_INCREMENT=3 ;

INSERT INTO `users` VALUES (1, 'omnipotens');
INSERT INTO `users` VALUES (2, 'yawatide');

So the id of omni is 1 while yawa is 2. This ids are unique too.

So i now want to tie this ids to the profile table in a relationship model.

So, i now go back to the profile table and click on user_id and select the option
to make it an indexed field. Image attached below.

Then you will now click on relation view (this is never available in MyISAM) - so dont be bothered that
you have never seen it before. Let us see some screenshots before we continue.




* db2.gif (13.05 KB, 641x180 )

* db3.gif (26.12 KB, 881x535 )
*dhtml
Re: MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
« #4 on: February 26, 2009, 08:55 PM »

Now go to the relation view and link the user_id of profiles to the id of users table.
Dont be confused, follow the screenshot below. Then we continue from there.


* db4.gif (11.46 KB, 1067x301 )
*dhtml
Re: MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
« #5 on: February 26, 2009, 09:04 PM »

Now lemme explain what we have done in essence:
1. You cannot add an entry to the profile table without specifyin the user_id and the user_id must exist in the users table too as pid.
2. When you delete a particular user from the users table, all the comments that carry his user_id will be deleted at once.
If you are confused, please forget about that and continue reading. We are talking practicals as alwayz.

profile.sql
Code:
CREATE TABLE `profile` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL default '0',
  `comment` text NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `user_id` (`user_id`)
) TYPE=InnoDB COMMENT='profile' AUTO_INCREMENT=1 ;

--
-- Dumping data for table `profile`
--


--
-- Constraints for dumped tables
--

--
-- Constraints for table `profile`
--
ALTER TABLE `profile`
  ADD CONSTRAINT `0_78` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

So let us now insert records into the profile table.
Notice i entered the user_id of omnipotens in the screenshot. If you specify the user_id that does not match any users.id
that query will be rejected, you can try it with a value of 0 instead of 1 or 2,

valid.php
Code:
$sql = 'INSERT INTO `profile` (`id`, `user_id`, `comment`) VALUES (NULL, \'1\', \'Hi Y\'\'all, i am omnipotens, i got here first.\');';
mysql_query("$sql");


invalid.php
Code:
$sql = 'INSERT INTO `profile` (`id`, `user_id`, `comment`) VALUES (NULL, \'0\', \'Hi Y\'\'all, i am omnipotens, i got here first.\');';
mysql_query("$sql");




* db6.gif (17.7 KB, 745x643 )
*dhtml
Re: MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
« #6 on: February 26, 2009, 09:10 PM »

INSERT INTO `profile` (`id`, `user_id`, `comment`) VALUES (NULL, \'1\', \'Hi Y\'\'all, i am omnipotens, i got here first.\')

full profile table will be

Code:
CREATE TABLE `profile` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL default '0',
  `comment` text NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `user_id` (`user_id`)
) TYPE=InnoDB COMMENT='profile' AUTO_INCREMENT=7 ;

--
-- Dumping data for table `profile`
--

INSERT INTO `profile` VALUES (1, 1, 'Hi Y''all, i am omnipotens, i got here first.');
INSERT INTO `profile` VALUES (2, 1, 'Hi Y''all, i wish you happy sql coding.');
INSERT INTO `profile` VALUES (3, 1, 'Time to go. Yawa will address y''all later');
INSERT INTO `profile` VALUES (4, 2, 'Hi I am yawatide. DHTML na my friend o.');
INSERT INTO `profile` VALUES (5, 2, 'You guyz had beta start answering my quiz else, ');
INSERT INTO `profile` VALUES (6, 2, 'Gotta go now. Hve a bizzy day ahead of me.');

--


* db7.gif (19.6 KB, 736x565 )
*dhtml
Re: MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
« #7 on: February 26, 2009, 09:14 PM »

Notice that the user_id contains only 1 and 2. Specifyin the required users,

So to delete all the entries of omnipotens. All you need do is:
delete from users where id='1';
or whatever. Once that row is deleted, all the user_id that have same
value as users.id will be deleted automatically.

That method of linking the profiles with the users table can be employed to
tie more tables together. This is one of the powers of innodb aside
the row locking stuff.

And this is the end of my article, good night folks. Gotta get back to work.
Seun (m)
Re: MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
« #8 on: June 12, 2009, 12:17 AM »

Thanks for the tutorial.
*dhtml
Re: MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
« #9 on: June 12, 2009, 06:01 AM »

Hi chief, you are welcome as alwayz. . .
segsalerty (m)
Re: MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
« #10 on: June 12, 2009, 09:19 AM »

nice thread , didnt know this exists before,
bt , cool
thanks as always DHTML
smartsoft (m)
Re: MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
« #11 on: June 12, 2009, 05:32 PM »

OMO i don 4get all dis thing ooooooooooooooooooooo colours don take ova me finish
*dhtml
Re: MyISAM Vs InnoDb - Featuring Relational Dbase With Php / Mysql
« #12 on: June 12, 2009, 08:46 PM »

Quote from: smartsoft on June 12, 2009, 05:32 PM
OMO i don 4get all dis thing ooooooooooooooooooooo colours don take ova me finish
Just lyke codes don scatter my head finish!

@segsalert, i have lost track of my useful threads on webmasters sef, i will try rearrange then again someday.

Thanks to omnipotens for helping me bookmark some of them here, and i tried to link some of them together too. . .
 Naija360.com: Nigerian Picture Rating Site  The Agent Of Change Forum  Starting A Forum  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 


Sections: Autos/Cars (2) Jobs/Vacancies (2) (3) Career Talk Education General(2) Politics Romance Computers Phones Travel
Sports Fashion Health Religion Celebrities TV/Movies (2) Music/Radio (2) Books Webmasters Programming

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa. See also: Nairalist Classified Ads
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.