How to use PRIMARY KEY on CREATE TABLE In MySQL
You can create Primary Key while creating a Table using the CREATE TABLE statement.
Syntax
CREATE TABLE table_name ( column1 datatype, column2 datatype, PRIMARY KEY (column1) ); //Composite Primary key CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, PRIMARY KEY (column1, column2) );
Example
Query to create a table and add primary key using CREATE TABLE statement:
CREATE TABLE w3wiki(Id int, Name varchar(20), PRIMARY KEY(Id));
MySQL Primary Key
MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manipulate databases. It stores data in a table format and to uniquely identify each record in a table, we require a Primary Key.
In this article, we will learn how to add, modify, and remove the primary key in MySQL tables with examples.
Contact Us