Here is a code extract from phpPgAdmin that dumps UNIQUE and PRIMARY
constraints.  Feel free to use the query...

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of Felipe Diaz Cardona
Sent: Sunday, January 14, 2001 7:17 AM
To: [EMAIL PROTECTED]
Subject: [HACKERS] primary keys


Hi.

Dos any one know any sql sentence to Find primary keys in a table.

I'm using postgresql v.7.0 (Mandrake 7.2)
        // Generate constraint clauses for UNIQUE and PRIMARY KEY constraints
        $sql_pri_keys = "
                SELECT 
                        a.attname AS column_name,
                        i.indisprimary AS primary_key,
                        i.indisunique as unique_key,
                        ic.relname AS index_name
                FROM 
                        pg_class bc,
                        pg_class ic,
                        pg_index i,
                        pg_attribute a
                WHERE 
                        i.indrelid = bc.oid
                        and i.indexrelid = ic.oid
                        and 
                        (
                                i.indkey[0] = a.attnum 
                                or
                                i.indkey[1] = a.attnum
                                or
                                i.indkey[2] = a.attnum
                                or
                                i.indkey[3] = a.attnum
                                or
                                i.indkey[4] = a.attnum
                                or
                                i.indkey[5] = a.attnum
                                or
                                i.indkey[6] = a.attnum
                                or
                                i.indkey[7] = a.attnum
                        )
                        and a.attrelid = bc.oid
                        and i.indproc = '0'::oid
                        and bc.relname = '$table'
        ";

        $result = @pg_exec($link, pre_query($sql_pri_keys)) or pg_die();

        $i = 0;
        while ($row = @pg_fetch_array($result, $i++)) {
                if ($row[column_name] != $drop_field) {
                        if ($row[primary_key] == "t") {
                                if (!empty($primary_key)) {
                                        $primary_key .= ", ";
                                }
                                $primary_key .= 
"$cfgQuotes$row[column_name]$cfgQuotes";
                                $primary_key_name = $row[index_name];
                        } elseif ($row[unique_key] == "t") {
                                $schema_create .= "   CONSTRAINT 
$cfgQuotes$row[index_name]$cfgQuotes UNIQUE 
($cfgQuotes$row[column_name]$cfgQuotes),$crlf";
                        } else {
                                $index_create .= "CREATE INDEX $cfgQuotes" . 
$row[column_name] . "_$table" . "_key$cfgQuotes ON $cfgQuotes$table$cfgQuotes 
($cfgQuotes$row[column_name]$cfgQuotes);$crlf";
                        }
                }
        }
        if (!empty($primary_key)) {
                $schema_create .= "   CONSTRAINT $cfgQuotes$primary_key_name$cfgQuotes 
PRIMARY KEY ($primary_key),$crlf";
        }

Reply via email to