Computer >> Máy Tính >  >> Lập trình >> MySQL

Làm cách nào để biết một cột có phải là auto_increment trong MySQL hay không?

Để tìm xem một cột có phải là auto_increment trong MySQL hay không, bạn có thể sử dụng cú pháp sau -

select COLUMN_NAME from information_schema.columns where
TABLE_SCHEMA='yourDatabaseName' and TABLE_NAME='yourTableName' and EXTRA
like '%auto_increment%';

Đầu tiên chúng ta hãy tạo một bảng. Ở đây, ClientId được đặt AUTO_INCREMENT -

mysql> create table autoIncrementTableDemo
   -> (
   -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ClientName varchar(20),
   -> ClientAge int,
   -> ClientAddress varchar(100),
   -> ClientCountryName varchar(100)
   -> );
Query OK, 0 rows affected (0.61 sec)

Bây giờ, chúng ta hãy tìm xem có cột nào là auto_increment hay không -

mysql> select COLUMN_NAME from information_schema.columns where
TABLE_SCHEMA='test' and TABLE_NAME='autoIncrementTableDemo' and EXTRA like
'%auto_increment%';

Sau đây là kết quả cung cấp cho cột, tức là auto_increment -

+-------------+
| COLUMN_NAME |
+-------------+
| ClientId    |
+-------------+
1 row in set (0.00 sec)