← Back to MySQL Course | Chapter 3: Data Types | Lesson 5 of 8

SET Data Type

SET एक checklist जैसा है जहाँ आप एक list में से कोई नहीं, एक या कई boxes tick कर सकते हैं, सब एक single column में saved।
Syntax
sql
column_name SET('value1', 'value2', 'value3')

SET को समझना

SET एक single column को एक predefined list में से एक साथ zero, एक, या कई values रखने देता है, ENUM के उलट जो सिर्फ एक की अनुमति देता है — किसी row पर tags या permissions की list जैसी चीज़ के लिए उपयोगी।

उदाहरण: Understanding SET

sql
CREATE TABLE users (permissions SET('read', 'write', 'admin'));
INSERT INTO users VALUES ('read,write');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

SET Elements Query करना

क्योंकि एक SET column एक साथ कई values रख सकता है, एक plain equality check अक्सर आपकी उम्मीद से match नहीं होगा; FIND_IN_SET() function यह test करने का सही तरीका है कि एक specific value मौजूद है या नहीं।

उदाहरण: Querying SET Elements

sql
CREATE TABLE users (permissions SET('read', 'write', 'admin'));
INSERT INTO users VALUES ('read,write');
SELECT * FROM users WHERE FIND_IN_SET('write', permissions);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

SET का Binary Representation

Internally, MySQL एक SET को एक bitmap की तरह store करता है जहाँ हर defined option एक bit occupy करता है (1, 2, 4, 8, वगैरह), जो कई values combine या test करना इतना storage-efficient बनाता है।

उदाहरण: Binary representation of SET

sql
-- 'read'=1, 'write'=2, 'admin'=4 -- combined values sum their bits
CREATE TABLE users (permissions SET('read', 'write', 'admin'));
INSERT INTO users VALUES ('read,admin'); -- stored internally as 1 + 4 = 5

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

SET Columns Update करना

आप string functions या direct bitwise operations इस्तेमाल करके एक SET column की current value से individual options add या remove कर सकते हैं, बिना पहले से stored हर दूसरी value जाने या दोबारा type किए।

उदाहरण: Updating SET Columns

sql
CREATE TABLE users (id INT, permissions SET('read', 'write', 'admin'));
INSERT INTO users VALUES (1, 'read');
UPDATE users SET permissions = CONCAT(permissions, ',write') WHERE id = 1;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Invalid SET Values

ENUM की तरह, SET की defined list का हिस्सा न होने वाली एक value insert करना strict SQL mode के तहत पूरी तरह fail होगा, column को चुपचाप typos या invalid tags जमा होने से protect करते हुए।

उदाहरण: Invalid SET Values

sql
CREATE TABLE users (permissions SET('read', 'write', 'admin'));
INSERT INTO users VALUES ('delete'); -- rejected in strict SQL mode

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी SET column को = से search करना, जैसे WHERE permissions = read, जो 'read,write' वाली rows से match नहीं करता (FIND_IN_SET इस्तेमाल करें)।
  2. value में spaces add करना, जैसे 'read, write', जो defined options से match नहीं करता।
  3. flexible संख्या में values चाहिए होने वाले या बहुत query होने वाले data के लिए SET इस्तेमाल करना, जब एक अलग table बेहतर हो।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.