Home
| java.io.IOException: No properties file: users.properties or defaults: defaultUsers.properties found »
PostreSQL: How create unique primary key with values after a table is already populated with data
Here’s how you can create a primary key with unique values, if say you need one for an existing table when using it with Java Persistence API.
1. Create a new field. I’ve named it ‘primary’ in this case.
2. Then create a new sequence. Here it’s called ‘primaryseq’.
3. Execute the following query:
UPDATE
public.tablename
SET
“primary” = NEXTVAL(‘primaryseq’)
WHERE “primary” IS NULL
;
Another solution you might want to take a look at.
- Primary Keys in PostgreSQL shouldn’t be a Mystery « Alex Hochberger pingbacked on 2 years, 9 months ago