How to model m:n with 1 optional default value? - sql

Below are two schema which I believe achieve the same result, i.e. m:n relationship with the option for a default/preferred value. Is there any reason to use one over the other?
Schema #1
CREATE TABLE people (
id serial,
primary key (id)
);
CREATE TABLE names (
id serial,
first_name text not null,
last_name text not null,
primary key (id)
);
CREATE TABLE person_has_name (
person_id integer not null references people (id),
name_id integer not null references names (id),
is_default boolean not null default false,
primary key (person_id, name_id)
);
Schema #2
CREATE TABLE people (
id serial,
default_name_id integer references names (id),
primary key (id)
);
-- this table has not changed
CREATE TABLE names (
id serial,
first_name text not null,
last_name text not null,
primary key (id)
);
CREATE TABLE person_has_name (
person_id integer not null references people (id),
name_id integer not null references names (id),
primary key (person_id, name_id)
);

Let's analyze:
Second schema prevent to you to set more than one name as default, but don't prevent to you to set as default name some not related name.
Opposite happens with first schema.
I propose to you (to your zombie user) a third schema that solves math problems:
CREATE TABLE people (
id serial,
default_name_id integer ,
primary key (id),
constraint default_person_name_fk --<--HERE
foreign key (id, default_name_id)
references person_has_name (person_id, name_id)
);
-- this table has not changed
CREATE TABLE names (
id serial,
first_name text not null,
last_name text not null,
primary key (id)
);
CREATE TABLE person_has_name (
person_id integer not null references people (id),
name_id integer not null references names (id),
primary key (person_id, name_id)
);
Regards and sorry about delay, I'm lag ;)

With the second schema you can't add people without first knowing their name. As long as this is ok (i.e. you know that whenever a person is added, they have to reveal their name), then I'd say that either of these schemas are fine, and that schema #2 might even be more optimized in the scenario where you need to query only for the default name.

I would use none of the two.
Solution 1 does not prevent you from having 2 default names for a person.
Solution 2 does not prevent a person having a default name that is not his. You could change the Foreign Key from default_name_id references names(id) to (person_id, default_name_id) references person_has_name(person_id, name_id) but then you'd have circular references and that's another problem, hard to solve.
You could use this, similar to your 2nd solution:
CREATE TABLE people (
id serial,
--- no default_name_id as foreign key here
primary key (id)
);
CREATE TABLE names (
id serial,
first_name text not null,
last_name text not null,
primary key (id)
);
CREATE TABLE person_has_name (
person_id integer not null references people (id),
name_id integer not null references names (id),
primary key (person_id, name_id)
);
With this extra table, for those persons that have a default name:
CREATE TABLE person_default_name (
person_id integer not null,
name_id integer not null,
primary key (person_id),
foreign key (person_id, name_id)
references person_has_name (person_id, name_id)
);

Related

References with PostgreSQL

I have this table:
CREATE TABLE cars_info.cars
(
id SERIAL,
owner_id INTEGER,
brand VARCHAR(50) NOT NULL,
model VARCHAR(50) NOT NULL,
color VARCHAR(50) NOT NULL,
register_number VARCHAR(50) NOT NULL,
created DATE NOT NULL,
PRIMARY KEY(id, brand, model, color, register_number, created),
CONSTRAINT fk_owner_id
FOREIGN KEY(owner_id)
REFERENCES persons_info.persons(id)
);
But when I tried create another table like this:
CREATE TABLE cars_info.violations
(
id SERIAL PRIMARY KEY,
car_id INTEGER NOT NULL,
message VARCHAR(100) NOT NULL,
active BOOLEAN NOT NULL,
CONSTRAINT fk_car_id
FOREIGN KEY(car_id)
REFERENCES cars_info.cars(id)
);
I get an error about that
Target external table "cars" does not have a unique constraint corresponding to the given keys
How can I fix that? I'm a beginner in SQL and don't know how to go about googling that
Your primary key definition for cars
PRIMARY KEY(id, brand, model, color, register_number, created)
makes no sense: The id column, being serial, is itself unique and it alone should be the primary key.
Delete your primary key definition and change the id column definition to:
id serial not null primary key
Unrelated, but best practice is to name table in the singular; name your tables car and violation rather than cars and violations

Design sql tables with list of foreign keys

