c# - Unexplainable MySQL composite key issue with .NET Application -
i writing .net application interact mysql database using odbc connection. application create database schema , tables on database upon start up. however, encountered weird unexplainable case below:
first application create following table
create table if not exists `sample` ( `item_id` varchar(20) not null, `item_no` int(11) not null, `sample_col1` varchar(20) not null, `sample_col2` varchar(20) not null, primary key (`item_id`, `item_no`) ) engine=innodb;
populate table with
insert sample set item_id='abc', item_no=1, sample_col1='', sample_col2=''; insert sample set item_id='abc', item_no=2, sample_col1='', sample_col2='';
then execute select query within .net application using following code:
dim query string query = "select item_id, item_no, sample_col1, sample_col2 sample" dim conn new odbcconnection("driver={mysql odbc 5.1 driver};server=localhost; port=3306;database=test; user=dbuser;password=dbpassword;option=3;") dim cmd new odbcdataadapter(query, conn) dim dt new datatable cmd.fillschema(dt, schematype.source) cmd.fill(dt) conn.close()
the cmd.fill(dt)
line throw exception: "failed enable constraints. 1 or more rows contain values violating non-null, unique, or foreign-key constraints".
however if modify table creation query to:
create table if not exists `sample` ( `item_id` varchar(20) not null, `item_no` int(11) not null, `sample_col1` varchar(20) not null, `sample_col2` varchar(20) not null, primary key (`item_no`,`item_id`) '--> here primary key order inverted ) engine=innodb;
the vb.net code works perfectly. notice on second creation query inverted order of primary key creation. on first example put item_id
first while on second example put item_no
first.
does has clue why happening? there difference point of view of mysql database between first , second create query?
any input appreciated. thank !
Comments
Post a Comment