简体   繁体   中英

How to update a column which is also a primary key?

There is a name field in the UI which is also the primary key column in the underlying table. There is a requirement to make that field editable in the UI. There should be an ID which should serve as the primary key, but there isn't and now it is not feasible to introduce any ID column.

Is there any alternate design idea which can be used in such a scenario ?

The UI is in Swing and DB is Oracle.

First of all, I don't know, who thinks Name field can be Primary Key . That's the wrong database design ever.

Yes, you better change it to some ID column as Primary Key and that shouldn't be updated in future. Since, you can't have multiple Primary Key . So, you need to perform some circus here.

  • You need to drop existing Primary Key first. Since, you can't have multiple Primary Key in single table.

  • Create your ID column and allow NULL

  • Then, update this column with sequence.

  • Once your ID column gets populated, you need to create Primary Key on this column.

You can only have one primary key, but you can have any number of unique indexes on a table. So let the existing primary key be the immutable primary key and have the application use this key internally for everything. Add another column to the table and create a unique index on it. Let the users modify this other field.

Another alternative would be to declare all child tables with foreign keys ON UPDATE CASCADE. That way, any update to the primary key will cascade to the child tables. Once implemented in production, quit the company and run fast in the other direction and write an article about how you were the first person ever to use ON UPDATE CASCADE in a production setting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM