in reply to DBI determine primary key
This is an old question, but I saw that it was referenced on StackOverflow and had some bad comments.
You can get a list of columns by looking at 'NAME' and an array of primary flags by looking at 'mysql_is_pri_key'. Because multiple columns can be part of the primary, you need to look at each column in turn. This is the code I use in my generic table updater.
I am going to assume you already have a database handle ($dbh) and a table name ($table)
my $sql = "select * from $table LIMIT 0,1"; my $tnames = $dbh->prepare($sql); $tnames->execute(); my $names = $tnames->{NAME}; my $prime = $tnames->{'mysql_is_pri_key'}; my $count = @{$names}; foreach my $pos (0...($count-1)){ print "Looking at column $names->[$pos] "; if($prime->[$pos] eq 1){ print "this column is part of the PRIMARY KEY\n"; }else{ print "it is just a regular column\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: DBI determine primary key
by Corion (Patriarch) on Oct 25, 2019 at 07:19 UTC |