
- 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 foreign key in MySQL DB?
Let us first create a table −
mysql> create table DemoTable1 (Id int NOT NULL PRIMARY KEY,EmployeeName varchar(100)); Query OK, 0 rows affected (0.50 sec)
Following is the query to create a second table with foreign key constraints −
mysql> create table DemoTable2 ( Id int NOT NULL, StreetName varchar(100), CityName varchar(100), CountryName varchar(100), PinCode int, EmployeeId int, PRIMARY KEY(Id), FOREIGN KEY (Id) REFERENCES DemoTable1(Id) ); Query OK, 0 rows affected (0.46 sec)
Following is the query to identify the foreign key in MySQL DB −
mysql> show create table DemoTable2\G
This will produce the following output −
*************************** 1. row *************************** Table: DemoTable2 Create Table: CREATE TABLE `demotable2` ( `Id` int(11) NOT NULL, `StreetName` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `CityName` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `CountryName` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `PinCode` int(11) DEFAULT NULL, `EmployeeId` int(11) DEFAULT NULL, PRIMARY KEY (`Id`), CONSTRAINT `demotable2_ibfk_1` FOREIGN KEY (`Id`) REFERENCES `demotable1` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 1 row in set (0.00 sec)
- Related Articles
- MySQL Syntax to create Foreign Key?
- How to refer primary key as Foreign to various table in MySQL?
- Get a list of Foreign Key constraints in MySQL
- Foreign Key in RDBMS
- How to identify composite primary key in any MySQL database table?
- Verify that MySQL SET FOREIGN KEY CHECKS is set to = 1?
- sp_help for MySQL to display field types and foreign key constraints?
- Difference between Primary key and Foreign key in Database
- Difference Between Primary key and Foreign key in DBMS
- How to disable MySQL foreign key checks and what are the benefits of\ndisabling it?
- How to use Primary Key Constraints and Foreign Key Constraints to enforce database integrity in Oracle?
- Difference between Primary key and Foreign key in SQL Database
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- How can we add FOREIGN KEY constraints to more than one fields of a MySQL table?
- What do you mean by FOREIGN KEY and how can we use it in MySQL table?

Advertisements