How to check if a given data exists in multiple tables (all of which has the same column)?

后端 未结 6 1192
眼角桃花
眼角桃花 2020-12-06 05:48

I have 3 tables, each consisting of a column called username. On the registration part, I need to check that the requested username is new and unique.

I need that si

6条回答
  •  再見小時候
    2020-12-06 06:33

    With your current set up RedFilter's answer should work fine. I thought it would be worth noting that you shouldn't have redundant or dispersed data in your database to begin with though.

    You should have one and only one place to store any specific data - so in your case, instead of having a username in 3 different tables, you should have one table with username and a primary key identifier for those usernames. Your other 3 tables should then foreign-key reference the username table. You'll be able to construct much simpler and more efficient queries with this layout. You're opening a can of worms by replicating data in various locations.

提交回复
热议问题