Insert/Update column with default values - linq-to-sql

In order to insert a row which has a default value I've set in the linq-to-sql dbml the auto generate values to true, but this setting generates an error if I try to update the same column. The error is: A member that is computed or generated by the database cannot be changed.I've tried all sorts of combinations in order to make it work. Like setting the auto generated values to false and auto sync to OnInsert. But none of them worked. Is there any way to make both, insert and update, work?

Related

SQL: Set default value to NULL for all columns without default value

The webhosting I use has enabled StrictMode for the Databases. All my php scripts now stopped working because they report I haven't defined a default value for some columns.
As I have a lot of columns in a lot of tables, is there a way to set all the columns with "default value = none" with "default value = NULL" ?
In this way it won't report me the error anymore.
Of course, If there's another (better) way, I am available for it.
I tried looking on the net, but I couldn't find anything suitable for this case.
you can alter column
ALTER TABLE table_name
MODIFY COLUMN col datatype DEFAULT null
A general approach here which should work for each column causing an error would be to set a default value, and then maybe do an update to backfill records missing a value.
ALTER TABLE yourTable ALTER some_text_column SET DEFAULT 'None';
And here is the update:
UPDATE yourTable SET some_text_column = 'None' WHERE some_text_column IS NULL;
You are not required to do this update, but it might make sense to bring older records missing values in line with what newer records would look like.

tell mysql to ignore updates/inserts for auto increment values

Is there a way to tell mysql to ignore any tentative to assign any value to an auto-increment column?. We are using slick and it seems to try to assign the value of 0 to any auto-increment column at the time a new row is created. These auto-increment columns are not primary keys.
With a before insert trigger, you should be able to do something like SET NEW.id = NULLIF(NEW.id, 0);. Of course, this means nothing can ever insert 0 into the field without dropping the trigger first. (Though update queries still can still set it to 0).

MySQL Add Column with Online DDL

I'm currently trying to add a column to a table of ~25m rows. I need to have near-0 down time, so was hoping to use online DDL. It runs for a while, but eventually runs into the issue:
"Duplicate entry '1234' for key 'PRIMARY'"
[SQL: u'ALTER TABLE my_table ADD COLUMN my_coumn BOOL NOT NULL DEFAULT false']
I think this is happening because I'm running INSERT ... ON DUPLICATE KEY UPDATE ... operations against the table while running the operation. This seems to be a known limitation.
After this didn't work, I tried using the Percona pt-online-schema-change tool, but unfortunately, because my table has generated columns, that didn't work either with error:
The value specified for generated column 'my_generated_column' in table '_my_table_new' is not allowed.
So, I'm now at a loss. What are my other options for adding a column without blocking DML operations?
Your Alter statement is creating a non nullable column with a default of false. I'd suspect this to place an exclusive lock on your table, attempt to create the column, then setting it to False across each row.
If you don't have any available downtime, I'd suggest you
Add the column as nullable and with no default
ALTER TABLE my_table ADD COLUMN my_coumn BOOL NULL;
Update the values for existing rows to false
update my_table set my_coumn=false;
Alter the table a second time to be not nullable and with a default.
ALTER TABLE my_table modify my_coumn BOOL NOT NULL DEFAULT false;
Alternatively you could use something like Percona which manages schema changes using triggers and is meant to offer the ability to update schemas without locking the table.
Either option I'd suggest you test in your development environment with some process writing to the table to simulate user activity.

updating a column to its default on mysql

My web application receives an update form for a db record, submitted by the user.
I would like to create an sql which will update all the values to exactly what the user submitted, except those which the user left blank. For those, I want the value to be set to its DB default.
Is there a way to do it?
I'm looking for something like
update my_table set col1=17, col2=DEFAULT, col3='some text'
Please notice that I'm updating an existing row, thus I cannot just live some columns out from the update sql, as they might have had a value before which needs to be erased now.
There is a DEFAULT(col_name) function:
Returns the default value for a table column. An error results if the
column has no default value.
UPDATE my_table SET col1=17, col2=DEFAULT(col2), col3='some text'
Well apparently it's just that simple, exactly as I wrote:
update my_table set col1=17, col2=DEFAULT, col3='some text'
http://dev.mysql.com/doc/refman/5.0/en/update.html

How to alter MySQL table without losing data?

In my application, I make some changes and upload them to a testing server. Because I have no access to the server database I run ALTER commands to make changes on it.
Using a method I ran the following command on server:
ALTER TABLE `blahblahtable` ADD COLUMN `newcolumn` INT(12) NOT NULL
After that, I found that the all the data of the table has been removed. Now the table is blank.
So I need to alter the table without removing his data. Is there any way to do that?
Your question is quite obvious. You're adding a new column to the table, and setting it to NOT NULL.
To make things clearer, I will explain the reaction of the server when you run the command:
You add a new column, so every row of the table has to set a value for that column.
As you don't declare any default value, all the rows set null for this new column.
The server notices that the rows of the table have a null value on a column that doesn't allow nulls. This is illegal.
To solve the conflict, the invalid rows are deleted.
There are some good fixes for this issue:
Set a default value (recommended) for the column you're creating.
Create the column without the NOT NULL, set the appropiate values, and then make the column NOT NULL.
You can create a temp table, pass all the information from the table you want to alter, and then return the info to the altered table.