简体   繁体   中英

Join query without where clause

The two queries below output the same result. I understand the first one, but can someone explain how the 2nd query works the same.

SELECT p0_.name AS name_0
FROM patient p0_ INNER JOIN consultant c1_
ON c1_.id = p0_.consultant_id
WHERE c1_.id=1; 

SELECT p0_.name AS name_0
FROM patient p0_ INNER JOIN consultant c1_
ON (c1_.id = 1)

These two queries give the same result only by coincidence, just in a case when there is ony 1 row in patients table.
These queries are different, and give different results.
Please examine this simple demo: http://sqlfiddle.com/#!9/1987a/1

CREATE TABLE patient (
  patient_id int,
  name varchar(20),
  consultant_id int
);

insert into patient values(1,'Patient 1', 1); 
insert into patient values(2,'Patient 2', 2); 

CREATE TABLE consultant(
  id int
);

insert into consultant values(1),(2);

SELECT p0_.name AS name_0
FROM patient p0_ INNER JOIN consultant c1_
ON c1_.id = p0_.consultant_id
WHERE c1_.id=1; 

|    name_0 |
|-----------|
| Patient 1 |

SELECT p0_.name AS name_0
FROM patient p0_ INNER JOIN consultant c1_
ON (c1_.id = 1)

|    name_0 |
|-----------|
| Patient 1 |
| Patient 2 |

The main reason for primary and foreign keys is to enforce data consistency.

A primary key enforces the consistency of uniqueness of values over one or more columns. If an ID column has a primary key then it is impossible to have two rows with the same ID value. Without that primary key, many rows could have the same ID value and you wouldn't be able to distinguish between them based on the ID value alone.

A foreign key enforces the consistency of data that points elsewhere. It ensures that the data which is pointed to actually exists. In a typical parent-child relationship, a foreign key ensures that every child always points at a parent and that the parent actually exists. Without the foreign key you could have "orphaned" children that point at a parent that doesn't exist.

You will not see the inconsistency of data you would have since you only have one record and retrieved just one column.

Let's say we have these tables and columns, and records.

CREATE TABLE patient (
   patient_id INT,
   name VARCHAR(20),
   consultant_id INT
);

INSERT INTO patient VALUES(100,'Patient 1', 1); 
INSERT INTO patient VALUES(200,'Patient 2', 1); 
INSERT INTO patient VALUES(300,'Patient 3', 1); 
INSERT INTO patient VALUES(400,'Patient 4', 2); 
INSERT INTO patient VALUES(500,'Patient 5', 2); 
INSERT INTO patient VALUES(600,'Patient 6', 3); 



CREATE TABLE consultant(
   id INT,
   name VARCHAR(20)
);

INSERT INTO consultant VALUES(1,'Consultant 1');
INSERT INTO consultant VALUES(2,'Consultant 2');
INSERT INTO consultant VALUES(3,'Consultant 3');

See that we have name as well for consultants and we wanted to see which consultant attended the patient

SELECT p0_.name AS patientname_0,
       c1_.name AS consultantname_0
  FROM patient p0_ 
 INNER JOIN consultant c1_
    ON c1_.id = p0_.consultant_id
 WHERE (c1_.id = 1);

Result

patientname_0   consultantname_0
Patient 1       Consultant 1
Patient 2       Consultant 1
Patient 3       Consultant 1

However, with the query without the keys in the ON clause

SELECT p0_.name AS patientname_0,
       c1_.name AS consultantname_0
  FROM patient p0_ 
 INNER JOIN consultant c1_
    ON (c1_.id = 1);

resulted in inconsistent data for consultantname of the rest of the patients whose consultant is not 1

patientname_0   consultantname_0
Patient 1       Consultant 1
Patient 2       Consultant 1
Patient 3       Consultant 1
Patient 4       Consultant 1
Patient 5       Consultant 1
Patient 6       Consultant 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM