test_pk.pl
text/plain
Filename: test_pk.pl
Type: text/plain
Part: 0
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my @tables = qw(simple complexer);
my $dbh=DBI->connect("dbi:Pg:;port=2225","","")
or die "Couldn't connect.";
foreach my $table (@tables) {
my @key_column_names=$dbh->primary_key(undef, "public",$table);
print "PK column(s): ", join(", ", @key_column_names), ".\n";
my $sth=$dbh->primary_key_info( undef, "public", $table, {pg_onerow=>2});
if (defined $sth) {
my $pk = $sth->fetchall_arrayref()->[0];
print "Table $pk->[2] has a primary key on these columns:\n";
for (my $x=0; defined $pk->[3][$x]; $x++) {
print "Column: $pk->[3][$x] (data type: $pk->[6][$x])\n";
}
}
}
$dbh->disconnect();