Can I create a Unique (Index) constraint for multiple tables?

Is there a way to create a unique index for tables in a MySQL database?

The only thing I mean

                table A ids=1,2,5,7... 

                table B ids=3,4,6,8...
+5
source share
2 answers

I think this is not possible by directly creating a constraint. But there are some solutions so that you can get unique identifiers.

One suggestion is to use TRIGGER. Create a BEFORE INSERT trigger for both tables that checks the identifier for presence at INSERTor UPDATE.

Secondly, create a third table containing numbers UNIQUEand other tables: TableAand TableBthey will refer to the third.

+8
source

JW, , , . MySQL , . - ( - ):

CREATE TABLE a
  (
    `id` int,
    `name` varchar(100),
    PRIMARY KEY(`id`)
  )
  ENGINE = INNODB;
CREATE TABLE b
  (
    `id` int,
    `name` varchar(100),
    PRIMARY KEY(`id`)
  )
  ENGINE = INNODB;
CREATE TABLE c
  (
    `id` int auto_increment,
    `intable` varchar(10) not null,
    PRIMARY KEY(`id`)
  )
  ENGINE = INNODB;

, , (, "Joe" a):

INSERT INTO c (`intable`) VALUES ('a');
INSERT INTO a (`id`, `name`)
  SELECT LAST_INSERT_ID() AS id, 'Joe' AS name;

c - , , . c.

+1

All Articles