- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
- MySQL Syntax to create Foreign Key?
- Foreign Key in RDBMS
- How to identify composite primary key in any MySQL database table?
- How to refer primary key as Foreign to various table in MySQL?
- Get a list of Foreign Key constraints in MySQL
- Difference between Primary key and Foreign key in Database
- Difference Between Primary key and Foreign key in DBMS
- 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 SQL Database
- How to use Primary Key Constraints and Foreign Key Constraints to enforce database integrity in Oracle?
- How to disable MySQL foreign key checks and what are the benefits of disabling it?
- Foreign key referencing of two DB2 tables
- 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?