I have two tables and I need to establish a 1-many relation among them, as an example:
1 Customer can have many Order(s).
What is a good way to create keys on the Order table such that there can be many rows in Orders, relating to one/same Customer details? i.e can I have cases when there are 2 rows with same CustomerID inserted into Order (1-many relation on CustomerID foreign key)
Assume
Customer table has columns:
CustomerID (key)
Name
OtherColumns
Order:
<IsaKeyNeeded>
customerID (foreign key)
OrderName
Another question I have is does 'Order' need to have it's own key?
You have it set up correctly ... the Order table should have a foreign key to the Customer table. This establishes the relationship of one customer to many orders. Just do not make the CustomerID a unique key.
To answer your other question ... yes, the Order table should have it's own primary key.
Related
I have two tables, one is Persons, and the other is Genders, hence I want to put more than two genders, M, F, Other.
Persons
id INT (pk, ai)
fk(gender_id)
Genders
id INT (pk, ai)
title VARCHAR(255)
What would be the relationship type between Genders and Persons table? Is the relationship between them should be non-identifying or identifying (gender_id has to be primary key too)?
Thanks in advance.
Midori
P.S: (Sorry for grammar mistakes.)
An identifying relationship would be if the gender_id in the Persons table were part of that table's primary key. In other words, if the gender needed to be part of the way to uniquely identify rows in the Persons table.
In the example you show, the gender_id is merely an attribute of the Persons table. It is not used to identify rows. Therefore it's not an identifying relationship.
I'm starting with MySQL database relashionships and I have a question.
I'm going to put in an exemple:
Table A = customer
Table B = products
Table C = sales
I wanna know how can I make a relationship with these tables where "sales" has only one customer and multiple produts.
The "sales" table can't have a Primary Key with both foreign keys, cuz it will have multiple products.
I could create a ´idsale´, but how I would structure the table to receive multiple products?
Thank you.
Costumers and Sales have a ONE-TO-MANY relation. You could add a customer reference on the Sales table
Table sales
- id PK
- customers_id FK
Sales and Product have a MANY-TO-MANY relation. You will need a new table to map this relation
Table sales_has_products
- sales_id FK
- products_id FK
I would recommend creating a fourth table, let's call it productSale, that has its own primary key, as well as foreign keys the sale ID and the product ID. Sale, then, will just have an ID and the foreign key of the customer ID (as well as any additional info you want, of course.)
This way, a sale can include multiple items of the same product (because all productSale rows have a unique ID), and all of the items included in a sale can be found by querying productSale by the specific sale ID.
I have a CUSTOMERS table and a CONTACTS table the relation between them is one to many obviously.
also I have PROJECTS table and PROJECT_CUSTOMERS table with relation one to many and with relation one to one between CUSTOMERS and PROJECT_CUSTOMERS.
my problem is that I have a fifth table PROJECT_CONTACTS ....I can't figure which tables shall I refer to in this tables, currently I am refering to PROJECT_CUSTOMERS and CONTACTS table, is this correct or there is something better ?
Your title refers to "foreign keys" but your question just seems to be about what columns should go in what tables.
First, decide what situations can arise and what you want/need to say about them. (This will your tables, columns, candidate keys, foreign keys and constraints.)
Every table holds rows that make some predicate (statement template parameterized by column names) true. Your design seems to have:
CUSTOMERS(id, ...) -- ID identifies a customer and ...
CONTACTS(id, ...) -- ID identifies a contact and ...
PROJECTS(id, ...) -- ID identifies a project and ...
PROJECT_CUSTOMERS(pid, cust_id, ...) -- project PID has customer CUST_ID and ...
PROJECT_CONTACTS(pid, cont_id, cust_id)...)
-- project PID has contact CONT_ID and project pid has customer CUST_ID and ...
A foreign key has a table & column list referencing a table and column list that forms a candidate key. It says that lists of values in the first table appear as lists of values in the second table. Where that is so, declare a foreign key.
I have a problem in logical design of a SQL Server database.
Still I can not distinct which relation has to be one-to-many and which one has to be many-to-many.
Someone told me if both entity tables are independent, they can have a many-to-many relation, else they will have one-to-many.
But now I am working on a project that collects personal information of the employees, in one part there is a table known as JobStatus which is for the personnel's current job. This table has a relationship with Person (table) that is many-to-many, of course there is a junction table between them.
I made this type of relation because one job position's name is assigned to several persons and with different performance.
For instance :
Person A ----->Operator
Person B------>Operator and so on...
And in other side there are some cases that a person has two Job position, I mean he is either a director and a teacher .
For instance :
Person C ------>Director & Teacher
So would you please guide me in this ambiguous logical mean?
Based on the project you described, I would create three tables: employeeTable, jobType, and jobAssignment. Give each employee a unique id (a primary key) and give each job a unique id (primary key) and let the jobAssignment table be the glue that links the employeeTable with the jobAssignment table. The jobAssignment table would have an index on the employeeID and jobID.
jobAssignment
---------------
employeeID (indexed)
jobID (indexed)
employeeTable
---------------
employeeID (primary key)
employeeName
jobType
---------------
jobID (primary key)
jobName
jobDescription
That way, you can keep track of the employees and their respective jobs in the jobAssignment table no matter how many job descriptions are assigned to each employee.
Simply put, you would recognize a many to many when either table can not have the PK from the other table as its foreign key
Taking a Student and Course table
Student can take many courses
Courses can belong to more than one Students
Putting a FK of course (CourseID) on student will restrict the Student to ONE course
Putting a FK of student (StudentID) on course will restrict the course to ONE student
To solve this, a third table StudentCourse will have the StudentID and the CourseID, therefore making either table independent.
That is Many to Many.
For one to many, that happens when you can easily put the ID of one table as the FK of the other.
In your case, Two Employees can be Operator at the same time and an Employee can be an Operator and Teacher - that design is MANY to MANY. You are right
i am building an invoicing application consisting of following business logic.
a) Place a new order for customer. (an order is a group of three
related components, estimate, invoice and purchaseorder)
b) After placing an order a new estimate can be generated. an order
will have only one estimate.(An estimate consist of item details)
c) With reference to an estimate of the order. an invoice can be
generated. an invoice qualify for the discount of price. apart from
item details, an invoice consist of some expenses.an order can contain
only one invoice
d) with reference to an invoice of the order, PurchaseOrder can be
generated. PurchaseOrder consist of item information about vendor
purchase. an order can contain multiple PurchaseOrder.
here is the database table design i have come up with.
while all look good, i am having difficulty deciding where to store the items list that belongs to particular estimate, invoice or purchaseorder of the order.
i had thought of several solution.
Approach A : create different tables for item list for each. (estimate, invoice and purchase order)
table : estimate_item , invoice_item , purchaseorder_item.(this tables contains columns similar to that of
order_item in above image).
problem: problem with this approach is all the three tables consist of identical columns storing identical information, the only
difference is foreign key that will be stored.
Approach B: create one item list table order_item
tablename: order_item
problem: not sure what to store as foreign key in this table since the foreign key can be from three different table. i thought of few
way of handling foreign keys in this table as follows.
1)foreignKey table reference column: type (example values: estimate, invoice, purchaseorder) foreignKey column: type_id(consist
of foreignKey of any of three tables) problem: i am using naming
convention for column names for example column name ending with tablename_id defines the foreign key. and this method violate the rules.
2) foreignKeyColumn: order_id , estimate_id , invoice_id ,
purchaseorder_id. problem: unnecessary foreign key columns
defined.
i want to know how should i store the foreign key in order_item table so that it identifies the order and estimate/invoice/purchaseorder it belongs too.
the relationship for tables are:
id is the primary key for all the tables
table name: order relates to (contact, estimate, invoice, shipment) tables.
column name: contact_id (foreign key(referring to id column of the contact table)).
column name: estimate_id (foreign key(referring to id column of the estimate table)).
column name: invoice_id (foreign key(referring to id column of the invoice table)).
column name: shipment_id (foreign key(referring to id column of the shipment table)).
tablename: purchaseorder (this have one to many relationship with order table)
column name: order_id (foreign key(referring to id column of the order table)).
column name: contact_id (foreign key(referring to id column of the contact table)).
the question is about how to go with the storing of foreign keys in order_item table.
Thank you.
Update 1:
Please note that each table estimate , invoice and purchaseorder will have item of it's own and having no relation to each other.
Hi I'm not sure how the relations happen. for instance, you have 'estimate' pointing to 'order item' but I don't see what key you have to make that join (or look-up). as another 'order' points to 'estimate' but how are those two joined? I dont see any shared attributes that both those entities have.
I'm assuming 'id' is just something to make rows in each particular table unique, but are not id's that have value to the application. so, I would think you' need to carry estimate.reference number into the 'order item' table. This is just a cursory comment.
also, would hep for clarity if keys were listed first. so in 'order item' you have attribute 'order id' (which appears to be an FK) buried in the end of the list of other attributes. makes this hard to read.
If I understand you correctly, each document associated with an order (i.e. an estimate, purchaseorder and/or invoice) might contain a different list of items.
If that is the case, I would probably create a Documents table along the following lines:
CREATE TABLE Documents (
DocumentId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
OrderId INT NOT NULL,
-- you can move any fields common to all document types here
-- e.g. date created, reference #, etc.
FOREIGN KEY (OrderId) REFERENCES order (id)
);
And then your order_item, estimate, purchaseorder and invoice tables all reference their associated entry in this table:
ALTER TABLE [tablename]
ADD COLUMN DocumentId INT NOT NULL,
ADD FOREIGN KEY (DocumentId) REFERENCES Documents (DocumentId)
);
Is this what you're after?