Why do I need to use foreign key if I can use WHERE?

前端 未结 10 1403
难免孤独
难免孤独 2020-12-23 16:34

A beginners\' question about foreign key in MySQL.

In w3school it says,

A FOREIGN KEY in one table points to a PRIMARY KEY in another table.<

10条回答
  •  一向
    一向 (楼主)
    2020-12-23 17:13

    First of all. Good Question !!

    MySql is an RDBMS - Relational DBMS, so all the entities (tables) are related by an column.

    EMPLOYEE - EMPID EMPNAME DEPTID

    DEPARTMENT - DEPTID DEPTNAME

    DEPTID is foriegn key in the EMPLOYEE table and primary key in the DEPARTMENT table.

    This relation is imaginary relation of objects just an consideration or kind of designing for structuring data in a easy way to retrieve in future. NOT A PHYSICAL RELATION (because its a programming language)

    In order to retrive that data, we need few syntax and described by the Creator of SQL.

    SELECT * from EMPLOYEE

    SELECT * FROM DEPARTMENT

    SELECT * FROM EMPLOYEE WHERE DEPTID = 5

    Here we have realted the two tables imaginary for our convinent, but for the required result we used this syntax WHERE DEPTID = 5.

提交回复
热议问题