How to alter “REFERENCES” in PostgreSQL?

Internal dependencies between tables and / or other objects are never bound to the object name. Internally, every object is stored in a catalog table and the OID (internal primary key) of the object is used for everything else. Accordingly, a FOREIGN KEY reference is stored in the catalog tables pg_constraint (the constraint itself incl. … Read more

Rename column only if exists

Please read this article on codingvila.com for a detailed explanation. Rename Column Only If Exists in PostgreSQL DO $$ BEGIN IF EXISTS(SELECT * FROM information_schema.columns WHERE table_name=”your_table” and column_name=”your_column”) THEN ALTER TABLE “public”.”your_table” RENAME COLUMN “your_column” TO “your_new_column”; END IF; END $$;

Hive Alter table change Column Name

Change Column Name/Type/Position/Comment: ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name] Example: CREATE TABLE test_change (a int, b int, c int); // will change column a’s name to a1 ALTER TABLE test_change CHANGE a a1 INT;

Add primary key to PostgreSQL table only if it does not exist

You could do something like the following, however it is better to include it in the create table as a_horse_with_no_name suggests. if NOT exists (select constraint_name from information_schema.table_constraints where table_name=”table_name” and constraint_type=”PRIMARY KEY”) then ALTER TABLE table_name ADD PRIMARY KEY (id); end if;

how to modify the size of a column

Regardless of what error Oracle SQL Developer may indicate in the syntax highlighting, actually running your alter statement exactly the way you originally had it works perfectly: ALTER TABLE TEST_PROJECT2 MODIFY proj_name VARCHAR2(300); You only need to add parenthesis if you need to alter more than one column at once, such as: ALTER TABLE TEST_PROJECT2 … Read more

Does adding a null column to a postgres table cause a lock?

The different sorts of locks and when they’re used are mentioned in the doc in Table-level Locks. For instance, Postgres 11’s ALTER TABLE may acquire a SHARE UPDATE EXCLUSIVE, SHARE ROW EXCLUSIVE, or ACCESS EXCLUSIVE lock. Postgres 9.1 through 9.3 claimed to support two of the above three but actually forced Access Exclusive for all … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)