Alter DB2 table with existent records with a new not null default '' column - db2

what will happen with existent records with this new column, will they have default '' ?
or this is only applying for new records? is this a safe way? Appreciate your help, regards.
SQL code:
ALTER TABLE mytable ADD COLUMN newColumn VARCHAR(10) NOT NULL DEFAULT '';

Once you run below command -
ALTER TABLE mytable ADD COLUMN newColumn VARCHAR(10) NOT NULL DEFAULT '';
All the existing rows in the table will have the value as a single space in column newColumn, and for newly inserted rows, You can specify the value in insert statement.

Related

Add a constraint with a where statement

I want to add a NOT NULL to a column that is conditional on another column.
ALTER TABLE mailers ALTER COLUMN original_id SET NOT NULL
WHERE offer_type = 'client cross'
In other words, if one column have 'client cross' do not let a different column be NULL.
You cannot create a NOT NULL column constraint with a condition. Either the column can contain NULL values or it can't.
However, you can create a normal CHECK constraint for the entire row:
ALTER TABLE mailers
ADD CONSTRAINT "non-null_original_id_for_client-cross"
CHECK (original_id IS NOT NULL OR offer_type <> 'client cross');

Add timestamp column with default NOW() for new rows only

I have a table that has thousands of rows. Since the table wasn't constructed with created_at column initially, there is no way of getting their creation timestamp. It is crucial though to start getting the timestamps for future rows.
Is there a way I can add a timestamp column with default value NOW() so that it won't populate the values to previous rows but only for the future ones?
If I do the ALTER query, it populates all rows with timestamp:
ALTER TABLE mytable ADD COLUMN created_at TIMESTAMP DEFAULT NOW()
You need to add the column with a default of null, then alter the column to have default now().
ALTER TABLE mytable ADD COLUMN created_at TIMESTAMP;
ALTER TABLE mytable ALTER COLUMN created_at SET DEFAULT now();
You could add the default rule with the alter table,
ALTER TABLE mytable ADD COLUMN created_at TIMESTAMP DEFAULT NOW()
then immediately set to null all the current existing rows:
UPDATE mytable SET created_at = NULL
Then from this point on the DEFAULT will take effect.
For example, I will create a table called users as below and give a column named date a default value NOW()
create table users_parent (
user_id varchar(50),
full_name varchar(240),
login_id_1 varchar(50),
date timestamp NOT NULL DEFAULT NOW()
);
Thanks
minor optimization.
select pg_typeof(now()); --returns: timestamp with time zone. So now include timezone.
So better with timestamptz.
begin;
ALTER TABLE mytable ADD COLUMN created_at TIMESTAMPTZ;
ALTER TABLE mytable ALTER COLUMN created_at SET DEFAULT now();
commit;
Try something like:-
ALTER TABLE table_name ADD CONSTRAINT [DF_table_name_Created]
DEFAULT (getdate()) FOR [created_at];
replacing table_name with the name of your table.

Alter column set not null fails

Consider the following table with approximately 10M rows
CREATE TABLE user
(
id bigint NOT NULL,
...
CONSTRAINT user_pk PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
)
Then i applied the following alter
ALTER TABLE USER ADD COLUMN BUSINESS_ID VARCHAR2(50);
--OK
UPDATE USER SET BUSINESS_ID = ID; //~1500 sec
--OK
ALTER TABLE USER ALTER COLUMN BUSINESS_ID SET NOT NULL;
ERROR: column "business_id" contains null values
SQL state: 23502
This is very strange since id column (which has been copied to business_id column) can't contain null values since it is the primary key, but to be sure i check it
select count(*) from USER where BUSINESS_ID is null
--0 records
I suspect that this is a bug, just wondering if i am missing something trivial
The only logical explanation would be a concurrent INSERT.
(Using tbl instead of the reserved word user as table name.)
ALTER TABLE tbl ADD COLUMN BUSINESS_ID VARCHAR2(50);
--OK
UPDATE tbl SET BUSINESS_ID = ID; //~1500 sec
--OK
-- concurrent INSERT HERE !!!
ALTER TABLE tbl ALTER COLUMN BUSINESS_ID SET NOT NULL;</code></pre>
To prevent this, use instead:
ALTER TABLE tbl
ADD COLUMN BUSINESS_ID VARCHAR(50) DEFAULT ''; -- or whatever is appropriate
...
You may end up with a default value in some rows. You might want to check.
Or run everything as transaction block:
BEGIN;
-- LOCK tbl; -- not needed
ALTER ...
UPDATE ...
ALTER ...
COMMIT;
You might take an exclusive lock to be sure, but ALTER TABLE .. ADD COLUMN takes an ACCESS EXCLUSIVE lock anyway. (Which is only released at the end of the transaction, like all locks.)
Maybe it wants a default value? Postgresql docs on ALTER:
To add a column, use a command like this:
ALTER TABLE products ADD COLUMN description text;
The new column is initially filled with whatever default value is given (null if you don't specify a DEFAULT clause).
So,
ALTER TABLE USER ALTER COLUMN BUSINESS_ID SET DEFAULT="",
ALTER COLUMN BUSINESS_ID SET NOT NULL;
You cannot do that at the same transaction. Add your column and update it. Then in a separate transaction set the not null constraint.

PostgreSQL - ALTER column data type from integer to integer array

Kindly help to modify a column of type integer to integer array:
I had created a table with a column content_id of type integer. then I tried to change the content_id(integer) to integer[](integer array) but its showing error as displayed:
TestDatabase=# ALTER TABLE tbl_handset_content ALTER COLUMN content_id TYPE integer[];
ERROR: column "content_id" cannot be cast to type "pg_catalog.int4[]"
Regards,
Sravan
Try this (column test_id is of type INTEGER before alter takes place). PostgreSQL 8.4.
ALTER TABLE test.test_id
ALTER COLUMN test_id TYPE INTEGER[]
USING array[test_id]::INTEGER[];
This worked better for me!
ALTER TABLE schema.table
ALTER COLUMN column
DROP DEFAULT;
ALTER TABLE schema.table
ALTER COLUMN column TYPE INTEGER[]
USING array[column]::INTEGER[];
ALTER TABLE schema.table
ALTER COLUMN column SET DEFAULT '{}';

Postgres: Reduce varchar size and truncate

I currently have a Postgres 8.4 database that contains a varchar(10000) column. I'd like to change this into a varchar(255) and truncate any data that happens to be too long. How can I do this?
Something like ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(255) USING SUBSTR(c, 1, 255)
1) Update the column data using a substring method to truncate it
update t set col = substring(col from 1 for 255)
2) Then alter the table column
alter table t alter column col type varchar(255)
Docs here http://www.postgresql.org/docs/8.4/static/sql-altertable.html
BEGIN;
UPDATE table SET column = CAST(column as varchar(255));
ALTER TABLE table ALTER COLUMN column TYPE varchar(255); --not sure on this line. my memory is a bit sketchy
COMMIT;