Mysql set to default? - mysql

Is there a way in mysql I can set to default?
$q = $dbc -> prepare("UPDATE accounts SET thisColumn = thisColumn(DEFAULT) WHERE id = ?");
Is there a function to set to default in mysql?

There is the DEFAULT keyword:
Use the keyword DEFAULT to set a column explicitly to its default value. This makes it easier to write INSERT statements that assign values to all but a few columns, because it enables you to avoid writing an incomplete VALUES list that does not include a value for each column in the table. Otherwise, you would have to write out the list of column names corresponding to each value in the VALUES list.
You can also use DEFAULT(col_name) as a more general form that can be used in expressions to produce a given column's default value.
DEFAULT(col_name) can only be used if a default is explicitly set for that column, but DEFAULT should always work.
Untested, but this should work:
UPDATE accounts SET thisColumn = DEFAULT WHERE id = ?

Related

Updating column with Replace

Using MySQL (MariaDB 10.4.12)
I am trying to update a column using the replace function. I need to replace an ip address with a URL. The value in the column is very long.
I am using:
UPDATE myTable
SET myColumn = REPLACE(myColumn, '192.168.9.1', 'www.mydomain.com/content')
WHERE ID = 1234;
When I run this, the value it updates to is very wrong.
However, if I remove the"/content" from the replace function, it updates correctly.
The data type of the column is longtext:
# Field, Type, Null, Key, Default, Extra
'myColumn', 'longtext', 'NO', '', NULL, ''
The value in this column is metadata, that is a very long string of characters/code. In the middle is my url that I need to update.
Example: a:4:{s:5:"child";a:1:{s:0:"";a:1:{s:3:"rss";a:1:{i:0;a:6:{s:4:"data";s:3:" and on and on and on.
Additionally, if I just select with the REPLACE() function, it returns the correct value.
Is there an issue with having a forward-slash ("/") in the replace function or update statement?
Thanks
-M

How to access an update value from within an update statement in SQL

I am trying to do something like that in a prepared statement:
UPDATE table SET active = ?, last_action = IF(VALUES(active) > 0, NOW(), last_action) WHERE id = ?
So I am trying to access the value I am setting with SET active = ? in the same query.
However VALUES(active) does not seem to represent the value of active = ?
Is there any way to make this work like it would when the values are part of an INSERT INTO [...] VALUES [...] ON DUPLICATE KEY UPDATE ... statement?
Possible solution: add another parameter like this: IF(? > 0, NOW(), last_action) and set it to the same value as the active = ? parameter.
Is there any better solution that does not require me to bind the same value to another parameter?

revert rows to default column value mysql

I am using MySQL community server 5.1. Is there a way to just set all the rows back to their default column values with one query?
I just added a lot of rows. I had default values for most of the columns specified, but for some reason all the columns values were set = 0 instead. I was wondering if there is a query to set all the column values to their default value, and I would need the ability for a where clause too.
something like this for example:
update table set values = values(default) where id > 300;
Thanks!
There is a DEFAULT keyword available in INSERT and UPDATE queries which you can use in place of any real column value:
update my_table set my_column = DEFAULT where id > 300;
As documented in the MySQL Manual
Note that there is also a DEFAULT() function, as mentioned in a previous answer; with which you can select any column's default value for use.
You should try:
UPDATE table_name SET col = DEFAULT(col) WHERE id > 300;

How can I make a field default a value with every new record?

I have an HTML form to insert ($_POST) a new record to MySQL.
I have a field that always needs the value "FC". Do I make this happen in phpMyAdmin or do I insert "FC" at the time when I insert the record via HTML form?
Just perform this query:
ALTER TABLE `tablename` ALTER `fieldname` SET DEFAULT 'FC';
Supposing that you have tablename table and fieldname
MySQL has the option to specify a default value for columns. Edit the column and set default to As defined: and put 'FC' in the field.
For consistency you might also want to insert 'FC' in the form since the default value will not be used if something else is provided.
Or if you really want to limit the value only to 'FC' then why not hardcode it into your query?

concat to text field

I have a table with 1 column "textField". Our version of mysql won't allow for full text fields to have a default value, the default is null.
When we want to update our table we want to append a value to whatever is in textField, like so:
update table set textField = concat( textField ,'value')...this works when there is something already in the field, but won't work if the field is null. Since mysql won't allow for a default value in a full text field the above statement won't work.
A solution is to do 2 queries:
1. check if that field is null
2. if so, then don't do a concat & just update w/ the value...if not null then do the concat
We'd rather not do 2 queries if we can avoid it...is there a one-liner alternative that would work?
Try this:
update table set textField = concat(COALESCE(textField, '') ,'value')...
concat(ifnull(textfield, ''), 'value')