登录
首页 >  数据库 >  MySQL

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

来源:tutorialspoint

时间:2023-08-24 19:54:56 226浏览 收藏

学习数据库要努力,但是不要急!今天的这篇文章《如何在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)

好了,本文到此结束,带大家了解了《如何在MySQL表中定义一个列为主键,而不使用PRIMARY KEY关键字?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!

声明:本文转载于:tutorialspoint 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>