INFORMATION_SCHEMA से अगला AUTO_INCREMENT मान प्राप्त करना
नहीं, नया रिकॉर्ड जोड़े बिना अगली प्राथमिक कुंजी प्राप्त करना संभव है। पहले टेबल बनाएं:
mysql> create table DemoTable1399 ( StudentId int NOT NULL AUTO_INCREMENT, PRIMARY KEY(StudentId) ); Query OK, 0 rows affected (0.53 sec)
रिकॉर्ड डालें:
mysql> insert into DemoTable1399 values(); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1399 values(); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1399 values(); Query OK, 1 row affected (0.07 sec)
सभी रिकॉर्ड देखें:
mysql> select * from DemoTable1399;
आउटपुट:
+-----------+ | StudentId | +-----------+ | 1 | | 2 | | 3 | +-----------+ 3 rows in set (0.00 sec)
नया रिकॉर्ड जोड़े बिना अगली प्राथमिक कुंजी प्राप्त करने की क्वेरी:
mysql> select auto_increment as NextPrimaryKey from information_schema.tables where table_schema=database() and table_name = 'DemoTable1399';
परिणाम:
+----------------+ | NextPrimaryKey | +----------------+ | 4 | +----------------+ 1 row in set (0.00 sec)
व्याख्या: INFORMATION_SCHEMA.TABLES में AUTO_INCREMENT कॉलम होता है जो अगले ऑटो-इंक्रीमेंट मान को संग्रहीत करता है। TABLE_SCHEMA=DATABASE() वर्तमान डेटाबेस को फ़िल्टर करता है। यह विधि नई पंक्ति डाले बिना अगली आईडी जानने के लिए उपयोगी है (उदाहरण के लिए, UI में पूर्व-प्रदर्शन के लिए)। नोट: रोलबैक या डिलीट के बाद मान पुन: उपयोग नहीं होता; यह केवल बढ़ता है।