Foreign keys and Referential Integrity in database

Foreign keys in a database :

A foreign key in a My Sql database is a field (or fields) of a table that points to a primary key of an other table. Such keys are defined in a database to ensure that only fields that supposed to appear in a database may be permitted.

What is the need of a foreign key :

For a long time MySQL do not support foreign keys being afraid of the fact that it would erode the speed and performance of the RDBMS. But considering the fact that foreign key relationships can play an important role in catching data entry errors and may result in a stronger and better-integrated database, MySQL included the foreign keys.

In order to set up a foreign key relationship between two MySQL tables, three conditions must be met:

  1. Both tables must be of the InnoDB table type.
  2. The fields used in the foreign key relationship must be indexed.
  3. The fields used in the foreign key relationship must be similar in data type.

Following examples may help you understand the need and use of foreign keys.

EXAMPLE :

We have two tables, say Students and Marks.

Students Table :-

Roll No.                  Name                        Surname
001                       Sulabh                      Jain
002                       Pratik                      Verma
003                       Kuldeep                     Kumar
004                       Anupam                      Jain

Marks Table :-

S No                         Name                         Roll No                    Subject                        Marks

1                                Sulabh                       001                          Maths                           87

2                                Sulabh                      001                           English                         76

3                                Kuldeep                   003                           English                         81

4                                Pratik                        002                          Chemistry                   72

In the Marks table, the field Roll No is a foreign key referenced to the Roll No field of Students table.

Creating a table with a foreign key :

CREATE TABLE `marks`

( `S No` int(10) unsigned NOT NULL AUTO_INCREMENT,

`Name` varchar(45) NOT NULL,

`Roll No` int(10) unsigned NOT NULL,

`Subject` varchar(45) NOT NULL,

`Marks` int(10) unsigned NOT NULL,

PRIMARY KEY (`S No`), KEY `Roll No` (`Roll No`),

CONSTRAINT `Roll No` FOREIGN KEY (`Roll No`)

REFERENCES `Students` (`Roll No`) )

ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

Foreign Key Constraints :

Try the following query,

INSERT INTO TABLE  Marks (S No, Name, Roll No, Subject, Marks) values (5, ‘Anupam’, 005, ‘Quantum’, 81);

(Notice that there is no value like 005 in the Roll No column of Students table to which the Roll No column of Marks table is referenced)

If we try to enter a value for the field Roll No in II table (Marks) which do not exists in the Roll No column of I table (Students), in such a case we will get an error like,

Cannot add or update a child row: a foreign key constraint fails

How to drop a FOREIGN KEY :

We use the ALTER TABLE command with the DROP FOREIGN KEY clause, for dropping a foreign key,

ALTER TABLE  Marks  DROP FOREIGN KEY Roll No ;
Query OK, 1 row affected (0.11 sec)
Records: 1  Duplicates: 0  Warnings: 0

Whether any key is a foreign key or not, could be easily checked by using the SHOW CREATE TABLE query.

References :

1. SQL Foreign Keys, http://ezdia.com/SQL_Foreign_Key/Content.do?id=1294

2. Referential Integrity, http://ezdia.com/Referential_Integrity_in_databases/Content.do?id=1295

Open Source Applications on Internet

Hello friends, In this post I want to share some of the open source software and applications which are widely used by different web developers. These software and IDE with included default servers are available on internet for free downloading. Many well known websites who are attracting huge number of users are based on these platforms.

Netbeans :

The latest edition which I have worked upon is Netbeans 6.7.1

