Tuesday, October 15, 2013

MySQL key, primary key and uinque key



KEY is normally a synonym for INDEX. Using key or index,  MySQL can quickly determine the position of the data  without having to look at all the data. It is much faster then reading sequentially.  MySQL uses indexes for the operations such as WHERE and LIKE for quick  search.

 A UNIQUE  key creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a UNIQUE index permits multiple NULL values for columns that can contain NULL.

A PRIMARY KEY is a special unique key. A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary key.

Example of MySQL code for these keys:
CREATE TABLE `FACULTIES` (
  `FACULTYID` int(11) NOT NULL auto_increment,
  `FACULTYCODE` varchar(4) NOT NULL default '',
  `TITLE` varchar(127) NOT NULL default '',
  `dean` varchar(7) NOT NULL default '',
  PRIMARY KEY  (`FACULTYID`),
  UNIQUE KEY `FACULTYCODE` (`FACULTYCODE`),
  KEY `dean` (`dean`)
) TYPE=MyISAM;


No comments:

Post a Comment