What to assign for MySQL column that must not be empty? - mysql

I have a MySQL table named customers. In this table, there is a column called fullname that must not be empty when a new row is created. Is it right to assign a Not Null attribute to this column to imply that the column cannot be empty?

It a word - yes. If a column may not be empty, it should be defined as not null.

Related

mysql colomn value in other column with extra value

In my table in mysql, I have 4 columns, but I added one extra column. And the value of the last column must be the value of column 1 with extra value.
for example:
UPDATE users SET user = 'column_1_value'."00";
It's like merging column value with extra value.
Thanks inadvance !
since you did not provide enough context like the datatypes your table consists of I can just guess. If they are string types (like varchar) you can use concat:
UPDATE users SET column_4 = CONCAT(column_1, "00");

SELECT all COLUMN_NAMES from a table that have a specific entry

I have a table with 21 columns and I need to find all column names that have a specific value in them. This goes beyond my knowledge of database querying.
If this is possible, how would I do this?
Do you need to find columns in which the name of the column contains a certain value or there exists a row in which the given column contains the given value?
In the first case you could try the show columns: http://dev.mysql.com/doc/refman/5.0/en/show-columns.html

how to set one field in a table equal to another field in a table in access

I have a table with two fields, IDCopy and ID. I want to copy the value of ID into IDCopy because ID is a number field and I need a second copy of this field as a text field.
I am used to doing things like this on sql server
UPDATE table SET table.IDCopy= table.ID;
But when I try to run that query in access it asks me for the parameter value of ID. What is the syntax for setting one column in a table to another column in Access?
You can use CStr() to cast the ID number to text. This should work when IDCopy is text and ID is numeric.
UPDATE [table] SET IDCopy = CStr(ID);
I bracketed the table name because table is a reserved word.
If Access still thinks ID is a parameter with this query, then [table] does not include a field named ID.

Update a MySQL table that has only one row with no id column

I have a MySQL table called settings. It has multiple columns, where every column is an item with a single value. So it has only one row and no id column. The design is final (I don't plan to add more columns).
How can I update the value in a single column (change one setting's value)?
What's the problem with using this --> http://dev.mysql.com/doc/refman/5.0/en/update.html
?
UPDATE table1 SET column1 = value
In case you have more than one row, you can add:
WHERE table1.column = matching_value;
making sure the match criteria is only the row you need.
to update a value of certain item column you must specify the row contains the value to be updated, since you don't have a primary key, you can depend on the nature of item values to act as row identifier and i don't recommend that.. the best way is to update your design and add column for the primary key

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

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?
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.
In MySQL, a unique index will ignore any NULL values. So there's a bit of a hack you could use.
You could consider adding a nullable bit column (which can only have the value 1 or 0):
ALTER TABLE mytable
ADD COLUMN `is_default` BIT NULL,
ADD UNIQUE INDEX `is_default_UNIQUE` (`is_default` ASC);
At this point, one row can be 1 (TRUE), and attempting to add another row that is also TRUE, will result in an error:
1062: Duplicate entry '\x01' for key 'is_default_UNIQUE'
The only catch is that all other rows need to be NULL and cannot be false as that would also count as a unique value.
(This really isn't what indexes are meant to be used for though)
a specific column can only take either 'true' or 'false' values.
Use Datatype for column either boolean or tiny-int
2 and exactly only one row should be having 'true' value?
you have to write a query which update all the row columns to false except one you set to true
Set the data-type of the column to "boolean"; then add a trigger-function, that sets the row you want to be always "true" to true on update.