Board index » delphi » Creating primary key in access

Creating primary key in access

Delphi 7 Pro, Access XP

In TableParticipants, the primary key currently is MemberID,Event.  I need to
change the key to MemberID,Event,EventDate.  I need to do this at the program
start when it checks for current database version.  I figured a simple drop key
and add key SQL statement would do the trick, but can't get the syntax right.
Any help correcting my code would be appreciated.

with QueryChange do begin
   {this portion works}
   Close;
   SQL.Clear;
   SQL.Add('alter table Participants drop PrimaryKey');
   ExecSQL;

   {this portion doesn't}
   Close;
   SQL.Clear;
   SQL.Add('alter table Participants add PrimaryKey (MemberID,Event,EventDate)');
   ExecSQL;
end;

Jon

 

Re:Creating primary key in access


try this (untested)

SQL.Add('alter table Participants add constraint PrimaryKey  Primary Key
(MemberID,Event,EventDate)');

Andy

Quote
"Jon Purvis" <jon.pur...@tpwd.state.tx.us> wrote in message

news:3f79f148@newsgroups.borland.com...
Quote
> Delphi 7 Pro, Access XP

> In TableParticipants, the primary key currently is MemberID,Event.  I need
to
> change the key to MemberID,Event,EventDate.  I need to do this at the
program
> start when it checks for current database version.  I figured a simple
drop key
> and add key SQL statement would do the trick, but can't get the syntax
right.
> Any help correcting my code would be appreciated.

> with QueryChange do begin
>    {this portion works}
>    Close;
>    SQL.Clear;
>    SQL.Add('alter table Participants drop PrimaryKey');
>    ExecSQL;

>    {this portion doesn't}
>    Close;
>    SQL.Clear;
>    SQL.Add('alter table Participants add PrimaryKey

(MemberID,Event,EventDate)');

- Show quoted text -

Quote
>    ExecSQL;
> end;

> Jon

Re:Creating primary key in access


That did the trick. Thanks.
Quote
Andy wrote:
> try this (untested)

> SQL.Add('alter table Participants add constraint PrimaryKey  Primary Key
> (MemberID,Event,EventDate)');

> Andy

> "Jon Purvis" <jon.pur...@tpwd.state.tx.us> wrote in message
> news:3f79f148@newsgroups.borland.com...

>>Delphi 7 Pro, Access XP

>>In TableParticipants, the primary key currently is MemberID,Event.  I need to
>>change the key to MemberID,Event,EventDate.  I need to do this at the program
>>start when it checks for current database version.  I figured a simple drop key
>>and add key SQL statement would do the trick, but can't get the syntax right.
>>Any help correcting my code would be appreciated.

>>with QueryChange do begin
>>   {this portion works}
>>   Close;
>>   SQL.Clear;
>>   SQL.Add('alter table Participants drop PrimaryKey');
>>   ExecSQL;

>>   {this portion doesn't}
>>   Close;
>>   SQL.Clear;
>>   SQL.Add('alter table Participants add PrimaryKey
>>    (MemberID,Event,EventDate)');
>>   ExecSQL;
>>end;

>>Jon

Other Threads