You are on page 1of 5

Write For Us

Submit Tips

Subscribe to Print Edition

Contact Us

Search

HOME

REVIEWS

HOW-TOS

CODING

INTERVIEWS

FEATURES

OVERVIEW

BLOGS

SERIES

IT ADMIN

A Simple Guide to Database Design in MySQL


By Siddharth Mankad on May 1, 2011 in Concepts, For You & Me, How-Tos, Overview, Tools / Apps 0 Comments

Search for:

Search

Get Connected RSS Feed Twitter

As the title says, this is an introduction to databases, their purpose and features. It deals with how to design and create a simple database in MySQL. Starting off with the concepts of data and information, it is also a good read for those new to databases.
Its a Saturday morning, as I feed these words into a word processor. I have a browser running in the background, with a page on eBay open in one tab, and Engadget in another. The sheer simplicity of browsing through the product catalogues on eBay, or reading the latest and greatest tech news on Engadget, is living proof of the power of the database. The information age, the social networking age, the collaborative age, or whatever age you may want to call it, all revolve around data. Shouldnt the information age be revolving around, well, information? At this point, I would like to highlight a key difference between information and data. Information is processed data. From a philosophical standpoint, it has been wonderfully put by Gene Bellinger, Durval Castro and Anthony Mills in their article, aptly titled, Data, Information, Knowledge, and Wisdom; the cumulative order would be Data < Information < Knowledge < Understanding < Wisdom. As far as computing goes in day-to-day contexts, the buck stops at information databases, which, as the word suggests, deal with what underlies information, i.e., data. Consider the product page on eBay. All you see is information, the cohesive form, which consists of the product images, the description, the price, information about the seller, etc. The data is beneath this processed presentation in the browser. In this example, the individual components are data like the words in the description. The words, by themselves, cant stand independently on the page. Only when they are in context with one another, and with other data, do they convey something that makes sense. When they are processed, they turn into information. The domain of databases is vast, and fills volumes and volumes of mammoth publications. The scope of this article is to give you a taste and understanding of databases, how to go about designing one, and I will conclude with designing an actual but hypothetical database, and implementing it.

So, what are databases?

Databases are structures that hold data. The software that enables the flow of data through these structures is called a database management system or DBMS. The most widely used system of DBMS is something called RDBMS or Relational DBMS. This simply means that the data is stored in tables; moreover, whatever relationships that exist within the data are stored within tables. To put it simply, there are three parts that make a database: 1. Tables: We all know what tables are a matrix of rows and columns. In databases, its the same. Each row is a record, or a unit of data. A record (row) can have several columns or fields. Each field is like an attribute of that record. 2. Queries: A query is a question posed to the database, to retrieve a specific set of records, based on conditions supplied in the query. 3. Views: These are virtual tables, or (a set of) stored queries. At a physical level, the data is stored in data files specific to the DBMS. Examples of modern-day RDBMSs that are widely used include Oracle, MySQL, etc. Oracle is the largest commercially available RDBMS, and MySQL (earlier acquired by Sun, and subsequently by Oracle) is a free and open source RDBMS that is very well-known.
256,370 people like Open Source For You. Find us on Facebook

LINUX For You on

Follow

+2,530

Open Source For You


Like

Some other (important) concepts


A database is accessed, read from and written to, using an interface that allows applications to store and retrieve data. This interface is called an interface driver or database driver. There is usually more than one driver for an RDBMS. ODBC (Open DataBase Connectivity) is a popular database driver. So, if you write a program that needs to store and read data, you would use the API (Application Programming Interface a collection of functions) provided by ODBC to do so. Similarly, there is JDBC (Java DataBase Connectivity) and others. Lets suppose you are making a payment online, and the power goes out at the least opportune moment, say, just when you hit the Pay button (as we all know, Murphys Law never fails us; its like gravity, I guess). Did the money get deducted from your bank account? If it did, did the merchant receive it? If so, does the system know that it happened successfully? There are so many steps involved after you push that Pay button on Amazon, eBay or any ecommerce site; like the various messages exchanged between the participating banks, the merchant and the buyer (you). Once the power comes back, you can see what happened: the money has gone from your account, but the order status is Unpaid. The seller says he didnt receive the money. This is where the concept of a transaction comes in. If one step in the multitude of steps fails, then the action wont happen. Its an all-or-nothing scenario. Either your purchase will be complete, with all steps successful, or nothing happens. There is no in-between state. So, in our example, the bank will roll back the changes, and the money will reappear (depending, of course, on the banks time policies).

