इसके लिए ALTER के साथ ADD का उपयोग करें। सिंटैक्स:
alter table yourTableName add yourColumnName DATETIME DEFAULT NOW(), add index(yourColumnName);
उदाहरण
टेबल बनाएं:
mysql> create table DemoTable (
-> Id int NOT NULL AUTO_INCREMENT,
-> Name varchar(100),
-> PRIMARY KEY(Id)
-> );
Query OK, 0 rows affected (0.69 sec)
टेबल विवरण जांचें:
mysql> desc DemoTable;
आउटपुट
+-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | Name | varchar(100) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec)
एक ही क्वेरी में कॉलम और इंडेक्स जोड़ें:
mysql> alter table DemoTable
-> add ArrivalDate DATETIME DEFAULT NOW(),
-> add index(ArrivalDate);
Query OK, 0 rows affected (2.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
फिर से टेबल विवरण जांचें:
mysql> desc DemoTable;
आउटपुट
+-------------+--------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+-------------------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | Name | varchar(100) | YES | | NULL | | | ArrivalDate | datetime | YES | MUL | CURRENT_TIMESTAMP | | +-------------+--------------+------+-----+-------------------+----------------+ 3 rows in set (0.01 sec)
नोट: MUL का अर्थ है मल्टीपल (नॉन-यूनिक) इंडेक्स। DEFAULT NOW() के लिए MySQL 8.0+ में CURRENT_TIMESTAMP उपयोग होता है।