I'll put this in big letters as it's really important... DO NOT QUERY THE DATABASE DIRECTLY!
When you use managed metadata within a webs lists and libraries, a local copy is placed in a hidden list in the root web of the Site Collection that you're currently in. You can get to this by appending the url "/Lists/TaxonomyHiddenList/AllItems.aspx" to the root web url.
Basically when you tag a document or list item with managed metadata, this list is checked for the term to exist. if it doesn't, it's added to this list and then a lookup is attached to the item that you're tagging that points to this item. The unique ID that you see is the ID of the local copy of the tag in this hidden list.
Hope this helps.
Paul.
PS, Just in case it wasn't clear enough, DON'T QUERY THE DATABASE DIRECTLY 8-)
www.myfatblog.co.uktwitter.com/@cimares