I have a database (postgresql) with 10 schema and every schema has 10 tables. For some reasons, some tables do not have a primary key.
Is it possible to create a function/trigger that can look for all tables and
- add a column
id_pk serial(if the column does not exist) - and make it a primary key (if the table has not any primary key)?
tables. http://www.postgresql.org/docs/9.1/static/infoschema-tables.html Use it to get a list of tables. Another view, called table_constraints will give you a list of all constraints for the tables. Using these you will be able to build a dynamic SQL statements to generate keys – cha Jun 26 '14 at 00:48EXECUTEwith theformatfunction to generate an appropriateALTER TABLE ... ADD PRIMARY KEY ...for each. – Craig Ringer Jun 26 '14 at 01:50