F acebook social plugin

Popular

Comments

Tag cloud

August 13, 2013 46 Comments Diksha P Gupta

India has immense under-utilised talent in the cloud security space


June 20, 2013 5 Comments sophie-samuel

New and amazing features of Linux


June 20, 2013 3 Comments Priyanka Sarkar

What it Takes to be an Open Source Expert


August 24, 2013 3 Comments Priyanka Sarkar

Secure Your Career with Ethical Hacking!


August 24, 2013 0 Comments Shashwat Pant

Get Fit With Android

Its about ACID


ACID stands for atomicity, consistency, isolation and durability. We have just seen what transactions are all about. The concept of ACID is nothing but a set of properties that define a successful execution of a transaction. The all-or-nothing attribute of the transaction is called atomicity. The data must be valid for the transaction to be successful; this is the consistency of the transaction. In the above example, its a guaranteed possibility that there will be many buyers making purchases on the site. The RDBMS must take care that each transaction is isolated from the rest, to prevent data with errors. This is the isolation of the transaction. And finally, we have durability, which is an assurance that the RDBMS makes that when the transaction completes successfully, it stays that wayirrespective of any form of failure that may occur once the transaction completes. Its also a way to make sure that the transaction is rolled back if it has failed intermediately.

Getting in touch with the data: SQL


The theory part is fundamentally in place now. But how does one actually communicate with an RDBMS? This is done by whats called SQL (Structured Query Language; pronounced sequel). SQL is pretty easy to understand, since its like broken English. Ill give you an example: There is a table that stores the names, addresses, cities, countries and phone numbers of college students. I want the RDBMS to show me all students who are from, say, Mumbai. This would be

the SQL statement for that query:


S E L E C T*F R O MS T U D E N T _ M A S T E RW H E R Ec i t y = ' M u m b a i ' ;

Here, the asterisk (* ) is synonymous to all its a wild-card character. The query will return all rows where the city is Mumbai. This SQL statement can be passed on to the RDBMS via an API function call in code, or through an application like phpMyAdmin. Upon receiving the query, the system returns the records as per the query. Suppose we want only names of the students from Mumbai, and not whole records (we dont want their addresses, phone numbers or countries of origin), the query would then become:
S E L E C Tn a m eF R O MS T U D E N T _ M A S T E RW H E R Ec i t y = ' M u m b a i ' ;

Note that the word Mumbai is in quotes because it is text. Numbers wont have quotes in a query. And the statement must end in a semi-colon. For the next part of this article, I will be using MySQL to create the database. Heres a quick way to go about installing and using it: Windows users: Download MySQL from its website (its called the MySQL Community Server). Just run the installer, and you should be okay. Linux users: Use the package manager and install m y s q l ; alternatively, you can build it from source, if you wish to. You can start MySQL through the command prompt (Windows) or terminal (Linux), by running m y s q lur o o tp . It will ask you for a password; keep it blank since the default is blank. You can change it later.

Types of SQL statements


SQL statements can be classified into the following four categories: DDL (Data Definition Language): This class of statement is used to create/destroy or define/change database components. Examples: C R E A T E ,D R O P ,R E N A M E . DML (Data Manipulation Language): As the name suggests, these statements manipulate the data itself, and the views related to it. Examples: S E L E C T ,I N S E R T . DCL (Data Control Language): These statements control access to the data. There are only two of them: G R A N Tand R E V O K E . TCL (Transaction Control Language): Transactions are handled using these statements. Examples: C O M M I T ,R O L L B A C K .

Designing a database
Heres where we design a database for some simple data. Lets say you want to make a database to store the names, addresses, phone numbers and birthdays of your friends and relatives. The first step is to see what kind of data you want to store: Names Addresses Phone Numbers Birthdays Lets begin by creating the database in MySQL:
m y s q l >C R E A T ED A T A B A S Em y c o n t a c t s ; Q u e r yO K ,1r o wa f f e c t e d( 0 . 0 0s e c ) m y s q l >U S Em y c o n t a c t s ; D a t a b a s ec h a n g e d