I want to create an application for rotating pairs in a team every day. I need to store this in the database. Requirments are:
A team should be assigned to one ore more members.
Each team can have multiple tabs and different members allocate in them.(If team consist of 4 members for the particular tab only 3 should be part of it)
Each tab will have a pair of members or list of pairs per day stored.
I have ended up designing something like the example below:
create table if not exists team (
id serial not null primary key,
name text not null
);
create table if not exists member (
id serial not null primary key,
team_id integer references team(id),
nickname text
);
create table if not exists team_tab (
id bigserial not null primary key,
team_id integer references team(id) on delete cascade,
name text not null,
member_ids integer[],
);
create table if not exists team_tab_pairs (
id bigserial not null primary key,
team_tab_id integer not null references team_tab(id) on delete cascade,
tab_date date not null,
pair_ids integer[][],
);
I need an advice and suggestions how could I achieve this without having a list of references ids stored in the 2 tables below.
You need an extra table to design an M:N relationship. This is the case, for example, between "team tab" and "member". In addition to both main entities:
create table member (
id serial not null primary key,
team_id integer references team(id),
nickname text
);
create table team_tab (
id bigserial not null primary key,
team_id integer references team(id) on delete cascade,
name text not null
);
...you'll need to create a table to represent the M:N relationship, as in:
create table team_tab_member (
team_tab_id bigint not null,
member_id int not null,
primary key (team_tab_id, member_id) -- optional depending on the model
);

SQL Constraint names

If we have query for creating table like this..
create table if not exists food
(
id int not null auto_increment,
user_id int,
name varchar(30),
constraint pk_food primary key(id,name),
foreign key(user_id) references userss(id)
);
What does pk_food mean in this example? I know this is a constraint name, but for what we should be give a name for constraint, if its working without?
create table if not exists food
(
id int not null auto_increment,
user_id int,
name varchar(30),
primary key (id, name),
foreign key (user_id) references userss(id)
);
I mean.. how to use these names and for what we need it?
You gives constraints names for basically two reasons:
You can better understand the error message when the constraint is violated.
You can more easily find the constraint if you want to delete it.

Foreign key in the first table

I have a question about foreign keys.
How does it work when I want to add a foreign key to the first table that I make that references to the primary key of the second table I create?
CREATE TABLE table1
(
name_id INT NOT NULL,
team TEXT REFERENCES table2(team_id),
PRIMARY KEY(name_id)
);
CREATE TABLE table2
(
team_id INT NOT NULL,
teamname TEXT,
PRIMARY KEY(team_id)
);
If I try the code above I get the following error:
ERROR: relation "" does not exist
Thanks in advance.
Either create the second table first. Or use alter table. That is, create the first table without the reference and then do:
alter table table1 add constraint fk_table1_team
foreign key (team_id) REFERENCES table2(team_id);
The declaration for table1 would be:
CREATE TABLE table1 (
name_id INT NOT NULL,
team_id INT,
PRIMARY KEY(name_id)
);
The reference between the tables should be on the primary key and certainly not on a character column, if an integer is available.
here's the syntax of creating a table with Foreign key:
CREATE TABLE table11
(
name_id INT NOT NULL,
team INT,
PRIMARY KEY(name_id),
foreign key(team) references table22(team_id)
);
CREATE TABLE table22
(
team_id INT NOT NULL,
teamname TEXT,
PRIMARY KEY(team_id)
);
but there was another problem. a foreign key from a child table cannot reference to a primary key from a parent folder if they do not contain the same type. in your code team was of TEXT and team_id was of INT which cannot be.

Foreign keys referring other foreign keys in PostgreSQL