The NetBeans IDE is an open-source integrated development environment. NetBeans IDE supports development of all Java application types (Java SE including JavaFX, (Java ME, web, EJB and mobile applications) out of the box. Among other features are an Ant-based project system, Maven support, refactorings, version control (supporting CVS, Subversion, Mercurial and Clearcase).

Features of Netbeans 6.7,

  • The newest features in NetBeans 6.7 involve the integration of Project Kenai – a collaborative environment for developers to host their open-source projects – native Maven support and Hudson integration.(Read more on Kenai .. http://ezdia.com/Netbeans_and_Kenai/Content.do?id=997)
  • It offers enhancements for Java, PHP, Ruby, JavaScript, Groovy and C/C++
  • It brings support for SVG Rich Components, remote debugging in Ruby and the latest version of GlassFish

Minor updates with Netbeans 6.7.1 are that it does add support for the latest features of JavaFX with JavaFX SDK 1.2 being bundled with the IDE. It also incorporates bug fixes included in Patch 1 for the NetBeans 6.7 release.

Netbeans 6.8 is also released

Why Netbeans and not Eclipse ?

  • Netbeans is very fast (atleast on Linux, it is way faster than Eclipse, One can say that it should approach Eclipse on Windows)

License: From July 2006 through 2007, NetBeans IDE was licensed under Sun’s Common Development and Distribution License (CDDL), a license based on the Mozilla Public License (MPL). In October 2007, Sun announced that NetBeans would henceforth be offered under a dual license of the CDDL and the GPL version 2 licenses, with the GPL linking exception for GNU Classpath

Download Links :

http://netbeans.org/downloads/

MySQL :

MySQL Client Version 5.1.11 (it is the latest)

MySQL is the world’s most popular open source database software, with over 100 million copies of its software downloaded or distributed throughout its history. With its superior speed, reliability, and ease of use, MySQL has become the preferred choice for Web, Web 2.0, SaaS, ISV, Telecom companies and forward-thinking corporate IT Managers because it eliminates the major problems associated with downtime, maintenance and administration for modern, online applications.

  • MySQL is the world’s most popular open source database software, with over 100 million copies of its software downloaded or distributed throughout its history
  • With its superior speed, reliability, and ease of use, MySQL has become the preferred choice for Web, Web 2.0, SaaS, ISV, Telecom companies and forward-thinking corporate IT Managers.
  • It eliminates the major problems associated with downtime, maintenance and administration for modern, online applications.
  • MySQL is a key part of LAMP (Linux, Apache, MySQL, PHP / Perl / Python), the fast-growing open source enterprise software stack.

MySql advantages over Oracle :

MySQL is characterised as a fast, robust database with a good feature set, but one which lacks all the extras of something like Oracle … So if we are writing code that’s designed to be portable it’s a pretty good choice.  Admin / security are effective but the setup of these and othe features isn’t over-complicated.

Download Links :

http://dev.mysql.com/downloads/

Tortoise SVN version is 1.6.6.

TortoiseSVN 1.6.6, Build 17493 – 32 Bit
Subversion 1.6.6,
apr 1.3.8
apr-utils 1.3.9
neon 0.28.6
OpenSSL 0.9.8k 25 Mar 2009
zlib 1.2.3

This software includes software developed by collabnet (www.Collab.Net)

This software includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit (www.openssl.org)

Open-source website – http://tortoisesvn.net/downloads

Tortoise Advantages over other CVS Clients:-

  1. Subversion gives us the confidence that when we commit, everything went into the repository.
  2. The ability to back out changes before going to production–using an activity branching model, we can allow developers to branch per activity and only merge to the main source base after code reviews have been performed.
  3. If there are problems, we have one revision we can back out that includes the full change-set for that change. While the repository level re-visioning was a shift for my developers to make that didn’t happen immediately, it begins to make sense when an activity had to be removed from the build-it’s easier to see what people are working on as the commits hit our internal commit mailing list. Since we tag each release, we’re able to determine which source code contributed to a release.
  4. It’s easier to see what people are working on as the commits hit our internal commit mailing list. Since we tag each release, we’re able to determine which source code contributed to a release.

Openfire 3.6.4 (Chat Server)

Openfire is an open source real time communication (RTC) server. Openfire uses the only widely adopted open protocol for instant messaging and group chat, XMPP (also called Jabber). Openfire is incredibly easy to setup and administer, but offers rock-solid security and performance. Jive Software leads the open source source project at Igniterealtime.org  and uses the technology to power real- time features in their Clearspace products.
Openfire and Spark use the open XMPP protocol (also called “Jabber”), the only broadly-adopted instant messaging protocol and an approved standard by the IETF. With many server implementations and dozens of clients, it’s also the only protocol with proven interoperability. The XMPP protocol is supported and continually enhanced by the active XMPP Software Foundation community.

Opensource Website - http://www.igniterealtime.org/projects/openfire/index.jsp

References:

  1. Netbeans and Kenai : http://www.ezdia.com/Netbeans_and_Kenai/Content.do?id=997
  2. Netbeans Keyboard Shortcut keys : http://www.ezdia.com/Netbeans_Keyboard_Shortcut_keys/Content.do?id=1119
  3. About MySQL : http://www.ezdia.com/About_My_SQL/Content.do?id=1168
  4. Books on MySQL : http://www.ezdia.com/Recommended_Books_of_MySQL/Content.do?id=60
  5. PHP and MySQL Guide for beginners : http://www.ezdia.com/PHP_and_MySQL_Guide_for_Beginners/Content.do?id=601
  6. Tortoise SVN Downloads: http://www.ezdia.com/TortoiseSVN_downloads/Content.do?id=1169
Follow

Get every new post delivered to your Inbox.