The U S Ecommand tells MySQL which database you want to be working on in this session. You can list the available databases using the following command:
m y s q l >s h o wd a t a b a s e s ; + + |D a t a b a s e | + + |i n f o r m a t i o n _ s c h e m a| |m y c o n t a c t s | |m y s q l | |p h p m y a d m i n | + + 4r o w si ns e t( 0 . 0 5s e c )

Now lets go table by table, and see what columns to put into each. These are the columns I put in:

1. Names: contact_id name 2. Addresses: contact_id address_line1 address_line2 city country email 3. Phone Numbers: contact_id landline mobile 4. Birthdays: contact_id birthdate So, we can now make four tables: N A M E S _ M A S T E R ,A D D R E S S _ M A S T E R ,P H O N E _ M A S T E Rand
B I R T H D A Y _ M A S T E R . The MySQL command for making N A M E S _ M A S T E Rwould be:
m y s q l >C R E A T ET A B L EN A M E S _ M A S T E R( c o n t a c t _ i dI N T , n a m eV A R C H A R ( 5 0 ) ) ; Q u e r yO K ,0r o w sa f f e c t e d( 0 . 1 4s e c )

Other tables can be made similarly. V A R C H A Rand I N Tare data-types. I N Tstands for integer. V A R C H A Ris a variable character. Its a dynamically changing field, whose size depends on the entered data. The V A R C H A R ( 5 0 )means the field can have a maximum size of fifty characters. Other supported data-types are listed in the MySQL documentation. Now lets explore the concept of primary and secondary keys. If you notice, the field c o n t a c t _ i d is common to all tables. This is to relate the tables by that common field. Here, the c o n t a c t _ i d in N A M E S _ M A S T E Ris the primary key, and the rest are secondary keys. In other words,
c o n t a c t _ i dis the reference for all the data in each table (as the secondary key), where it finally

relates to the name of the contact (as primary key). Once a table is made, you can populate it using a tool called phpMyAdmin. Doing justice to using phpMyAdmin would require another article; hence, I wont cover this aspect here. You can also populate using the I N S E R Tstatement:
I N S E R Ti n t ot a b l e _ n a m e( c o l u m n 1 ,c o l u m n 2 . . . ) v a l u e s( v a l u e 1 ,v a l u e 2 . . . ) ;

At the end of this exercise, you will have a database ready to use. This, frankly, is just the starting point in designing a database. I hope it proved helpful to you in understanding what goes on beneath the surface in cyberspace, at least when it comes to managing data.

Related Posts:
Connecting to MySQL with Python and PHP Installing and Using PostgreSQL Modules Database Programming in Python Lets Play with CodeIgniter FOSS is __FUN__: Get the Basics Right
Tags: acid, Amazon, Anthony Mills, API, database driver, database management system, Durval Castro, e commerce site, Gene Bellinger, information databases, interface driver, Java, jdbc, LFY May 2011, Murphy's Law, MySQL, odbc, Oracle, phpMyAdmin, product catalogues, RDBMS, SQL, Sun

Article written by:


Siddharth Mankad
The author is a new media designer, with an affinity for physical computing, software, code, and country music. Connect with him: Website

Previous Post

Next Post

A Quick-Start Practical Guide to Drupal Module Development

Testing Your Databases with DbUnit

ALSO ON LINUX FOR YOU

What's this?

India has immense under-utilised talent in the cloud 46 comments Cyber Attacks Explained: The Botnet Army 1 comment

Secure Your Career with Ethical Hacking! 3 comments GNOME Extensions Spicing Up the Desktop Experience 1 comment

0 comments Leave a message...


Newest Community Share

No one has commented yet.

C o m m e n t fe e d

Su b s cri b e vi a e m a i l

Reviews

How-Tos

Coding

Interviews

Features

Overview

Blogs

Search
Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems

For You & Me Developers Sysadmins Open Gurus CXOs Columns

All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.

You might also like