Here are my db table design. in my expense table has two foreign key which is connected to project table and category table. I want to show category name in my project Model . how can i make a relation between this tables???
Note: Only expense table have foreign key[pro_id from project and cat_id from expense_category]
expense Table
Related
So as you know in data modeling, the many-to-many relationships are handled by creating a bridge table. This will enable us to have foreign key constraints.
My question relating to query data from tables that have many-to-many relationships.
I will give an example. Let's say we have the following tables
Table 1
Column 1
Column 2
Column 3
Table 2
Column 1
Column 4
Column 5
Table 3 (bridge table)
Column 1
Ok, so when I tried to query data from table 1 and left joining with table 2 I got the same results as query data from table left joining with table 3 and left joining with table 2. And that makes me wonder if the bridge table is a necessary step to include in a SQL query!
Thank you in advance :)
Your bridge table has just one column, which suggests that you are confusing one-to-many relationships (or many one-to-one), and many-to-many relationships.
In a one-to-many relationship, the child table refers directly to the parent table, and there is no need for a bridge table:
create table orders (
order_id int primary key,
order_date date,
...
);
create table order_items (
order_item_id int primary key,
order_id int references orders(order_id),
...
)
In a many-to-many relationship, the bridge table has (at least) two columns, one for each of the referential table that come into play in the relation:
create table authors (
author_id int primary key,
name varchar(50),
...
);
create table books (
book_id int primary key,
title varchar(50),
...
);
create table book_authors (
author_id int references authors(author_id),
book_id int references books(book_id),
primary key (author_id, book_id)
)
Let's make your example less abstract. There are posts with dislikes and likes. Table 1 = likes, Table 2 = dislikes.
Table likes (post_id, who, why)
Table dislikes (post_id, who, why)
The bridge table is then
Table posts (post_id, content)
This is an example where two child tables are related by a parent table. You don't create the bridge table in order to get an m:n relation between the two child tables, but because the child tables make no sense without their parent table. The child tables are m:n related, but we don't usually call this an m:n relation, because the tables are only losely related (a like belongs to a post and a dislike belongs to a post, but the like doesn't really belong to the dislike).
When talking about m:n relations, we are usally not talking about parent/child relationship.
An example: An order can contain many products and a product can be in many orders.
Table orders (order_id, date)
Table products (product_id, name, price)
The bridge table is the position in the order:
Table order_detail (order_id, product_id, amount)
There is no parent/child relation between orders and products, but the two are related, because products get ordered. This is what we typically call an m:n relation. The bridge table establishes the relation between order and product.
Hello I have 2 entities that are CUSTOMER and PRODUCT at my Entity Relation Diagram(ER).
CUSTOMER and PRODUCT has a M to N relationship which is RATE and this relationship has 2 attributes which are Comment and Rate.
My PRODUCT entity has a derived attribute named Rating-avg which is the average rating of the product, being rated by the CUSTOMER's.
I don't know and can't find how to add the derived attribute to the table while creating it or altering it.
I would be really glad if someone could help.
I am using SQLite3(3.25.2) and SQLiteStudio(3.2.1) (The latest versions up to date.).
You would use a third table, which is called a "junction" or "association" table:
create table CustomerProducts (
CustomerProductId int primary key,
CustomerId int references customers(CustomerId),
ProductId int products(productId),
Rate ?, -- unclear what the type is
Comment text
);
You could name the table Rate, if you like. I typically name association tables after the two tables involved in the relationship, unless it is an entity itself.
Suppose I have a table patients and a table tests. Patients can take many tests and tests are given to many patients. so it's a many to many relationship. I create a junction table between them. I also want to store the date the patient wants to take the tests so I do that in the junction table. Also, suppose a patient takes more than 1 test on a single day and I want to create a specific billing account against all the test on that date.
I create an autogenerated billing no. I add that to the junction table as well.
So far, my junction table record the tests a patient may have taken on a date and the billing no.
Now I create a table billingaccounts. In this table I want to store all the billing information against 1 billing No. such as totalamount, paid amount and so on. I also want to include the billing no here, since it will help me identify the patient and tests the billing account field refers to.
I was trying to make the billing no in this billing account table as a foreign key to the billing no in the previous junction table.
But I can't do that since the billing no in the junction table cannot be kept unique since it's an m:n junction table.
What can I do to make this work or any other alternatives?
Just add a primary key column to the "junction" table. Your junction table is richer than simply matching two tables. Because you want a foreign key relationship, it is worthy of its own primary key. For example (using MySQL syntax):
create table PatientTests (
PatientTestId int primary key auto_increment,
PatientId int not null,
TestId int not null,
TestDate date,
BillingAccount int,
. . .
constraint fk_patienttests_patientid references Patients(PatientId),
. . .
);
create table
Make the billing no in the junction table a foreign key to the primary key of the billingaccounts table. So, you would create a new row in billingacounts first, including an autogenerated id/billing no, then use that number in the junction table.
On a SaS project (Software as a Service), imagine this scenario:
The main table for customers, those who use the application, is tenant and it primary key is the column id.
I have 3 tables to manage data from tenants and one specific table to manage a functionality of vacancies:
Period, Unity and Shift all those with PK id and FK tenant_id.
My table vacancy have 3 Foreign Keys
period_id - References to "Period" table
unity_id - References to "Unity" table
shift_id - References to "Shift" table
Now the problem:
I need to guarantee that all these 3 FK's are reference to rows on Period, Unity and Shift tables that belong to the same Customer on the tenant table.
I was clear in my explanation?
Maybe i can create a Trigger to handle those validations. I really don't know if SGBD already have resources to do with that.
For information: I'm using SQL Server with Eloquent ORM. But, if really exists a solution for this, the best would be one that who works everywhere.
1) Ensure that your tenant_id is defined as NOT NULL on the tables Period, Unity and Shift.
2) Create the following unique keys on that tables:
Period(tenant_id, period_id)
Unity(tenant_id, unity_id)
Shift(tenant_id, shift_id)
3) Define column tenant_id for table vacancy as NOT NULL.
4) Define the folowing FKs on table vacancy:
(tenant_id, period_id) referencing Period(tenant_id, period_id)
(tenant_id, unity_id) referencing Unity(tenant_id, unity_id)
(tenant_id, shift_id) referencing Shift(tenant_id, shift_id)
This way you can guarantee that every linked row has the same tenant_id.
I am creating a new SQL Server 2008 database. I have two two tables that are related.
The first table looks like this:
BRANDS // table name
BrandID // pk
BrandName // varchar
The second table looks like this:
MODELS // table name
ModelID // pk
ModelDescription // varchar
Every brand will have at least one model and every model will belong to only one brand.
The question is, should I create a junction table like this
BRANDS_MODELS // table name
RecordID // pk
BrandID
ModelID
Or should I modify the MODELS table to include the BrandID like this
MODELS // table name
BrandID //
ModelID // pk
ModelDescription // varchar
Thanks!
If a model belongs to only one brand then you can put the FK to brand on the model table (your second approach). The first way, with the junction table, is for a many-to-many relation.
Based on what you've said so far, I would leave out the junction table and use an ordinary foreign key in the MODELS table.
But if a model could move brands and you needed to maintain a current junction and history, a junction table has advantages over keeping history of the entire MODELS row when just a foreign key changes. Also if other things exist which might be associated with the relationship "entity" more than the MODEL entity it might make more sense to have a junction table. You can always make a unique constraint on ModelID in the junction table to ensure that the same model is not linked to multiple brands. So although a junction table is required to effectively implement a many-to-many relationship, it can also be useful for one-to-many relationships where that relationship itself has attributes.
Junction tables are used for many-to-many relationships which does not seem to be a good fit here.
For example, you would not want to enable the creation of a Honda Civic and a Toyota Civic. That's an example of car's make/model relationship but should fit your brand/model relationship.