In PostgreSQL I have a database, which I intend to make the following table declaration:
CREATE TABLE canvas_user (
id INTEGER,
login_id VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(355) UNIQUE NOT NULL,
name_given VARCHAR(30),
name_family VARCHAR(30),
name_full VARCHAR(50),
role canvas_role,
last_login TIMESTAMP,
PRIMARY KEY (id)
);
CREATE TABLE problem (
id SERIAL,
title VARCHAR(50),
author VARCHAR(50),
path TEXT,
compiler VARCHAR(20),
PRIMARY KEY (id)
);
CREATE TABLE assignment (
id INTEGER,
title TEXT NOT NULL,
points_possible INTEGER NOT NULL,
problem_id INTEGER,
PRIMARY KEY (id),
FOREIGN KEY (problem_id) REFERENCES problem(id)
);
CREATE TABLE submission (
num SERIAL,
user_id INTEGER,
assignment_id INTEGER,
timestamp TIMESTAMP,
path TEXT,
lti_info TEXT[],
PRIMARY KEY(num, user_id, assignment_id),
FOREIGN KEY (user_id) REFERENCES canvas_user(id),
FOREIGN KEY (assignment_id) REFERENCES assignment(id)
);
CREATE TABLE correction (
num INTEGER,
user_id INTEGER,
assignment_id INTEGER,
timestamp TIMESTAMP,
path TEXT,
execution_time interval,
PRIMARY KEY(num, user_id, assignment_id),
FOREIGN KEY (num) REFERENCES submission(num),
FOREIGN KEY (user_id) REFERENCES submission(user_id),
FOREIGN KEY (assignment_id) REFERENCES submission(assignment_id)
);
Everything works fine, except for the following error at the creation of the last table (correction):
ERROR: there is no unique constraint matching given keys for
referenced table "submission"
What I intend with the correction table is to have an unique correction for each submission but a submission can have (or not) a correction.
How can I solve this error? Is it a problem of design or just a table declaration mistake?
A foreign key constraint does not care whether the referenced column(s) is referencing another column itself. But the referenced column(s) must be unique. That's what the error message tells you (quite clearly).
What you are missing is that a foreign key constraint can be based on multiple columns. This should work:
FOREIGN KEY (num, user_id, assignment_id) REFERENCES submission
Replacing:
FOREIGN KEY (num) REFERENCES submission(num),
FOREIGN KEY (user_id) REFERENCES submission(user_id),
FOREIGN KEY (assignment_id) REFERENCES submission(assignment_id)
The short form of the syntax (REFERENCES submission) is possible, because you are referencing the primary key, which is the default.
Plus, you can simplify: make submission.num the sinlge-column primary key, drop the redundant columns user_id and assignment_id from correction and reduce the fk constraint to just (num) - as discussed in #Tim's answer.
As long as you have the multicolumn fk constraint, consider NOT NULL constraints on each of the referencing columns (as commented by #joop). Else, one or more NULL values in the referencing columns allow to escape the fk constraint with the default MATCH SIMPLE behaviour. This may or may not be intended, typically it is not.
Alternatively consider MATCH FULL for multicolumn fk constraints to only allow that if all referencing columns are NULL. Details:
MATCH FULL vs MATCH SIMPLE
Make the foreign key from the correction table to the submission table a compound key that is unique.
Also, review the design of the submission table; num is a serial type and should be the unique primary key. You can add a unique constraint to the columns num, user_id, and assignment_id
CREATE TABLE canvas_user (
id INTEGER,
login_id VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(355) UNIQUE NOT NULL,
name_given VARCHAR(30),
name_family VARCHAR(30),
name_full VARCHAR(50),
role canvas_role,
last_login TIMESTAMP,
PRIMARY KEY (id)
);
CREATE TABLE problem (
id SERIAL,
title VARCHAR(50),
author VARCHAR(50),
path TEXT,
compiler VARCHAR(20),
PRIMARY KEY (id)
);
CREATE TABLE assignment (
id INTEGER,
title TEXT NOT NULL,
points_possible INTEGER NOT NULL,
problem_id INTEGER,
PRIMARY KEY (id),
FOREIGN KEY (problem_id) REFERENCES problem(id)
);
CREATE TABLE submission (
num SERIAL,
user_id INTEGER,
assignment_id INTEGER,
timestamp TIMESTAMP,
path TEXT,
lti_info TEXT[],
PRIMARY KEY(num, user_id, assignment_id),
FOREIGN KEY (user_id) REFERENCES canvas_user(id),
FOREIGN KEY (assignment_id) REFERENCES assignment(id)
);
CREATE TABLE correction (
num INTEGER,
user_id INTEGER,
assignment_id INTEGER,
timestamp TIMESTAMP,
path TEXT,
execution_time interval,
PRIMARY KEY(num, user_id, assignment_id),
FOREIGN KEY (num, user_id,assignment_id ) REFERENCES submission(num, user_id, assignment_id)
);