DROP NOT NULL CONSTRAINT - postgresql

How to drop not null constraint that will work both in PostgreSql and HSQL?
I'm using: ALTER TABLE tablename ALTER COLUMN columnname DROP NOT NULL;
But it's not working in HSQL.
This column was created: columnname TEXT NOT NULL,

The current HSQLDB syntax is: ALTER TABLE tablename ALTER COLUMN columnname SET NULL But the PostgreSQL syntax will be supported in HSQLDB version 2.3.4 release.

Related

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

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.

ALTER TABLE ON UPDATE CURREN_TIMESTAMP in postgresql

ALTER TABLE tabelName ADD COLUMN IF NOT EXISTS columnName TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
I am using this query to create column on update current_timestamp
It is throwing error ERROR: syntax error at or near "ON"
Thank you all in advance!
There is no ON UPDATE clause in ALTER TABLE ... ADD COLUMN ... statement. Try instead:
ALTER TABLE tableName ADD COLUMN IF NOT EXISTS columnName TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

Postgresql alter table column type to unique not null

ALTER TABLE users ALTER COLUMN email VARCHAR(50) UNIQUE NOT NULL;
ERROR: syntax error at or near "VARCHAR"
LINE 1: ALTER TABLE users ALTER COLUMN email VARCHAR(50) UNIQUE NOT ...
I want to alter column email to add its type as UNIQUE NOT NULL in Postgresql and get this error. Can you explain to me what's wrong?
You cannot create 2 constraints with one single statement. And you have to use PostgreSQL syntax.
alter table users alter column email set not null;
alter table users add constraint email_unique unique (email);

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;