By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,789 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,789 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.