How can I have a unique column in many tables

I have ten or more (I don't know) tables that have a column named foo with the same data type.
how can i tell sql that the values ​​in all tables must be unique.
I mean If (I have a value of "1" in table1), I should not have a value of "1" in table2

+3
source share
5 answers

Has a common identifier table, to which these ten tables belong. This will work well because it will provide unique identifiers, but that does not mean that you could not duplicate the identifier in the table if someone really wants to.

I am referring to a common ID table, which ensures that you do not have duplicates to insert (by adding an identifier to this general table), but the point is to ensure that this never happens, by creating a business rule in the system or by posting control restrictions for cross-referencing other tables (which would ensure uniqueness, but would degrade performance).

+4
source

; , , GUID ; ( ), .

, , . , , . , , , . - .

+1

GUID ( UNIQUEIDENTIFIER SQL-). , .

0

, ID% 10 = N ( N - , 0 9). IDENTITY (N, 10) .

0
source

I would suggest that perhaps your design is flawed. Why are these separate tables? It’s better to put them in the same table with one identifier field and another to determine what spearate tables do (e.g. id cusotmer). You can then read about partioning tables if you want them to be shared by the client for performance reasons.

0
source

Source: https://habr.com/ru/post/1736006/


All Articles