Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could I make a column in a table only allows one 'true' value and all other rows should be 'false'

Tags:

mysql

In MySQL, Can I put a restriction like below at database level?

I need to make sure 1. a specific column can only take either 'true' or 'false' values. 2. and exactly only one row should be having 'true' value?

like image 632
Reddy Avatar asked Jul 12 '10 11:07

Reddy


People also ask

Can a column have multiple values?

i want to know how can we store multiple values in a sql column, or which is the best way to store the values in a sql column. which is the best way to store the Genre values in database. The best way by far, is NOT to store multiple values in a column. It violates normal form.

Can we make multiple rows and columns in a table?

Method 2: Add Multiple Rows or Columns with Contextual Menu At first, select a number of rows or columns. Next right click and choose “Insert”. Then choose the right command as you need. The number of rows or columns is as the same as that of the rows or columns you selected at the very first.

Can we insert only one column value in SQL?

Insert Data Only in Specified Columns It is also possible to only insert data in specific columns.

How do I select only one column in a table?

To select columns, choose one of the following options: Type SELECT , followed by the names of the columns in the order that you want them to appear on the report. Use commas to separate the column names.


1 Answers

Rather than having the boolean attribute in the table, you could have another table that contains one row and points to the row in the original table that you consider true.

Changing the true value is a matter of updating the foreign key in the TrueRow table.

like image 196
Gilbert Le Blanc Avatar answered Sep 20 '22 08:09

Gilbert Le Blanc