← Back to MySQL Course | Chapter 4: Constraints | Lesson 8 of 8

AUTO_INCREMENT

AUTO_INCREMENT एक deli counter के ticket machine जैसा है जो हर बार कोई line में join करने पर order में अगला number दे देता है।
Syntax
sql
column_name INT AUTO_INCREMENT PRIMARY KEY

AUTO_INCREMENT को समझना

AUTO_INCREMENT MySQL को बताता है कि जब भी एक नई row insert हो तो automatically sequence में अगला integer generate करे, यही वजह है कि यह लगभग हमेशा एक primary-key ID column के साथ pair किया जाता है।

उदाहरण: Understanding AUTO_INCREMENT

sql
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));

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

Start Value बदलना

Default रूप से sequence 1 से शुरू होता है और हर बार 1 बढ़ता है, लेकिन आप एक custom starting value set कर सकते हैं — data migrate करते समय उपयोगी जब नई IDs को किसी पुराने system की मौजूदा range से collide होने से बचना हो।

उदाहरण: Changing the Start Value

sql
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY) AUTO_INCREMENT = 1000;

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

मौजूदा Tables में AUTO_INCREMENT Add करना

आप ALTER TABLE के through एक मौजूदा column में AUTO_INCREMENT add कर सकते हैं, लेकिन वह column पहले से एक key (आमतौर पर primary key) की तरह defined होना ज़रूरी है — MySQL एक unindexed column के लिए automatically values generate नहीं करेगा।

उदाहरण: Adding AUTO_INCREMENT to Existing Tables

sql
ALTER TABLE users MODIFY id INT AUTO_INCREMENT;

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

AUTO_INCREMENT हटाना

AUTO_INCREMENT हटाना आगे automatic number generation रोक देता है, यानी future inserts को उस column के लिए explicitly अपनी value supply करनी होगी या इसके बजाय एक DEFAULT पर depend करना होगा।

उदाहरण: Removing AUTO_INCREMENT

sql
ALTER TABLE users MODIFY id INT;

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

Last Inserted ID Retrieve करना

LAST_INSERT_ID() same session के अंदर आपके सबसे हाल के insert से auto-generated ID return करता है, जो exactly वह है जो आपको एक parent row insert करके फिर तुरंत उसे reference करने वाले related child rows insert करते समय चाहिए।

उदाहरण: Retrieving the Last Inserted ID

sql
CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
INSERT INTO users (name) VALUES ('Amit');
SELECT LAST_INSERT_ID();

⚠️ 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. AUTO_INCREMENT column में एक value insert करना और फिर surprise होना कि sequence gaps भरने के बजाय उससे continue होता है।
  2. यह मान लेना कि AUTO_INCREMENT numbers gap-free हैं, जबकि deleted rows और failed inserts gaps छोड़ते हैं जो दोबारा इस्तेमाल नहीं होते।
  3. एक ऐसे column पर AUTO_INCREMENT declare करना जो एक key नहीं है, जो एक error है क्योंकि इसे indexed होना ज़रूरी है।
चैप्टर सारांश
  • NOT NULL, UNIQUE, DEFAULT, और CHECK जैसे constraints column values पर rules enforce करते हैं।
  • PRIMARY KEY हर row identify करता है, और FOREIGN KEY tables को साथ link करता है।
  • AUTO_INCREMENT नई rows के लिए automatically unique numbers generate करता है।
🔒

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.