Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Menampilkan kolom tabel MYSQL dengan tipe kunci dan referensi

Ini adalah kueri (diputar) terakhir saya:

SELECT c.COLUMN_NAME,
       --c.COLUMN_KEY,
       IF(EXISTS(select *
                 FROM information_schema.KEY_COLUMN_USAGE k
                 JOIN information_schema.TABLE_CONSTRAINTS tc 
                   ON (k.TABLE_SCHEMA=tc.TABLE_SCHEMA 
                       AND k.TABLE_NAME=tc.TABLE_NAME 
                       AND k.CONSTRAINT_NAME=tc.CONSTRAINT_NAME)
                WHERE k.TABLE_SCHEMA=c.TABLE_SCHEMA 
                      AND k.TABLE_NAME=c.TABLE_NAME 
                      AND tc.CONSTRAINT_TYPE='PRIMARY KEY' 
                      AND c.COLUMN_NAME=k.COLUMN_NAME),'PK',null) AS PK,
       IF(EXISTS(select *
                 FROM information_schema.KEY_COLUMN_USAGE k
                 JOIN information_schema.TABLE_CONSTRAINTS tc 
                   ON (k.TABLE_SCHEMA=tc.TABLE_SCHEMA 
                      AND k.TABLE_NAME=tc.TABLE_NAME 
                      AND k.CONSTRAINT_NAME=tc.CONSTRAINT_NAME)
                WHERE k.TABLE_SCHEMA=c.TABLE_SCHEMA 
                      AND k.TABLE_NAME=c.TABLE_NAME 
                      AND tc.CONSTRAINT_TYPE='UNIQUE' 
                      AND c.COLUMN_NAME=k.COLUMN_NAME),'UQ',null) AS UQ,
       IF(EXISTS(select *
                 FROM information_schema.KEY_COLUMN_USAGE k
                 JOIN information_schema.TABLE_CONSTRAINTS tc 
                   ON (k.TABLE_SCHEMA=tc.TABLE_SCHEMA 
                      AND k.TABLE_NAME=tc.TABLE_NAME 
                      AND k.CONSTRAINT_NAME=tc.CONSTRAINT_NAME)
                WHERE k.TABLE_SCHEMA=c.TABLE_SCHEMA 
                      AND k.TABLE_NAME=c.TABLE_NAME 
                      AND tc.CONSTRAINT_TYPE='FOREIGN KEY' 
                      AND c.COLUMN_NAME=k.COLUMN_NAME),'FK',null) AS FK,
       c.EXTRA,
       c.IS_NULLABLE,
       c.DATA_TYPE,
       c.COLUMN_TYPE,
       c.CHARACTER_MAXIMUM_LENGTH,
       c.COLUMN_COMMENT,
       k.REFERENCED_TABLE_SCHEMA,
       k.REFERENCED_TABLE_NAME,
       k.REFERENCED_COLUMN_NAME
FROM information_schema.COLUMNS c
LEFT JOIN information_schema.KEY_COLUMN_USAGE k
    ON (k.TABLE_SCHEMA=c.TABLE_SCHEMA
   AND k.TABLE_NAME=c.TABLE_NAME
   AND k.COLUMN_NAME=c.COLUMN_NAME
   AND k.POSITION_IN_UNIQUE_CONSTRAINT IS NOT NULL)
 WHERE c.TABLE_SCHEMA='PHPDAO'
   AND c.TABLE_NAME='employees';

Tapi saya pikir harus ada solusi yang lebih sederhana.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Kesalahan fatal:Menggunakan $this saat tidak dalam konteks objek di

  2. Kemungkinan konsekuensi dari peningkatan panjang varchar di MySql?

  3. Menggabungkan rentang interval secara efisien dalam SQL

  4. cara menyuntikkan sql dalam proses login

  5. PANJANG MYSQL CONCAT MAX