当前位置: 首页 > 新闻动态 > 技术教程

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

作者:王林 浏览: 发布日期:2023-08-21
[导读]:AsweknowthataPRIMARYKEYcolumnmusthaveuniquevaluesandcannothavenullvalueshenceifwewilldefineacolumnwithUNIQUEandNOTNULLconstraintboththenthatcolumnwouldbecomePRIMARYKEYcolumn.ExampleInthisexample,wehavecreatedatable‘Student123’byde

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)
免责声明:转载请注明出处:http://www.sczxchw.cn/news/515214.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!