开发者

Is There a Unique Record Key for Mysql?

I have a simple admin I'm creating for a client using PHP and Mysql. Anytime I wish to edit or delete a record I have to send the table name and unique index of my record(an auto incremented field I call index) in my query. This is fine except passing around a table parameter seems like it would be redundant if Msyql generates it's own unique index for EACH record in a particular database.

Anyone know if there is such a thing?

THE REASONING:

I'm making an admin table with (for this examp开发者_JS百科le) two columns. One column has a title, the other has a delete button. To build this table I query a table called inventory:

$query = "SELECT * FROM `inventory`";
$results = mysql_query($query);
$table = 'inventory';
$returnString = '
    <table>
        <tr>
            <th>Title</th>
            <th>Command</th>
        </tr>
    ';

while($data = mysql_fetch_assoc($results)){
    $returnString .='
        <tr>
            <td>' . $data['title'] . '</td>
            <td><input type="button" value="delete" onclick="remove_record(' . $data['index']. ',\'' . $data['table'] . '\')" />
        </tr>';
}

$returnString .='</table>';

return $returnString;

So, it's fine. It works fine except that I'd be happy NOT sending around two pieces of data (the index AND table name) through my javascript function, back through my formhandler back through my query to retrieve a piece of data I GENERATED FROM an original query.

It would be GREAT if there was some sort of GLOBAL reference to a record so I could create a function that would just eliminate a record from ANYWHERE in my database.


To my knowledge, MySQL doesn't create an expicit unique id (e.g. a global guid) for every record created in the database. I also don't think it makes sense for it to exist, since this job is already fulfilled by creating primary keys in each table.

The combination of the table name and that table's primary key are the unique value for the record since no other record in the database can hold that primary key for that table.

I'm at a loss to comprehend the reasoning behind this question. If you need to store a reference to a specific record in a separate table, then you should be adding a foreign key constraint on that other table to reference the primary key of the first table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