问题
I am designing a database which will be used for internationalized content. One of the features is a tagging system, similar to what Stack Overflow has. This is what I've got:
TABLE tags
tag_id | int(11) unsigned | NOT NULL | PRI | auto_increment
TABLE tag_translations
| tag_translation_id | int(11) unsigned | NOT NULL | PRI | auto_increment
| fk_language_id | int(11) unsigned | NOT NULL | MUL |
| fk_tag_id | int(11) unsigned | NOT NULL | MUL |
| tag_name | varchar(255) | NOT NULL | UNI |
TABLE tag_relationships
| tag_relationship_id | int(11) unsigned | NOT NULL | PRI | auto_increment
| fk_tag_id | int(11) unsigned | NOT NULL | MUL |
| fk_solution_id | int(11) unsigned | NOT NULL | MUL |
First of all, does it seem reasonable to have that tags table containing nothing but an ID? Second, how could I populate that column with the only field being an auto incrementing ID?
回答1:
Q1: No. I can't see the point of the tags
table. It seems not to mean anything. If you can't explain the semantic value of a row in that table (what does an entry in that table represent in the real world?), then it probably doesn't belong. I suspect it is only there in order to give you auto_increment
, which would not be a good answer. You can assign your own IDs.
Q2: already answered.
I also don't see what tag_translation_id
and tag_relationship_id
are for. A habitual use of auto_increment
?
I think what I'd do for basic structure is:
create table tag_translations (
tag_id int not null,
language_id int not null,
tag_name varchar(255),
primary key (tag_id, language_id)
);
create table tag_relations (
tag_id int not null,
solution_id int not null,
primary key (tag_id, solution_id)
);
To which I'd add metadata and indexes as needed. Two column indexes are very nice for junctions such as tag_relations
because of the server's 'Using index' optimization:
Using index
The column information is retrieved from the table using only information in the index tree without having to do an additional seek to read the actual row. This strategy can be used when the query uses only columns that are part of a single index.
Btw, for internationalized systems, 255 isn't a magic number for varchar
field length unless you're sticking to single-byte encodings. If you're using UTF-8, look at the manual and give it some thought, especially if you are going to index that column.
回答2:
As I say in my comments to the OP, I had exactly the same problem a few years ago. I was using SQL Server rather than MySql, though, but the problem was the same.
Unfortunately, the only solution I found was to add extra columns to the Tags table. I decided to add a DateCreated column which, eventually, turned out to be useful.
回答3:
INSERT INTO tbl_name () VALUES();
来源:https://stackoverflow.com/questions/6481505/one-column-sql-table-the-column-being-an-id-is-this-crazy