Need query to ALTER more rows in one column without WHERE constrain - pymysql

I am looking for a query which I can use in pymysql to UPDATE the rows in one column without the WHERE constraint.
This is the query I used but it replaces all the NULL into 1s:
UPDATE simplefocusstatistic_studentanswers
SET Player_id = null WHERE simplefocusstatistic_id = 1

Related

How to update a column by adding 1000 for each records in a table in Oracle?

I have a column name in a table named 'Test' which contains almost 100k rows, I want to update existing values of 'Test' column by adding 1000. For example if a record contains existing value 5555 , it should get updated with 5555+1000 = 6555. In same way, how can i update for all rows in a table?
update "Test" set "Test"="Test"+1000

How to replace NULL values in one column to 0 (of a very large table) without creating a new column of the desired results added to the table in HIVE?

I am trying to replace all of the NULL values to 0 in a column of a big table in HIVE.
However, every time I try to implement some code I end up generating a new column to the table. The column I am trying to change/modify still exists and still has the NULL values but the new column that is automatically generated (i.e. _c1) is what I want the column I am trying to modify, to look like.
I tried to run a COALESCE but that also ended up generating a new column. I also tried to implement a CASE WHEN, but the same results ensued.
Select *,
CASE WHEN columnname IS NULL THEN 0
ELSE columnname
END
from tablename;
Also tried
SELECT coalesce(columnname, CAST(0 AS BIGINT)) FROM tablename
I would just like to update the table with the other columns being as is but the column I want to modify still has its original name but instead of NULL values it has 0's that replaced them.
I don't want to generate a new column but modify an existing one.
How should I do that?
Use insert overwrite .. option.
insert overwrite table tablename
select c1,c2,...,coalesce(columnname,0) as columnname
from tablename
Note that you have to specify all the other column names required in select.

adding boolean field with default value on a large postgres table

We have a very large table (1 Million records) in some cases and we need to add boolean fields to it which have default values.
If we add only column it takes 3 minutes and we add 3 columns in the same statement it takes same time.
$ALTER TABLE Job ADD COLUMN test BOOLEAN NOT NULL default false;
ALTER TABLE
Time: 186506.603 ms
$ALTER TABLE Job ADD COLUMN test BOOLEAN NOT NULL default false ,
ADD COLUMN test1 BOOLEAN NOT NULL default false,
ADD COLUMN test2 BOOLEAN NOT NULL default false;
ALTER TABLE
Time: 179055.546 ms
We are on Postgres 9.1 . Is there a postgres feature allows to add multiple boolean fields with default values in one shot? This is for a database change management /upgrade solution . Is it better than using temp table to copy and insert to add multiple boolean fields with default values to a table ? The temp table approach is described in this blog:
http://blog.codacy.com/2015/05/14/how-to-update-large-tables-in-postgresql/
You've already shown the best (simple) way - a compound ALTER TABLE statement that adds them all at once.
To do it without a long lock, you have to do it in multiple steps. Add the column as nullable without the default. Add the default but leave it nullable. UPDATE all existing rows to add the new value, preferably in batches. Then finally alter the table to add the not null constraint.

date Not null , error ora_01758

why I am getting this error ?
In the table DDL I only have 2 columns , id (number) and name (varchar)
ALTER TABLE mytable ADD SUSPEND date NOT NULL
ORA-01758: table must be empty to add mandatory (NOT NULL) column
ORA-06512: at line 7
ORA-01758: table must be empty to add mandatory (NOT NULL) column ORA-06512: at line 7
And is your table empty? I think not.
There's probably a way around this involving adding the column as nullable, then populating every row with a non-NULL value, the altering the column to be not null.
Alternatively, since the problem is that these current rows will be given NULL as a default value, and the column is not allowed to be NULL, you can also get around it with a default value. From the Oracle docs:
However, a column with a NOT NULL constraint can be added to an existing table if you give a default value; otherwise, an exception is thrown when the ALTER TABLE statement is executed.
Here is a fiddle, how you could do it
Would a date in the future be acceptable as a temporary default? If so, this would work:
ALTER TABLE MYTABLE ADD (SUSPEND_DATE DATE DEFAULT(TO_DATE('21000101', 'YYYYMMDD'))
CONSTRAINT SUSPEND_DATE_NOT_NULL NOT NULL);
If table already contain the records then table won't allowes to add "Not null" column.
If you need same then set default value for the column or truncate the table then try.

Oracle Update and Return a Value

I am having a Update Statement on a large volume table.
It updates only one row at a time.
Update MyTable
Set Col1 = Value
where primary key filters
With this update statement gets executed I also want a value in return to avoid a Select Query on a same table to save resources.
What will be my syntax to achieve this?
You can use the RETURNING keyword.
Update MyTable
Set Col1 = Value
where primary key filters
returning column1,column2...
into variable1,variable2...

Resources