컴퓨터/DB, SQL

Null values (edX StanfordOnline Databases: Relational Databases and SQL)

수제녹차 2025. 6. 15. 15:38
728x90
반응형

출처

https://www.edx.org/learn/relational-databases/stanford-university-databases-relational-databases-and-sql

 

StanfordOnline: Databases: Relational Databases and SQL | edX

This course is one of five self-paced courses on the topic of Databases, originating as one of Stanford's three inaugural massive open online courses released in the fall of 2011. The original "Databases" courses are now all available on edx.org. This cour

www.edx.org

Not null 같은 제약이 없는 이상,

DB는 null 값을 받을 수 있다.

 

Null: undefined or unknown 을 의미한다.

 

null value를 db table에 넣어보자.

insert into Student values (432, 'Kevin', null, 1500);
insert into Student values (321, 'Lori', null, 2500);

삽입 후 확인해보면, null value는 db table에 빈 칸으로 보인다.

 

select sID, sName, GPA
from Student
where GPA > 3.5 or GPA <= 3.5;

이 쿼리를 실행해보면,

GPA가 null인 Kevin과 Lori를 찾을 수 없다.

 

select sID, sName, GPA
from Student
where GPA > 3.5 or GPA <= 3.5 or GPA is null;

이 쿼리로는 찾을 수 있다.

 

NULL이 있으면 비교나 조건 판별이 애매해지고, null을 고려하지 않았을 때 그 행은 결과에서 빠질 수 있다.

따라서 null-safe한 쿼리를 사용해야 한다.

three valued logic을 사용해야 한다 (true, false, unknown)

 

 

* null value가 있을 때 aggregate function은 어떻게 동작할까?

 

select distinct GPA
from Student;

null값도 출력된다.

 

 

select count(distinct GPA)
from Student;

null value를 세지 않는다.

 

 

반응형