77

In database design what do n:m and 1:n mean?

Does it have anything to do with keys or relationships?

ForguesR
  • 3,558
  • 1
  • 17
  • 39
Abdullah Khan
  • 2,384
  • 2
  • 22
  • 32

9 Answers9

109

m:n is used to denote a many-to-many relationship (m objects on the other side related to n on the other) while 1:n refers to a one-to-many relationship (1 object on the other side related to n on the other).

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
  • 18
    Ah ok ok , so "m" and "n" are taken as variables I see :D, I thought "m" stood for "many" and for that reason the "n" made confusion as to what is stands for (can't stand for "none" I mean). Anyway thanks :D – Abdullah Khan Aug 03 '10 at 14:23
  • 25
    FYI, since no one has mentioned it, the Comp Sci term for this relationship is called "cardinality" see http://en.wikipedia.org/wiki/Cardinality_%28data_modeling%29 for details. – Jason Tan Aug 03 '10 at 14:34
  • The wikipedia link doesn't even use letters 'n' or 'm' and we got a discussion why it's called `n:m` not `n:n`. As also all other answers I find it that natural I can't explain. Answer https://stackoverflow.com/a/59149318/1184842 argues about ordinal vs cardinal, but also without sources. Anybody got a source why 'm' at all? – jan Jan 09 '23 at 09:09
  • I found it in the german version of the Chen ERD notation: https://de.wikipedia.org/wiki/Chen-Notation , but it's not mentioned in the english version. – jan Jan 09 '23 at 09:25
63

1:n means 'one-to-many'; you have two tables, and each row of table A may be referenced by any number of rows in table B, but each row in table B can only reference one row in table A (or none at all).

n:m (or n:n) means 'many-to-many'; each row in table A can reference many rows in table B, and each row in table B can reference many rows in table A.

A 1:n relationship is typically modelled using a simple foreign key - one column in table A references a similar column in table B, typically the primary key. Since the primary key uniquely identifies exactly one row, this row can be referenced by many rows in table A, but each row in table A can only reference one row in table B.

A n:m relationship cannot be done this way; a common solution is to use a link table that contains two foreign key columns, one for each table it links. For each reference between table A and table B, one row is inserted into the link table, containing the IDs of the corresponding rows.

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
tdammers
  • 20,353
  • 1
  • 39
  • 56
  • "link table" also known as a "join table" – Matt Ball Dec 06 '10 at 21:10
  • "None at all" -> wouldn't that be a 0/1:n relationship?(rare) My understanding of 1:n is that it must have one. Like "A city must be in one country, but countries can have n cities", "A Company can have n employees, but an Employee must work for one company", ... – Max Binnewies Jul 28 '17 at 05:52
  • this is what drives me crazy. Link Table, Join table. but you JOIN tables. you also have tuple, row, attributes. I mean it's like database design was never specified fully, and allows for multiple words. Also some words are highly outdated, and cause confusion. Like Domain Integrity. Why isn't it attribute Integrity. Or Column Integrity. The word domain is so vague, and used in other areas. And all of this basically means input validation, which is a term in cyber security. AHHH – Robert Cotterman Jan 21 '19 at 21:16
11

n:m --> if you dont know both n and m it is simply many to many and it is represented by a bridge table between 2 other tables like

   -- This table will hold our phone calls.
CREATE TABLE dbo.PhoneCalls
(
   ID INT IDENTITY(1, 1) NOT NULL,
   CallTime DATETIME NOT NULL DEFAULT GETDATE(),
   CallerPhoneNumber CHAR(10) NOT NULL
)

-- This table will hold our "tickets" (or cases).
CREATE TABLE dbo.Tickets
(
   ID INT IDENTITY(1, 1) NOT NULL,
   CreatedTime DATETIME NOT NULL DEFAULT GETDATE(),
   Subject VARCHAR(250) NOT NULL,
   Notes VARCHAR(8000) NOT NULL,
   Completed BIT NOT NULL DEFAULT 0
)

this is the bridge table for implementing Mapping between 2 tables

CREATE TABLE dbo.PhoneCalls_Tickets
(
   PhoneCallID INT NOT NULL,
   TicketID INT NOT NULL
)

One to Many (1:n) is simply one table which has a column as primary key and another table which has this column as a foreign key relationship

Kind of like Product and Product Category where one product Category can have Many products

Ash
  • 2,531
  • 2
  • 29
  • 38
6

In a relational database all types of relationships are represented in the same way: as relations. The candidate key(s) of each relation (and possibly other constraints as well) determine what kind of relationship is being represented. 1:n and m:n are two kinds of binary relationship:

C {Employee*,Company}
B {Book*,Author*}

In each case * designates the key attribute(s). {Book,Author} is a compound key.

C is a relation where each employee works for only one company but each company may have many employees (1:n): B is a relation where a book can have many authors and an author may write many books (m:n):

Notice that the key constraints ensure that each employee can only be associated with one company whereas any combination of books and authors is permitted.

Other kinds of relationship are possible as well: n-ary (having more than two components); fixed cardinality (m:n where m and n are fixed constants or ranges); directional; and so on. William Kent in his book "Data and Reality" identifies at least 432 kinds - and that's just for binary relationships. In practice, the binary relationships 1:n and m:n are very common and are usually singled out as specially important in designing and understanding data models.

nvogel
  • 24,981
  • 1
  • 44
  • 82
5

To explain the two concepts by example, imagine you have an order entry system for a bookstore. The mapping of orders to items is many to many (n:m) because each order can have multiple items, and each item can be ordered by multiple orders. On the other hand, a lookup between customers and order is one to many (1:n) because a customer can place more than one order, but an order is never for more than one customer.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
2

What does the letter 'N' on a relationship line in an Entity Relationship diagram mean? Any number

M:N

M - ordinality - describes the minimum (ordinal vs mandatory)

N - cardinality - describes the miximum

1:N (n=0,1,2,3...) one to zero or more

M:N (m and n=0,1,2,3...) zero or more to zero or more (many to many)

1:1 one to one

Find more here: https://www.smartdraw.com/entity-relationship-diagram/

unpluggeDloop
  • 109
  • 1
  • 5
  • The benefit of this answer is that it explicitly states that the "n" in "1:n" can actually be 0 or higher. Calling n "many" makes it less obvious that 0 and 1 can also be included. – Sebastian Nov 17 '20 at 19:01
1

m:n refers to a many to many relationship whereas 1:n means one to many relationship.

For example:

employee(id,name,skillset)

skillset(id,skillname,qualifications)

in this case the one employee can have many skills and ignoring other cases you can say that it's a 1:N relationship

Jasperan
  • 2,154
  • 1
  • 16
  • 40
Matovu Ronald
  • 782
  • 11
  • 13
0

Many to Many (n:m) One to Many (1:n)

Adrian Regan
  • 2,240
  • 13
  • 11
0

Imagine you have have a Book model and a Page model,

1:N means:
One book can have **many** pages. One page can only be in **one** book.


N:N means:
One book can have **many** pages. And one page can be in **many** books.
Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46