Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL two tables and creating a link table

I have two tables: Employee (ID, Name, Address) and Store(ID,Address) and I would like to record information about people who work in each store.

I thought of making a new table called Employee_List table. My questions:

1- Employee_List and Employee has one-to-many relation, right?

2- Employee_list to store has one-to-one relation, right?

3- How to define foreign and primary keys for Employee_List table?

enter image description here

like image 831
Hamid Avatar asked Feb 20 '13 11:02

Hamid


People also ask

How do you link two tables in SQL?

The join is done by the JOIN operator. In the FROM clause, the name of the first table ( product ) is followed by a JOIN keyword then by the name of the second table ( category ). This is then followed by the keyword ON and by the condition for joining the rows from the different tables.

How can I link more than two tables in SQL?

In SQL Server, you can join more than two tables in either of two ways: by using a nested JOIN , or by using a WHERE clause. Joins are always done pair-wise.

Which one is required to link two table together?

To combine tables we will use the UNION, UNION ALL, INNER JOIN, LEFT OUTER JOIN and RIGHT OUTER JOIN keywords.

What is a linking table?

Link tables are usually association/bridge tables between different Hub tables in Datavault. They mostly resolve many to many relation between different Hub tables. Example. Link - INVOICE_LINE_ITEM. Hub - INVOICE, PRODUCT etc.

How do you link two tables in a relational database?

Linking tablesTwo tables are linked when they have a matching field, that is, a field in each table containing similar data. For example, suppose you want to link a Suppliers table with another table called Products. Both tables must have at least one field in common.


1 Answers

Employee_list should have:

  • employee_listid (INT PK)
  • employee_id (INT FK)
  • store_id (INT FK)

I would recommend changing the table name to represent the composite table, i.e. EmployeeStores. This would allow your schema to be scalable, employees can work in multiple stores.

In SQL Server:

CREATE TABLE EmployeeStores
(
   EMPLOYEEStoreID   INT IDENTITY,
   EMPLOYEEID INT FOREIGN KEY REFERENCES Employee(employee_id),
   STOREID INT FOREIGN KEY REFERENCES Store(store_id)
)
like image 167
Darren Avatar answered Oct 13 '22 09:10

Darren