revert rows to default column value mysql - 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;

Related

MySQL update zero columns

Is it possible to update zero columns in a MySQL update query?
I'd like to utilise the automatic updating of the timestamp column when this particular row is selected. Does anyone have any clue?
So you want your update query to only update the timestamp column? Just update it yourself instead of relying on the automatic update:
UPDATE mytable SET tscolumn = NOW() WHERE ...
Just try something like :
UPDATE myTable SET someField = someField WHERE id = myId
So the data values won't move, and the automatic timestamp will be updated.
As said by GaryG, you may also update the timestamp directly.

Mysql set to default?

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 = ?

On/Off query in mysql?

I use an update query in mysql to change the published row in my db to 1 or 0. Is there a way to do this automatically in one query, ie, if the database had a 1 in published it would set it to 0 and if it had a 0 it would set it to 1... just like a switch?
Thanks
Assuming the field's a boolean/bit field, then
UPDATE table SET bitfield = ~bitfield
where ~ is mysql's bit-wise NOT operator.
If the field is INT, then:
UPDATE table
SET field = 1 - field
WHERE ...
UPDATE tbl SET published = !published WHERE ...
You can do this with a conditional query. Something like this for example
UPDATE table SET published = IF(published = 1, 0, 1)

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?

Insert the same fixed value into multiple rows

I've got a table with a column, lets call it table_column that is currently null for all rows of the table. I'd like to insert the value "test" into that column for all rows. Can someone give me the SQL for this?
I've tried INSERT INTO table (table_column) VALUES ("test"); but that only populates that last row. How do I do all of the rows at once?
You're looking for UPDATE not insert.
UPDATE mytable
SET table_column = 'test';
UPDATE will change the values of existing rows (and can include a WHERE to make it only affect specific rows), whereas INSERT is adding a new row (which makes it look like it changed only the last row, but in effect is adding a new row with that value).
This is because in relational database terminology, what you want to do is not called "inserting", but "UPDATING" - you are updating an existing row's field from one value (NULL in your case) to "test"
UPDATE your_table SET table_column = "test"
WHERE table_column = NULL
You don't need the second line if you want to update 100% of rows.
To update the content of existing rows use the UPDATE statement:
UPDATE table_name SET table_column = 'test';
What you're actually doing is adding rows. To update the content of existing rows use the UPDATE statement:
UPDATE table SET table_column = 'test';
UPDATE `table` SET table_column='test';
The SQL you need is:
Update table set table_column = "test";
The SQL you posted creates a new row rather than updating existing rows.
To create a new empty column and fill it with the same value (here 100) for every row (in Toad for Oracle):
ALTER TABLE my_table ADD new_column INT;
UPDATE my_table SET new_column = 100;