简体   繁体   中英

Mysql Unique constraints across 2 columns

I just started using sqlalchemy so my question below might not make sense but bear with me. I have two columns in my table colA and colB. I want elements of colA and colB to be unique across both the columns. For example if,

     ColA  ColB
row1 1     2 
row2 3     2 X(2 is already present in row1 ColB, therefore is a duplicate)
row3 4     1 X(1 is already present in row1 ColA, therefore is a duplicate)

then row2 and row3 in the example above must not be allowed. How do I do this in sqlalchemy? I have already researched about this and looked at other similar question but haven't found a suitable answer yet.

Use your DBMS to create a two-column unique index.

ALTER TABLE mytable ADD UNIQUE INDEX myindex (colA, colB);

If I understand your question correctly, this will constrain your table the way you want.

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