php - How to select a multiple column primary key of an inserted row? -


problem:

i have following table in mysql. example lets there (and be) 1 person in world called "tom" "bell". (name, surname) primary key in table. every person has salary, unsigned integer.

create table if not exists `user` (   `name` varchar(64) not null default 'default_name',   `surname` varchar(64) not null default 'default_surname',   `salary` int(10) unsigned not null default '0',   unique key `id` (`name`,`surname`) ) engine=myisam default charset=utf8; 

whenever insert row using php script want function return primary key of inserted row (an array key=>value).

from php context not know primary key of table 'user' consists of , not need set primary key values (example 2, stupid, possible).

i can add argument insert function (for example pass table name, in case "user").

if matters, using pdo (php data objects) connect mysql database.

example 1:

$db->insert('insert `user` (`name`,`surname`,`salary`) values ('tom','bell','40');'); 

should return array:

$arr = ['name' => 'tom', 'surname' => 'bell']; 

example 2:

$db->insert('insert `user` (`name`,`salary`) values ('nelly','40');'); 

should return array:

$arr = ['name' => 'nelly', 'surname' => 'default_surname']; 

disclaimer & other information:

  1. i know not well-designed table, use auto_increment id column make easier , more efficient well. example show problem without having explain project structure.

  2. without loss of generality: using functions "getlastinsertid()" or "@@identity" return 0, guess reason because table not have auto_increment column.

  3. what have tried? nothing (other things stated in point 2 (which wouldn't work) , searching solution).

there aren't "nice" ways around problem. 1 of reasons having auto_increment avoid having problems described.

now, avoid answer 1 of take account half picture - realize inherit project or screw during initial stages , have fix things quickly.

to reflect on example - pk natural pk, not surrogate 1 auto_increment is. due fact it's implied know pk.

in example #1 - inserted tom bell - means knew pk tom bell since instructed mysql insert it. therefore, since knew pk before insert, know how return it.

in example #2 specified part of pk. however, table definition says thtat default values both name , surname default_surname. means, if omit either part of pk, know it'll assume default value. means know before insertion pk is.

since have use natural pk instead of surrogate, responsibility of "knowing" shifts instead of rdbms. there no other way of performing action. problem becomes more complex if allow default value become null. let insert more 1 tom null surname, , index constraint wouldn't apply (null not equal null, therefore (tom, null) not equal (tom, null) , insert can proceed).

long story short need surrogate pk or auto_increment. require based on description. if can't use have huge problem @ hands might not solvable.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -