问题
I am designing a database but I come to a struggle at this moment,
Currently I have 3 tables:
- ex_artists
- ex_tracks
- ex_labels
Now I wish to have a Unique ID throughout these 3(or more) tables
So if 'Example Artist' had ID '1', then 'Example Track' can not also have ID '1' but should get ID '2' since '1' already exists
Thank you greatly in advance!
回答1:
I understand your concerns. Once you decide to design your database with technical IDs, there is always the danger of confusing IDs. While
insert into album_track (album, artist, track, no)
values ('B0016991-00', 'JBIEBER', 'BOYFRIEND0001', 2);
instead of
insert into album_track (album, artist, track, no)
values ('B0016991-00', 'BOYFRIEND0001', 'JBIEBER', 2);
would probably through an error,
insert into album_track (album_id, artist_id, track_id, no) values (40, 22, 12, 2);
instead of
insert into album_track (album_id, artist_id, track_id, no) values (40, 12, 22, 2);
would probably not, and the time you notice your program error it may be too late to tell the bad records from the good ones. Your data would be technically consistent, but a mess really.
To overcome this problem, you need one source to pull your IDs from. In Oracle for instance you would use a sequence. In MySQL you can create an ID table for this only purpose:
create table ids(id int auto_increment primary key);
create table album(id int primary key, album_no text, album_name text,
foreign key (id) references ids(id));
create table track(id int primary key, track_name text, record_date date, take int,
foreign key (id) references ids(id));
insert into ids values ();
insert into album (id, album_no, album_name) values
((select last_insert_id), 'B0016991-00', 'Justin Bieber – Believe - Deluxe Edition');
So whenever you insert a record in one of your tables, you must specify an ID (because it is not automatically got). You get the ID with an insert into your IDs table and then call MySQL's LAST_INSERT_ID().
An less safe, but simpler alternative would be to start the IDs at different offsets:
create table album(id int auto_increment primary key, album_no text, album_name text);
create table track(id int auto_increment primary key, track_name text, record_date date);
alter table track auto_increment=10000001;
create table artist(id int auto_increment primary key, artist_name varchar(100));
alter table artist auto_increment=20000001;
insert into artist (artist_name) values ('Justin Bieber');
This works as long as your IDs stay in the desired range.
回答2:
it is not that i didn't read what you wrote :>
but you don't need a unique ID across them all, just unique ways of identifying rows.
consider the following:
-- drop table artist
create table artist
(
artist_id int unsigned auto_increment PRIMARY KEY,
artist_name varchar(255) not null
);
insert into artist (artist_name) values ('U2');
insert into artist (artist_name) values ('Cranberries');
-- drop table label
create table label
(
label_id int unsigned auto_increment PRIMARY KEY,
artist_id int not null, -- will leave FK RI to developer
label_name varchar(255) not null,
release_date datetime not null
);
insert into label(artist_id,label_name,release_date) values (1,'Boy','1980-10-20');
insert into label(artist_id,label_name,release_date) values (2,'No Need to Argue','1994-10-03');
create table track
(
id int unsigned auto_increment PRIMARY KEY, -- not completely necessary, will explain
track_id int not null, -- u number this 1 to n consistent with label layout
label_id int not null, -- will leave FK RI to developer
track_name varchar(255) not null
);
-- Cranberries:
insert track (track_id,label_id,track_name) values (1,2,'Zombie');
-- U2:
insert track (track_id,label_id,track_name) values (1,1,'I Will Follow');
insert track (track_id,label_id,track_name) values (2,1,'Twilight');
-- select * from track
artist and label rather obvious. track does not need the 'id' column but i threw it in anyway. That is, track can be identified as combo of artist/label id's
foreign key (FK) referential integrity is up to you but i could plop it in if you want
来源:https://stackoverflow.com/questions/30547993/generate-unique-ai-id-over-multiple-data-tables