, " " ( , "", " " B, C ..). "" , . , , . , (, , , - ?)
, . , , , , .
-, . , "" ; ImageType ( ) . -. ( CHECK ImageType, , .)
CREATE TABLE Image
(
ImageId int not null
,ImageType char(1) not null
,constraint PK_Image
primary key clustered (ImageId, ImageType)
)
. , .
CREATE TABLE A
(
AId int not null
constraint PK_A
primary key clustered
)
CREATE TABLE B
(
BId int not null
constraint PK_B
primary key clustered
)
, . ( ...)
CREATE TABLE Image_A
(
ImageId int not null
constraint PK_Image_A
primary key clustered
,AId int not null
,ImageType char(1) not null
constraint DF_Image_A
default 'A'
constraint CK_Image_A__ImageType
check (ImageType in ('A'))
,constraint FK_Image_A__A
foreign key (AId) references A (AId)
,constraint FK_Image_A__Image
foreign key (ImageId, ImageType) references Image (ImageId, ImageType)
)
CREATE TABLE Image_B
(
ImageId int not null
constraint PK_Image_B
primary key clustered
,BId int not null
,ImageType char(1) not null
constraint DF_Image_B
default 'B'
constraint CK_Image_B__ImageType
check (ImageType in ('B'))
,constraint FK_Image_B__B
foreign key (BId) references B (BId)
,constraint FK_Image_B__Image
foreign key (ImageId, ImageType) references Image (ImageId, ImageType)
)
INSERT (1, 'A')
INSERT Image values (2, 'A')
INSERT Image values (3, 'B')
INSERT Image values (4, 'B')
INSERT A values (101)
INSERT A values (102)
INSERT B values (201)
INSERT B values (102)
:
SELECT * from A
SELECT * from B
SELECT * from Image
SELECT * from Image_A
SELECT * from Image_B
:
INSERT Image_A (ImageId, AId) values (1, 101)
INSERT Image_A (ImageId, AId) values (1, 102)
INSERT Image_B (ImageId, BId) values (1, 201)
INSERT Image_A (ImageId, AId) values (2, 101)
( )
drop table Image
drop table A
drop table B
drop table Image_A
drop table Image_B
( , / "".)