use dBI function look for Primary key for MYSQL db table.

S

sam

Hi group,

I m wondering how to use DBI function to determine a field is (or is
part of) the primary key in a table?

I tried somethine like the following but have no luck to make it work:
print "This is PK<br>\n" if ($sth->{PK_NAME});

Thanks
Sam
 
M

Matt Garrish

sam said:
Hi group,

I m wondering how to use DBI function to determine a field is (or is part
of) the primary key in a table?

I tried somethine like the following but have no luck to make it work:
print "This is PK<br>\n" if ($sth->{PK_NAME});

I think there is a method in the DBD::mysql driver to determine which is the
primary key, but if all else fails get a description of the table:

(untested, but known to work)

use DBI;

my $dbh = DBI->connect('dbi:ODBC:somedb', 'xxx', 'yyy', {RaiseError => 1,
AutoCommit => 1});

my $sel_sth = $dbh->prepare("DESCRIBE mydatabase");

$sel_sth->execute();

while (my $col = $sel_sth->fetchrow_hashref) {

print "The primary key is $col->{'Field'}" if $col->{'Key'} eq 'PRI';

}

Matt
 
M

Michael Budash

Matt Garrish said:
I think there is a method in the DBD::mysql driver to determine which is the
primary key, but if all else fails get a description of the table:

(untested, but known to work)

use DBI;

my $dbh = DBI->connect('dbi:ODBC:somedb', 'xxx', 'yyy', {RaiseError => 1,
AutoCommit => 1});

my $sel_sth = $dbh->prepare("DESCRIBE mydatabase");

$sel_sth->execute();

while (my $col = $sel_sth->fetchrow_hashref) {

print "The primary key is $col->{'Field'}" if $col->{'Key'} eq 'PRI';

}

Matt

the DBD::mysql driver method you were thinking of is "is_pri_key".
"perldoc Mysql" will explain the details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top