SQL :Database Relationships

#One to One Relationships
- In a one-to-one relationship, a key will appear only once in a related table.

![image](https://qiita-image-store.s3.amazonaws.com/0/93548/a9c03f41-6009-ebcc-8c67-e82b06d444db.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/d243d2d7-aba6-aef7-3376-da1200fc7435.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/70bfb491-20d0-fad0-4399-cc7c1e30980c.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/8f571fbc-af85-03a4-4748-633c4902afc5.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/9f25f533-77b2-fea5-b062-ca271c0f2abc.png)

#One to Many and Many to One Relationships
- In a one-to-many relationship, keys from one table will appear multiple times in a related table.

![image](https://qiita-image-store.s3.amazonaws.com/0/93548/2fe82bbf-24f5-8548-8ee1-8837226780c6.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/5f6780c8-5e77-b391-53b7-0fd5d59ecfdc.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/3f7fb57d-dfa2-0700-b700-ffc087d6680e.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/7eef1fe6-aa43-9946-4bbd-2d854e62b495.png)

#Many to Many Relationships
- In some cases, you may need multiple instances on both sides of the relationship. 

![image](https://qiita-image-store.s3.amazonaws.com/0/93548/f06f4161-cdf2-480e-cf3a-1bd0c3aed7ba.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/8c857724-1a71-0d85-2aca-f8e8b0d627a6.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/8cf8c86d-c200-634a-9f29-ab592e97346c.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/1cb0d3cf-f5c2-7382-3aaf-4c0bab3cf07f.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/67bddeac-431c-5869-912f-18907bd61679.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/00dd4828-7923-443a-bc9c-2fdcb5d954e1.png)

#Self Referencing Relationships
- Customers can refer other customers to your shopping website.

![image](https://qiita-image-store.s3.amazonaws.com/0/93548/d1d9f4b4-2bfc-7ee2-ddc7-f3096bb46430.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/7c65a6b8-bc1a-761b-638d-1dea8fffacac.png)

#Foreign Keys
- we always had these "****_id" fields that referenced a column in another table. 

![image](https://qiita-image-store.s3.amazonaws.com/0/93548/4c2a3336-9d35-986b-183a-2d8d9fca2bcb.png)

- Defining the Foreign Key Explicitly

![image](https://qiita-image-store.s3.amazonaws.com/0/93548/33b057eb-7261-1957-95d3-7c522f540baf.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/4bf6c1fb-e543-c35f-cfb5-74b9c7e48e67.png)
   *Without Explicit Declaration*
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/2f761752-0159-85c8-d74c-b01d3ceabec8.png)


- Visualizing the Relationships in MySQL Workbench

![image](https://qiita-image-store.s3.amazonaws.com/0/93548/821e8515-b0b2-9ae9-16d2-edd419562e12.png)

- [Detail](http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561)

- shortcuts
<img width="224" alt="Screen Shot 2015-11-04 at 16.30.47.png" src="https://qiita-image-store.s3.amazonaws.com/0/93548/63cdd693-5c39-4140-70a6-c719c8f5067d.png">
![relationshipSymbols.png](https://qiita-image-store.s3.amazonaws.com/0/93548/2fcf217c-4633-fe39-ef5b-caae87891a46.png)

- Non-identifying relationship
*A non-identifying relationship means that a child is related to parent but it can be identified by its own.*
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/38cbcf39-2e4e-19c0-e583-5931c62ed667.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/9da4b5c6-de4d-1392-ae4d-09cca682562b.png)


- Identifying relationship
*An identifying relationship means that the parent is needed to give identity to child. The child solely exists because of parent.*
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/030048af-17d7-5e7f-2c87-54b1bde769ce.png)
![image](https://qiita-image-store.s3.amazonaws.com/0/93548/649d6a18-95f6-eb02-0f95-bb94b10a7100.png)