Posts

Showing posts with the label Database Design

Many-to-many Relationship

Image
Today, at work, our team were designing a screen for collecting some data. I don't think I explained very well how would we implement a many-to-many relationship between 2 entities. I believe I don't understand a subject well enough until I can help other understand how it works. The motivation for this post is helping me solidify my knowledge and help other people. Example: We have a database that holds information about books. A book can have one or many authors, and an author can write one or many books. Let's take a look at the design. Notice that the middle table is the bridge table. The bridge table has 2 foreign keys that reference to the Books and Authors table. Basically, we establish an one-to-many relationship between the Books and the bridge table, and an one-to-many relationship between the Authors and the bridge table. Below is the SQL code for create these 3 tables. CREATE TABLE dbo.Books ( BookId INT NOT NULL PRIMARY KEY, BookTitle VARCH...