
- 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
What do you mean by PRIMARY KEY and how can we use it in MySQL table?
PRIMARY KEY uniquely identifies each row in a database. A PRIMARY KEY must contain unique value and must not contain NULL values. There can be only one PRIMARY KEY in a MySQL table. We can create a PRIMARY KEY column by defining a PRIMARY KEY constraint. For defining PRIMARY KEY constraint we must have to use PRIMARY KEY keyword while creating the table and it can be demonstrated in the following example −
Example
The following query we have created a table named ‘student’ by defining the column ‘RollNo’ as PRIMARY KEY −
mysql> Create Table Student(RollNo INT PRIMARY KEY, Name Varchar(20), Address Varchar(20), DOB DATE); Query OK, 0 rows affected (0.16 sec)
Now by describing the table as follows, we can see ‘RollNo’ is having a PRIMARY KEY constraint −
mysql> Describe Student; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | RollNo | int(11) | NO | PRI | NULL | | | Name | varchar(20) | YES | | NULL | | | Address | varchar(20) | YES | | NULL | | | DOB | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.03 sec)
Now, the ‘RollNo’ column of the ‘Student’ table must have unique values and it cannot have null values.
- Related Articles
- What do you mean by FOREIGN KEY and how can we use it in MySQL table?
- Can we remove a primary key from MySQL table?
- How can we set PRIMARY KEY on multiple columns of a MySQL table?
- Can we use PRIMARY KEY( column1, column2) in MySQL to make pairs?
- How can we set PRIMARY KEY on multiple columns of an existing MySQL table?
- What do you mean MySQL user variables and how can we assign values to\nthem?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- What do you mean by database view and how do MySQL views work?
- How can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
- How do you get whether a column is a primary key in MySQL?
- How can we remove composite PRIMARY KEY constraint applied on multiple columns of an existing MySQL table?
- How do I drop a primary key in MySQL?
- How to make MySQL table primary key auto increment?
- How to get primary key of a table in MySQL?

Advertisements