Return a table primary key attribute name
* @param string $db Database Name
@param string $table Table Name
@return array
558547
public function getPrimaryKey ($db, $table)
	{
		
		// Get pk name
		
		return $this->db->query ('SHOW COLUMNS FROM ' . $db . '.' . $table . ' WHERE `Key` = \'PRI\'')->fetch (\PDO::FETCH_ASSOC);
		
	}
574881
function GetForeignKeys($table)
    {
        $sql = new Sql\Builder($this);
        $fields = array('TABLE_SCHEMA', 'TABLE_NAME', 'REFERENCED_COLUMN_NAME', 'CONSTRAINT_NAME');
        
        $tbl = $sql->Table('information_schema.key_column_usage', $fields);
        
        $where = $sql->Equals($tbl->Field('TABLE_SCHEMA'), $sql->Value($this->dbName))->
                And_($sql->Equals($tbl->Field('TABLE_NAME'), $sql->Value($table)))->
                And_($sql->IsNotNull($tbl->Field('REFERENCED_COLUMN_NAME')));
        $what = $sql->SelectList($tbl->Field('CONSTRAINT_NAME'));
        
        
        $select = $sql->Select(true, $what, $tbl, $where);
        $result = array();
        $reader = $this->ExecuteQuery((string)$select);
        while ($reader->Read())
        {
            $result[] = $reader->ByIndex(0);
        }
        return $result;
    }
807990
public function primaryKey ($tableName)
		{
			if (! isset($this->primaryKeys[$tableName])) {
				$sql = "SELECT *
					    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
					        JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.Constraint_name
					    WHERE tc.CONSTRAINT_TYPE = 'Primary Key'
					        AND tc.TABLE_NAME = " . $this->quote($tableName);

				$keyResult                      = $this->query($sql);
				$key                            = $keyResult->fetchRow();
				$this->primaryKeys[$tableName]  = $key['COLUMN_NAME'];
			}

			return $this->primaryKeys[$tableName];
		}
1132666
public function getTableKeys($table)
	{
		// Get the details columns information
		$this->setQuery('SHOW KEYS FROM ' . $this->quoteName($table));
		$keys = $this->loadObjectList('Key_name');

		return $keys;
	}
1057782
public function getMasterNodeForTable($database_table)
    {
        $list = $this->getTables(true);

        if (empty($list[$database_table])) {
            return [];
        }


        $result = [];
        foreach ($list[$database_table] as $node=>$row)
        {
            if ($row['is_leader']) {
                $result[] = $node;
            }
        }
        return $result;
    }
1317370
protected function getPhinxTablePrimaryKey($attributes, $table)
    {
        $alternatePrimaryKeys = $this->getAlternatePrimaryKeys($table);
        if (!empty($alternatePrimaryKeys)) {
            $attributes[] = "'id' => false";
            $valueString = '[' . implode(', ', $alternatePrimaryKeys) . ']';
            $attributes[] = "'primary_key' => " . $valueString;
        }

        return $attributes;
    }
297697
public function getTableSchemas($database = 'base')
    {
        $driver = $this->getDatabase($database)->getDriver();
        $connection = $this->establishConnection($database, Database::TARGET_SLAVE);
        $names = $driver->getTableNames($connection);

        $schemas = [];
        foreach ($names as $name) {
            $schemas[] = $this->getTableSchema($name, $database);
        }

        return $schemas;
    }
133836
public function getPrimaryKey($table)
    {
        //Argument 1 must be a string
        Argument::i()->test(1, 'string');
        
        $query = $this->utility();
        $results = $this->getColumns($table, "`Key` = 'PRI'");
        return isset($results[0]['Field']) ? $results[0]['Field'] : null;
    }
127216
public function getPrimaryKey($table)
    {
        $results = $this->getColumns($table, "`Key` = 'PRI'");
        return isset($results[0]['Field']) ? $results[0]['Field'] : null;
    }
685795
public function keys($key, $db = -1)
    {
        if(-1 != $db) {
            $this->execute('select', array($db));
        }

        return $this->execute('keys', array($key));
    }
