參考代碼:sql
1 /** 2 * 獲取主鍵字段 3 * @param $table 4 * @param $database 5 * @return mixed 6 */ 7 public function get_primary_key($table,$database){ 8 $sql = "SELECT 9 k.column_name 10 FROM 11 information_schema.table_constraints t 12 JOIN 13 information_schema.key_column_usage k 14 USING 15 (constraint_name,table_schema,table_name) 16 WHERE 17 t.constraint_type='PRIMARY KEY' 18 AND 19 t.table_schema='$database' 20 AND 21 t.table_name='$table'"; 22 return Db::query($sql)[0]["column_name"]; // 數據庫查詢語句根據狀況而定 23 24 }