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;
Related
select (now();
just like so.. but it doesn't works:
ALTER TABLE old_name RENAME TO new_name || select now();
You can try to use RENAME and SET DEFAULT
ALTER TABLE T
RENAME old_name TO new_name;
ALTER TABLE T
ALTER COLUMN new_name
SET DEFAULT now();
sqlfiddle
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.
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.
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 '{}';
I want to drop 200 columns in my table in PostgreSQL. I tried:
ALTER TABLE my_table
DROP COLUMN col1, col2
But I get an error like this:
ERROR: syntax error at or near "col2"
As per the docs, you can do this:
ALTER TABLE table DROP COLUMN col1, DROP COLUMN col2;
(You may need to wrap some of your column names in " quotes if they happen to be keywords.)
Thanks long & Ondrej - below commands worked for me,
ALTER TABLE table_name DROP COLUMN column_name_1,DROP COLUMN column_name_2,DROP COLUMN column_name_3;
ALTER TABLE table_name DROP column_name_1,DROP column_name_2,DROP column_name_3;
This worked for me:
alter table your_table_name drop column your_column_name;