Home > Database > Mysql Tutorial > body text

How to define a column as the primary key in a MySQL table without using the PRIMARY KEY keyword?

王林
Release: 2023-08-21 14:25:02
forward
796 people have browsed it

如何在MySQL表中定义一个列为主键,而不使用PRIMARY KEY关键字?

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)
Copy after login

The above is the detailed content of How to define a column as the primary key in a MySQL table without using the PRIMARY KEY keyword?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!