Để tìm tất cả các bảng trong một database chứa một cột tên cụ thể (ví dụ: 'StudentFirstName'), bạn truy vấn hệ thống catalog information_schema.columns.
Ví dụ minh họa
Giả sử database tên là web, cần tìm cột StudentFirstName:
mysql> select myColumnName.table_name from information_schema.columns myColumnName where myColumnName.column_name = 'StudentFirstName' and table_schema='web';
Kết quả:
+---------------+ | TABLE_NAME | +---------------+ | demotable109 | | demotable297 | | demotable335 | | demotable395 | | demotable418 | | demotable425 | | demotable436 | +---------------+
7 bảng chứa cột 'StudentFirstName'.
Kiểm tra cấu trúc một bảng mẫu
mysql> desc demotable297;
Kết quả xác nhận có cột StudentFirstName:
+------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+-------+ | StudentId | int(11) | YES | | NULL | | | StudentFirstName | varchar(100) | YES | | NULL | | | StudentLastName | varchar(100) | YES | | NULL | | +------------------+--------------+------+-----+---------+-------+
Giải thích:
information_schema.columns: Bảng hệ thống chứa metadata tất cả cột mọi bảngtable_schema='web': Lọc theo database tên 'web'column_name='StudentFirstName': Lọc theo tên cột cần tìm
Mở rộng: Thêm SELECT table_name, column_name, data_type, character_maximum_length ... để xem chi tiết kiểu dữ liệu, độ dài của cột đó ở từng bảng.