A
A
Artem00712017-01-23 14:43:55
MySQL
Artem0071, 2017-01-23 14:43:55

Is it possible to set a unique field in two tables?

Good afternoon!
There are 2 tables:

  1. table1
    • param1_1
    • param1_2

  2. table2
    • param2_1
    • param2_2


It is necessary to make sure that param1_1 and param2_1 are unique.
Is it possible to arrange this without checking 2 tables each time?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ilya, 2017-01-23
@rpsv

Slightly strange structure.
If they must be unique, then make one of the columns a FOREIGN KEY and make both columns UNIQUE.
Or make such a structure so that both columns are in the same table and make the index UNIQUE composite.

E
Egorithm, 2017-01-23
@EgoRusMarch

If you want a unique key for 2 tables (which is rather strange), then you can simply put a constraint on the primary key. For example like this:

CREATE TABLE `table_1` (
`id` INT CHECK(id BETWEEN 10000 AND 49999)
);
CREATE TABLE `table_2` (
`id` INT CHECK(id BETWEEN 50000 AND 99999)
);

L
leremin, 2017-01-23
@leremin

But in PostgreSQL, this is easy to do through sequences. And so everyone is right, it is better to redo the base itself. She is clearly not good.

L
lega, 2017-01-23
@lega

"External" counter either in one table even in another odd

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question