546652
private function getTableDiffForAlterForeignKey($table)
    {
        if (! $table instanceof Table) {
            $tableDetails = $this->tryMethod('listTableDetails', $table);

            if ($tableDetails === false) {
                throw new DBALException(sprintf('Sqlite schema manager requires to modify foreign keys table definition "%s".', $table));
            }

            $table = $tableDetails;
        }

        $tableDiff            = new TableDiff($table->getName());
        $tableDiff->fromTable = $table;

        return $tableDiff;
    }
869888
public function getPrimaryKey($table, $resultSet = null){
		if($resultSet instanceof PDOStatement){
			$i = 0;
			while($col = $resultSet->getColumnMeta($i++)){
				if(in_array('primary_key', $col['flags']))
					$primaryKey = $col['name'];
			}
		}
		if(isset($primaryKey))
			return $primaryKey;
		throw new ImplementationException;
	}
1219874
public function getPrimaryKey($table)
    {
        $query = $this->getUtilityQuery();
        $results = $this->getColumns($table, "`Key` = 'PRI'");
        return isset($results[0]['Field']) ? $results[0]['Field'] : null;
    }
671193
public function getTableColumns(string $table): array {
		$query = <<<QUERY
			SELECT COLUMN_NAME
			FROM INFORMATION_SCHEMA.COLUMNS
			WHERE TABLE_SCHEMA = :database
			AND TABLE_NAME = :table
QUERY;
		
		$params = [
			"database" => $this->database,
			"table"    => $table,
		];
		
		try {
			$results = $this->getCol($query, $params);
			return is_array($results) ? $results : [];
		} catch (\PDOException $databaseException) {
			throw $this->prepareDatabaseException($databaseException, $query, $params);
		}
	}
1265609
protected function findPrimaryKey($table)
	{
		$kcu='INFORMATION_SCHEMA.KEY_COLUMN_USAGE';
		$tc='INFORMATION_SCHEMA.TABLE_CONSTRAINTS';
		if (isset($table->catalogName))
		{
			$kcu=$table->catalogName.'.'.$kcu;
			$tc=$table->catalogName.'.'.$tc;
		}

		$sql = <<<EOD
		SELECT k.column_name field_name
		FROM {$this->quoteTableName($kcu)} k
		LEFT JOIN {$this->quoteTableName($tc)} c
		ON k.table_name = c.table_name
			AND k.constraint_name = c.constraint_name
		WHERE c.constraint_type ='PRIMARY KEY'
			AND k.table_name = :table
			AND k.table_schema = :schema
EOD;
		$command = $this->getDbConnection()->createCommand($sql);
		$command->bindValue(':table', $table->name);
		$command->bindValue(':schema', $table->schemaName);
		$primary=$command->queryColumn();
		switch (count($primary))
		{
			case 0: // No primary key on table
				$primary=null;
				break;
			case 1: // Only 1 primary key
				$primary=$primary[0];
				break;
		}
		return $primary;
	}
1066762
public function getPrimaryKey($table){
		$key = '';
		$q = $this->runQuery('SHOW FULL COLUMNS FROM ' . $table);
		while($col = $this->fetch($q)){
			if(strtolower($col['Key']) === 'pri' && $key === '')
				$key = $col['Field'];
		}
		return $key;
	}
246549
protected function getKeySql($columns)
	{
		// TODO Error checking on array and element types.

		$kNonUnique = (string) $columns[0]['Non_unique'];
		$kName      = (string) $columns[0]['Key_name'];
		$kColumn    = (string) $columns[0]['Column_name'];

		$prefix = '';

		if ($kName === 'PRIMARY')
		{
			$prefix = 'PRIMARY ';
		}
		elseif ($kNonUnique == 0)
		{
			$prefix = 'UNIQUE ';
		}

		$nColumns = \count($columns);
		$kColumns = array();

		if ($nColumns === 1)
		{
			$kColumns[] = $this->db->quoteName($kColumn);
		}
		else
		{
			foreach ($columns as $column)
			{
				$kColumns[] = (string) $column['Column_name'];
			}
		}

		$sql = $prefix . 'KEY ' . ($kName !== 'PRIMARY' ? $this->db->quoteName($kName) : '') . ' (' . implode(',', $kColumns) . ')';

		return $sql;
	}