Telegram Group & Telegram Channel
Антипаттерн: NULL в WHERE — и ты в ловушке

Когда в таблице есть NULL, а в WHERE ты пишешь что-то вроде:


SELECT * FROM users WHERE age != 30;


Ты ожидаешь, что выберутся все, кто не 30.
Но если age IS NULL — такие строки пропадут из выборки!

Почему? Потому что NULL != 30 не TRUE, это UNKNOWN.
А SQL возвращает строки только там, где WHERETRUE.

Как избежать?

1. Будь явно осторожен с NULL:

SELECT * FROM users
WHERE age != 30 OR age IS NULL;


2. Логика на уровне схемы:
– Если поле нужно всегда — делай NOT NULL.
– Если допускаешь NULL, продумывай поведение выборок.

3. Не верь глазам своим:
Даже count(*) и count(column) ведут себя по-разному из-за NULL.

Вывод:
NULL — это не ноль, не пустая строка и не "ничего".
Это "мы не знаем". И SQL ведёт себя с ним очень осторожно.

Сохрани, чтобы не словить грабли.

#db

👉 @database_info



tg-me.com/database_info/1445
Create:
Last Update:

Антипаттерн: NULL в WHERE — и ты в ловушке

Когда в таблице есть NULL, а в WHERE ты пишешь что-то вроде:


SELECT * FROM users WHERE age != 30;


Ты ожидаешь, что выберутся все, кто не 30.
Но если age IS NULL — такие строки пропадут из выборки!

Почему? Потому что NULL != 30 не TRUE, это UNKNOWN.
А SQL возвращает строки только там, где WHERETRUE.

Как избежать?

1. Будь явно осторожен с NULL:

SELECT * FROM users
WHERE age != 30 OR age IS NULL;


2. Логика на уровне схемы:
– Если поле нужно всегда — делай NOT NULL.
– Если допускаешь NULL, продумывай поведение выборок.

3. Не верь глазам своим:
Даже count(*) и count(column) ведут себя по-разному из-за NULL.

Вывод:
NULL — это не ноль, не пустая строка и не "ничего".
Это "мы не знаем". И SQL ведёт себя с ним очень осторожно.

Сохрани, чтобы не словить грабли.

#db

👉 @database_info

BY Базы данных (Data Base)




Share with your friend now:
tg-me.com/database_info/1445

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Find Channels On Telegram?

Telegram is an aspiring new messaging app that’s taking the world by storm. The app is free, fast, and claims to be one of the safest messengers around. It allows people to connect easily, without any boundaries.You can use channels on Telegram, which are similar to Facebook pages. If you’re wondering how to find channels on Telegram, you’re in the right place. Keep reading and you’ll find out how. Also, you’ll learn more about channels, creating channels yourself, and the difference between private and public Telegram channels.

Among the actives, Ascendas REIT sank 0.64 percent, while CapitaLand Integrated Commercial Trust plummeted 1.42 percent, City Developments plunged 1.12 percent, Dairy Farm International tumbled 0.86 percent, DBS Group skidded 0.68 percent, Genting Singapore retreated 0.67 percent, Hongkong Land climbed 1.30 percent, Mapletree Commercial Trust lost 0.47 percent, Mapletree Logistics Trust tanked 0.95 percent, Oversea-Chinese Banking Corporation dropped 0.61 percent, SATS rose 0.24 percent, SembCorp Industries shed 0.54 percent, Singapore Airlines surrendered 0.79 percent, Singapore Exchange slid 0.30 percent, Singapore Press Holdings declined 1.03 percent, Singapore Technologies Engineering dipped 0.26 percent, SingTel advanced 0.81 percent, United Overseas Bank fell 0.39 percent, Wilmar International eased 0.24 percent, Yangzijiang Shipbuilding jumped 1.42 percent and Keppel Corp, Thai Beverage, CapitaLand and Comfort DelGro were unchanged.

telegram from us


Telegram Базы данных (Data Base)
FROM USA