
- 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 can I define a column of a MySQL table PRIMARY KEY without using the PRIMARY KEY keyword?
As we know that a PRIMARY KEY column must have unique values and cannot have null values hence if we will define a column with UNIQUE and NOT NULL constraint both then that column would become PRIMARY KEY column.
Example
In this example, we have created a table ‘Student123’ by defining column ‘RollNo’ with UNIQUE and NOT NULL constraints. Now, by describing the table we can see that ‘RollNo’ is the PRIMARY KEY column.
mysql> Create table Student123(RollNo INT UNIQUE NOT NULL, Name varchar(20)); Query OK, 0 rows affected (0.25 sec) mysql> DESCRIBE Student123; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | RollNo | int(11) | NO | PRI | NULL | | | Name | varchar(20) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 2 rows in set (0.04 sec)
- Related Articles
- Can we remove a primary key from MySQL table?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How to get primary key of a table in MySQL?
- How to get the primary key “column name” of a specific table in MySQL?
- How to reset the primary key of a table in mysql?
- How do I drop a primary key in MySQL?
- How can we set PRIMARY KEY on multiple columns of a MySQL table?
- How to make MySQL table primary key auto increment?
- Set existing column as Primary Key in MySQL?
- ALTER TABLE to add a composite primary key in MySQL?
- Remove Primary Key in MySQL?
- Reset Primary Key in MySQL
- How to find the primary key of a DB2 table TAB1?
- How do you get whether a column is a primary key in MySQL?
- MySQL ALTER column to remove primary key and auto_increment?

Advertisements