简体   繁体   中英

SQL AUTO_INCREMENT PRIMARY KEY only for UNIQUE name field

So i made an auto incrementing primary key for my family table of the presidents of America, now some presidents are listed multiple times because they got remarried (fe Tyler J) but that results in Tyler J having a 10 and 11 as pres_id. So how do i make the primary key auto_increment only for unique name fields? link to picture of the table: http://puu.sh/fN6KD/e7c5e03c7a.png

And now that i've created the pres_id column allready can i adjust it?

Thanks in advance guys.

The fix to this is normalization.

Since a president can have multiple spouses, those marriage details should be moved to a separate table, and they'd reference the president via the pres_id . If you repeat the pres_id in your current table based on name, it wouldn't be a Primary Key anymore.

Your tables could be:

tbl_POTUS (Pres_ID,Pres_Name,Birth_Yr,Death_Yr)
tbl_FLOTUS (Spouse_ID,Pres_ID,Spouse_Name,Mar_Year,Nr_Children)

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