Home > database > mysql_table_key.m

mysql_table_key

PURPOSE ^

returns the primary key name of a mysql table. If a composite key, returns a cell array of strings

SYNOPSIS ^

function primary_key = mysql_table_key(table,conn_id)

DESCRIPTION ^

returns the primary key name of a mysql table. If a composite key, returns a cell array of strings
 primary_key = mysql_table_key(table,conn_id)

 Author(s):
 Sept 23, 2009 - Stefan Tomic, first version

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function primary_key = mysql_table_key(table,conn_id)
0002 %returns the primary key name of a mysql table. If a composite key, returns a cell array of strings
0003 % primary_key = mysql_table_key(table,conn_id)
0004 %
0005 % Author(s):
0006 % Sept 23, 2009 - Stefan Tomic, first version
0007   
0008   
0009 try
0010   conn_id;
0011 catch
0012   conn_id= 0;
0013 end
0014 
0015 mysql_check_conn(conn_id,'open');
0016 
0017 [tableDescription{1:6}] = mysql(conn_id,sprintf('describe %s',table));
0018 
0019 %Key type is fourth column. There doesn't seem to be a general way to reference this
0020 keyType = tableDescription{4};
0021 
0022 primaryIdxs = strmatch('PRI',keyType);
0023 
0024 if(length(primaryIdxs) == 1)
0025   primary_key = tableDescription{1}{primaryIdxs};
0026 else
0027   primary_key = tableDescription{1}(primaryIdxs);
0028 end

Generated on Wed 20-Sep-2023 04:00:50 by m2html © 2003