简体   繁体   中英

Bool field as a unique index

Can a Bool field become a unique index and used for a foreign key relationship in conjunction with another table's index?

I don't think it can become primary key but what about unique index? Every time I try to make it into a unique index it converts it into a tinyint.

EDIT

So I have a SQL relationship problem. Lets say I have a database where I want to keep records of information about individuals. Now I have setup a table to take on that information. Okay so far so good.

Often times duplicate information can be discovered in the table and it would be removed. A record is considered a duplicate if a particular field has the same value as another field in another row. Example: Duplicate emails.

Now I want to create another table in the database to keep track of every duplicate that is ever discovered and deleted. My first thought into this was to create a Foreign Key relationship. So I created and then connected a dupes table to my persons table. The relationship was a simple Foreign to Primary key relationship with an on delete constraint.

Now while that may have worked at first the problem arose that the dupes table was receiving records that were deleted even if they were not deleted because they were dupes. This was a problem because even if I decided to delete a person from the persons table just because I did not like them, they would stored in the dupes table anyway.

Then I thought, why not create a disposition field in the persons table and connect that as a unique or primary key to my dupes table's index foreign key. Well the problem is a unique key must have a unique value so multiple dispositions of dupe or I don't like you would not work. The other option was to make the disposition field a primary key. That has the same problem though.

What would be the right relationship for this problem?

Essentially a bool-value in sql (or bit in mssql) is an very short integer. So basically there's no reason that it would not be allowed as foreign key. But from a design perspective it does not make any sense.

Yes, BOOL field can be a primary or unique key. In case of unique key it is possible to set NULL as value.

About the TINYINT: MySQL does not support BOOL data type, it uses TINYINT instead.

From the reference - BOOL, BOOLEAN: These types are synonyms for TINYINT(1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM