Warning: include(../adbrite_inline.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 17

Warning: include(../adbrite_inline.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 17

Warning: include() [function.include]: Failed opening '../adbrite_inline.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/primarykey.php on line 17

Warning: include(../mainbanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 32

Warning: include(../mainbanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 32

Warning: include() [function.include]: Failed opening '../mainbanner.htm' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/primarykey.php on line 32

Warning: include(../left.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 53

Warning: include(../left.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 53

Warning: include() [function.include]: Failed opening '../left.htm' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/primarykey.php on line 53
Delphi Tutorial

Primary Key


Warning: include(../ad1.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 68

Warning: include(../ad1.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 68

Warning: include() [function.include]: Failed opening '../ad1.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/primarykey.php on line 68

Primary key is a unique value which may not be same as the other value in a field of a table. This could be one of the attributes that are guaranteed unique, such as invoice number, Social Security Number, Student ID number in the table. Example imagine we have a telephone book application. Telephone Number Field certainly have a unique number, although the name of your friend may be same.

Almost all database applications have primary key to ensure that the resulting data does not have a double value.

Here is a demo of primary key, a phone book application with phone number as primary key. Prepare two tables, a table used to store the record has a unique value, in this example a table called 'phone.pk'. Another table as the main table, called as 'phone.dat'. Both tables have the same field name field that used as primary key record. Table primary key is used as a reference

  type //Main table
    TPhoneBook = record
      NumId: cardinal;
      Name: string[25];
      Phone: string[20];
      MobilePhone: string[20];
      Fax: string[20];
      Hide: boolean;
    end;
  type //Primary key table
    TPrimaryKey = record
      NumId: cardinal;
      Phone: string[25];
      Hide: boolean;
  end;
  
const
  FTableName = 'phone.dat';
  FTable_Primary_Key = 'phone.pk';  
  

 

To ensure that a phone number to be entered does not match the existing. Previously, a new phone number will be compared with all phone numbers in primary key tabel 'phone.pk'. If the phone number entered already exist, then insert record process will be cancel with the Exit procedure.

procedure TForm1.BtnPostClick(Sender: TObject);
var
  F1: File of TPhoneBook;
  F2: File of TPrimaryKey;
  UniqueNam: TPrimaryKey;
  PhoneOwner: TPhoneBook;
  i, size2: cardinal;
begin
  //Post a new record to main table 'phone.dat'
  //Check unique phnne number in phone.pk
  AssignFile(F2, FTable_Primary_Key);
  Reset(F2);
  try
    size2:= FileSize(F2);
    if Size2 <> 0 then
    begin
      for i:=0 to size2-1 do
      begin
        Seek(F2, i);
        Read(F2, UniqueNam);
        if CompareText(Edit2.Text, UniqueNam.Phone) = 0 then
        //Checking existing phone number
        begin
          ShowMessage('Phone number of '+ Edit2.Text + ' already used.');
          exit;
        end;
      end;
    end;
  finally
    CloseFile(F2);
  end;

  // Try to open the phone.dat file
  AssignFile(F1, FTableName);
  Reset(F1);
  try
    size:= FileSize(F1);
    seek(F1, size);
    //Write a couple of telephone records to the file
    PhoneOwner.NumId:= Size;
    PhoneOwner.Name:= Edit1.Text;
    PhoneOwner.Phone:= Edit2.Text;
    PhoneOwner.MobilePhone:= Edit3.Text;
    PhoneOwner.Fax:= Edit4.Text;
    PhoneOwner.Hide:= false;
    Write(F1, PhoneOwner);
   //Close the file
  finally
    CloseFile(F1);
  end;

  //Try to open phone.pk file as primarykey table
  AssignFile(F2, FTable_Primary_Key);
  Reset(F2);
  try
    size2:= FileSize(F2);
    seek(F2, Size2);
    //Write phone number as primary key
    UniqueNam.NumId:= Size2;
    UniqueNam.Phone:= Edit2.Text;
    UniqueNam.Hide:= false;
    Write(F2, UniqueNam);
  finally
    CloseFile(F2);
  end;
   BtnSizeClick(sender);
   Caption:= 'Primary Key Demo - Phone Book';
end;

For more details, please download the source code here.


Warning: include(../ad2.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 183

Warning: include(../ad2.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 183

Warning: include() [function.include]: Failed opening '../ad2.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/primarykey.php on line 183

Warning: include(../ulink468x15.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 185

Warning: include(../ulink468x15.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 185

Warning: include() [function.include]: Failed opening '../ulink468x15.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/primarykey.php on line 185

Warning: include(../fileroutines.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 188

Warning: include(../fileroutines.php) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 188

Warning: include() [function.include]: Failed opening '../fileroutines.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/primarykey.php on line 188

Warning: include(../right.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 202

Warning: include(../right.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 202

Warning: include() [function.include]: Failed opening '../right.htm' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/primarykey.php on line 202

Warning: include(../bottombanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 217

Warning: include(../bottombanner.htm) [function.include]: failed to open stream: No such file or directory in /home/makedata/public_html/delphitutorial/primarykey.php on line 217

Warning: include() [function.include]: Failed opening '../bottombanner.htm' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/makedata/public_html/delphitutorial/primarykey.php on line 217