схема базы

Frutik

1024-й
схема базы

после трудного дня чегото не соображу...

create table device (
id serial,
name varchar(254) unique not null,
primary key (id)
)

create table panel (
id serial,
name varchar(255),
primary key (id)
)

create table port (
panel int4 not null references panel(id),
port int4 not null,
primary key(panel, port)
)

create table layout (
device int4 unique not null references device(id),
panel int4 not null references port(panel),
port int4 not null references port(port),
primary key(device, panel, port)
)


последнюю таблицу не создает...

PostgreSQL said: ERROR: UNIQUE constraint matching given keys for referenced table "port" not found
 

Sad Spirit

мизантроп (Старожил PHPClub)
Команда форума
Re: схема базы

Автор оригинала: Frutik

create table layout (
device int4 unique not null references device(id),
panel int4 not null references port(panel),
port int4 not null references port(port),
primary key(device, panel, port)
)


последнюю таблицу не создает...

PostgreSQL said: ERROR: UNIQUE constraint matching given keys for referenced table "port" not found
У тебя первичный ключ составной. Внешний тоже д.б. составным:
Код:
create table layout (
    device int4 unique not null references device(id),
    panel int4 not null,
    port int4 not null,
    primary key(device, panel, port)
    foreign key (panel, port) references port
)
 

Frutik

1024-й
схема немного изменилась и снова вылезли проблемы

create table device_types (
id varchar(10),
description varchar(254) not null,
primary key (id)
)

insert into devices_type values ('DTE', 'Data Terminal Equipment');
insert into devices_type (id, description) values ('DCE', 'Data Communication Equipment');

create table devices (
id serial,
type varchar(10) references device_types(id),
name varchar(254) unique not null,
primary key (id)
)

create viev dte_devices as select * from devices where type = 'DTE';
create viev dce_devices as select * from devices where type = 'DCE';

create table ports (
device int4 not null references devices(id),
port int4 not null,
primary key(device, port)
)

create table layout (
dce_device int4 not null,
dce_port int4 not null,
device int4 not null,
port int4 not null,
unique(dce_device, dce_port),
unique(device, port),
foreign key (dce_device, dce_port) references ports,
foreign key (device, port) references ports,
check(dce_device in (select id from dce_devices))
)

при создании последней таблици

ERROR: cannot use subselect in CHECK constraint expression

хотя в мануале явного запрета на это я не нашел... это тупик? или я туплю?

можно както по другому наложить ограничение на вставку в layout.dce_device только именно dce девайса?

и + к этому можна организовать както
чтото типа check (dce_device_fk <> device_fk ) тоесть выражаясь терминами задачи "нельзя было подключить порт устройства к самому себе"?
 

Sad Spirit

мизантроп (Старожил PHPClub)
Команда форума
Автор оригинала: Frutik
ERROR: cannot use subselect in CHECK constraint expression

хотя в мануале явного запрета на это я не нашел... это тупик? или я туплю?

можно както по другому наложить ограничение на вставку в layout.dce_device только именно dce девайса?

и + к этому можна организовать както
чтото типа check (dce_device_fk <> device_fk ) тоесть выражаясь терминами задачи "нельзя было подключить порт устройства к самому себе"?
запрет на запросы в CHECK я где-то видел... ссылку не приведу, ибо не помню.

это можно (как мне кажется) реализовать триггером BEFORE INSERT

ну а второй check записать по-моему вообще не проблема
(device <> dce_device) or (port <> dce_port)
 

Frutik

1024-й
угу... пришел тоже к выводу что без тригера не обойтись...

сижу мучусь :)
 

Frutik

1024-й
перед тем как писать функцию для тригера решил поиграться еще с такой схемой:

create table devices (
id serial,
name varchar(254) unique not null,
primary key (id)
);

create table dce_devices (
type varchar(10) not null default 'DCE' check(type = 'DCE')
) inherits (devices);

create table device_ports (
id serial,
device int4 not null references devices(id),
port int4 not null,
unique(device, port),
primary key(id)
);

create table dce_devices_ports () inherits (device_ports);

create table layout (
dce_port int4 not null references dce_devices_ports(id),
port int4 not null references device_ports(id),
unique(dce_port, port),
check(dce_port <> port)
);


снова проблемы при создании последней таблици

ERROR: UNIQUE constraint matching given keys for referenced table "dce_devices_ports" not found

я уже наверное всех достал... но мне я уже похоже приклинило :(
 

Frutik

1024-й
все заработало после того как заменил

create table dce_devices_ports () inherits (device_ports);

на


create table dce_devices_ports (
id serial,
device int4 not null references devices(id),
port int4 not null,
unique(device, port),
primary key(id)
) inherits (device_ports);


правда смысла этого я не понял
 
Сверху