MySQL Forums
Forum List  »  Perl

Re: How to use DBI module to retrive the primary key of any table
Posted by: Ben H.
Date: February 08, 2008 05:01AM

In my version of DBI primary_key_info is marked as experimental, so I try it
but without any success. But that's not really matter, think how you can
know which columns are primary keys at sql level? How it can be done? I give
you my response,

DESCRIBE tbl_name

And primary keys are colums from this output with column Key that contains PRI.
I am not sure at this point if this might be different between mysql version etc,
so take a look at yours.

Next we do this query ourselves, so with how describe outputs in my version I
have for example,

my $table = 'tbl_name';
my @row;
my @primary_keys;

my $dbh = DBI->connect(...);
my $sth = $dbh->prepare(qq{DESCRIBE $table});
$sth->execute();
while (@row = $sth->fetchrow_array) {
push @primary_keys, $row[0] if $row[3] eq 'PRI';
}

Depends on the rest of your script but I hope you might adapt this snippet
to your particuliar need.

Options: ReplyQuote


Subject
Written By
Posted
Re: How to use DBI module to retrive the primary key of any table
February 08, 2008 05:01AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.