Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

one to one and many to many => one to (one and many)

0 views
Skip to first unread message

carlosc...@yahoo.com

unread,
Jun 14, 2004, 11:44:46 PM6/14/04
to
I have these tables. There are many languages, and many countries.
Every country has many (some subset of) languages.

create table countries (
country_id serial primary key,
country_name varchar(128) unique not null
);

create table languages (
language_id serial primary key,
language_name varchar(128) unique not null
);

create table countries_languages (
country_id int references countries,
language_id int references languages,
primary boolean not null default false,
unique(country_id, language_id)

);

Politics clouding reality as it always does, each country must be
assigned a primary language. That's the primary column in
countries_languages. This column doesn't exist yet, but is the best
way I can think of to solve the problem.

For each country there should be no more than one row in
countries_languages for which primary is true.

How do I enforce it in the database? Is there a clean and portable way
to do it? Or at least a clean postgresql-only way to do it?

I don't want to make a separate one-to-(zero or one) table, because
all the important stuff will still work with multiple languages per
country. If there is a separate countries_primarylanguage table, it
must be guaranteed consistent with countries_language.

0 new messages