
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to identify composite primary key in any MySQL database table?
You can use aggregate function count(*). If it returns a value greater than 1, that would mean the table has composite primary key.
Let us first create a table −
mysql> create table DemoTable1324 -> ( -> StudentId int, -> StudentName varchar(20), -> StudentAge int, -> StudentCountryName varchar(20) -> ); Query OK, 0 rows affected (0.52 sec)
Here is the query to add composite primary key −
mysql> alter table DemoTable1324 ADD CONSTRAINT constr_IdAgeCountry PRIMARY KEY (StudentId, StudentAge,StudentCountryName); Query OK, 0 rows affected (1.29 sec) Records: 0 Duplicates: 0 Warnings: 0
Following is the query to identify composite primary key in any MySQL database table −
mysql> select count(*) AS Total -> from information_schema.KEY_COLUMN_USAGE -> where table_name='DemoTable1324' and table_schema=database();
This will produce the following output −
+-------+ | Total | +-------+ | 3 | +-------+ 1 row in set, 2 warnings (0.76 sec)
- Related Articles
- ALTER TABLE to add a composite primary key in MySQL?
- How to make MySQL table primary key auto increment?
- How to get primary key of a table in MySQL?
- How can we remove composite PRIMARY KEY constraint applied on multiple columns of an existing MySQL table?
- How to reset the primary key of a table in mysql?
- How to refer primary key as Foreign to various table in MySQL?
- How to implement CANDIDATE key in any MySQL table?
- Difference between Primary key and Foreign key in Database
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- Can we remove a primary key from MySQL table?
- How to get the primary key “column name” of a specific table in MySQL?
- Difference between Primary key and Foreign key in SQL Database
- Remove Primary Key in MySQL?
- Reset Primary Key in MySQL
- How to use Primary Key Constraints and Foreign Key Constraints to enforce database integrity in Oracle?

Advertisements