Skip to main content
Maher Sakka's Blog
Main navigation
  • Home
  • Contact me
Language switcher
Breadcrumb
  1. Home
  2. Blog
  3. How To Detect Tables Wihtout Primary Key In a Database

How to detect tables wihtout primary key in a database

Primary keys in Database are essential for many reasons; they maintain entity integrity and help the database work faster when searching or linking data.

Sometimes, the primary keys aren't set for some tables. It's better to create the keys for these tables for better performance and integrity.

The following SQL query list all tables without Primary Key in a specific Database (<database_name>):

SELECT 
    TABLE_SCHEMA as 'Database',
    TABLE_NAME as 'Table',
    TABLE_ROWS as 'Row Count'
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = '<database_name>'
AND TABLE_TYPE = 'BASE TABLE'
AND TABLE_NAME NOT IN (
    SELECT TABLE_NAME 
    FROM information_schema.TABLE_CONSTRAINTS 
    WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' 
    AND TABLE_SCHEMA = '<database_name>'
)
ORDER BY TABLE_ROWS DESC;

just replace <database_name> by your database name.

Tags
  • Database
Related posts
  • How to export Database with time-stamped file name in phpMyAdmin
  • How to clear Drupal cache via database
Tags
  • Laravel
  • Linux
  • Drupal
  • Database
  • Tips & Tricks
  • Mobile
  • Servers
  • SEO
Recent posts
  • How to detect tables wihtout primary key in a database
  • How to know which ini configuration files are loaded with PHP
  • How to prevent search engines from indexing your staging server websites
Monthly archive
  • May 2017 (2)
  • August 2018 (1)
  • October 2020 (1)
  • August 2023 (1)
  • July 2024 (2)
  • November 2024 (1)
  • July 2025 (10)
  • October 2025 (6)
Projects
  • elmanager.tn
  • 365mathex.com
  • vpop.tn
  • toufoula.tn
  • writethemoney.com
  • dinartunisien.com
RSS feed

mahersakka.com © 2024 
Build Your IT Skills, Grow Your Wealth with Maher Sakka