1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
create table cat
(
id int identity not null primary key,
code char(3) not null unique,
name char(20) not null
);
create table scat
(
id int identity not null primary key,
cat_code char(3) not null references cat(code),
scat_code char(3) not null,
unique (cat_code, scat_code),
name char(20) not null
);
create table article
(
id int identity not null primary key,
cat_code char(3) not null references cat(code),
scat_code char(3) null,
constraint fk_scat foreign key (cat_code, scat_code) references scat(cat_code, scat_code),
name char(30) not null
);
insert into cat (code, name) values ('001', 'Catégorie 1'), ('002', 'Catégorie 2'), ('003', 'Catégorie 3');
insert into scat (cat_code, scat_code, name) values ('001', '011', 'SCatégorie 1.1'), ('001', '012', 'SCatégorie 1.2'), ('001', '013', 'SCatégorie 1.3'),
('002', '021', 'SCatégorie 2.1'), ('002', '022', 'SCatégorie 2.2'), ('002', '023', 'SCatégorie 2.3'),
('003', '031', 'SCatégorie 3.1'), ('003', '032', 'SCatégorie 3.2'), ('003', '033', 'SCatégorie 3.3'),
('003', '001', 'SCatégorie 3.1bis')
insert into article (cat_code, scat_code, name) values ('001', null, 'Article 1 cat 1 sans scat'), ('002', null, 'Article 2 cat 2 sans scat'); -- OK
insert into article (cat_code, scat_code, name) values ('001', '012', 'Article 3 cat 1 scat 1.2'), ('002', '023', 'Article 4 cat 2 scat 3.2'); -- OK
insert into article (cat_code, scat_code, name) values ('003', '012', 'Article 5 cat 3 scat 1.2'); -- KO!!!
insert into article (cat_code, scat_code, name) values ('003', '001', 'Article 6 cat 3 scat 3.1bis'); -- OK |
Partager