和谐自然 發表於 2025-9-6 11:34:18

一文详解如何区分数据库中的主键

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">1. 主键的核心特性</a></li><li><a href="#_label1">2. 区分主键的方法</a></li><ul class="second_class_ul"><li><a href="#_lab2_1_0">(1) 通过表定义(DDL)识别</a></li><li><a href="#_lab2_1_1">(2) 通过数据库工具查看</a></li><li><a href="#_lab2_1_2">(3) 通过约束名称识别</a></li><li><a href="#_lab2_1_3">(4) 通过数据字典查询</a></li></ul><li><a href="#_label2">3. 主键 vs 其他键</a></li><ul class="second_class_ul"></ul><li><a href="#_label3">4. 主键的常见实现形式</a></li><ul class="second_class_ul"></ul><li><a href="#_label4">5. 示例验证</a></li><ul class="second_class_ul"></ul><li><a href="#_label5">总结</a></li><ul class="second_class_ul"></ul></ul></div><p>在数据库中,<strong>主键</strong>(Primary Key)是用于唯一标识表中每一行记录的列或列组合。以下是区分主键的关键特征和方法:</p>
<p class="maodian"><a name="_label0"></a></p><h2>1. 主键的核心特性</h2>
<ul><li><strong>唯一性</strong>:主键的值在表中必须唯一,不能重复。</li><li><strong>非空性</strong>:主键列不能为 <code>NULL</code>(必须包含值)。</li><li><strong>不可变性</strong>:主键值通常不应频繁修改(如需要修改,需确保不影响数据完整性)。</li></ul>
<p class="maodian"><a name="_label1"></a></p><h2>2. 区分主键的方法</h2>
<p class="maodian"><a name="_lab2_1_0"></a></p><h3>(1) 通过表定义(DDL)识别</h3>
<p>在创建表的SQL语句中,主键会显式声明:</p>
<div class="jb51code"><pre class="brush:sql;">CREATE TABLE Students (
    student_id INT PRIMARY KEY,-- 单列主键
    name VARCHAR(50),
    age INT
);

-- 或复合主键(多列组合)
CREATE TABLE Orders (
    order_id INT,
    product_id INT,
    quantity INT,
    PRIMARY KEY (order_id, product_id)-- 复合主键
);
</pre></div>
<p class="maodian"><a name="_lab2_1_1"></a></p><h3>(2) 通过数据库工具查看</h3>
<ul><li><strong>图形化工具(如MySQL Workbench、DBeaver等)</strong>:<br />主键通常会被标记为 <strong>PK</strong>(Primary Key),或在表设计视图中高亮显示。</li><li><strong>命令行工具</strong>:<br />使用 <code>DESCRIBE 表名;</code> 或 <code>SHOW CREATE TABLE 表名;</code> 查看主键信息。</li></ul>
<p class="maodian"><a name="_lab2_1_2"></a></p><h3>(3) 通过约束名称识别</h3>
<p>主键约束的名称通常包含 <code>PK</code> 或 <code>PRIMARY</code>,例如:</p>
<div class="jb51code"><pre class="brush:sql;">-- 查询表的约束(以MySQL为例)
SELECT * FROM information_schema.TABLE_CONSTRAINTS
WHERE table_name = '表名' AND constraint_type = 'PRIMARY KEY';
</pre></div>
<p class="maodian"><a name="_lab2_1_3"></a></p><h3>(4) 通过数据字典查询</h3>
<p>不同数据库的系统表存储主键信息:</p>
<ul><li><strong>MySQL</strong>:<code>information_schema.KEY_COLUMN_USAGE</code></li><li><strong>Oracle</strong>:<code>USER_CONSTRAINTS</code> 和 <code>USER_CONS_COLUMNS</code></li><li><strong>SQL Server</strong>:<code>sys.key_constraints</code></li></ul>
<p class="maodian"><a name="_label2"></a></p><h2>3. 主键 vs 其他键</h2>
<table><thead><tr><th><strong>特性</strong></th><th><strong>主键 (Primary Key)</strong></th><th><strong>唯一键 (Unique Key)</strong></th><th><strong>外键 (Foreign Key)</strong></th></tr></thead><tbody><tr><td><strong>唯一性</strong></td><td>必须唯一</td><td>必须唯一</td><td>可重复</td></tr><tr><td><strong>NULL值</strong></td><td>不允许</td><td>允许(除非显式限制)</td><td>允许</td></tr><tr><td><strong>用途</strong></td><td>标识唯一行</td><td>确保列值唯一</td><td>关联其他表的主键</td></tr><tr><td><strong>数量限制</strong></td><td>每表仅一个主键</td><td>每表可多个唯一键</td><td>可多个外键</td></tr></tbody></table>
<p class="maodian"><a name="_label3"></a></p><h2>4. 主键的常见实现形式</h2>
<ol><li><p><strong>自然主键</strong>:<br />使用业务中具有唯一性的列(如身份证号、学号等)。<br /><strong>风险</strong>:业务规则变化可能导致主键不稳定。</p></li><li><p><strong>代理主键</strong>:<br />使用与业务无关的列(如自增ID、UUID等)。<br /><strong>优点</strong>:稳定且易于管理,推荐使用。</p></li></ol>
<p class="maodian"><a name="_label4"></a></p><h2>5. 示例验证</h2>
<p>假设有一个表 <code>Employees</code>:</p>
<div class="jb51code"><pre class="brush:sql;">CREATE TABLE Employees (
    emp_id INT AUTO_INCREMENT PRIMARY KEY,-- 代理主键
    emp_name VARCHAR(50) NOT NULL,
    email VARCHAR(100) UNIQUE            -- 唯一键
);
</pre></div>
<ul><li><strong>主键</strong>:<code>emp_id</code>(唯一、非空、自增)。</li><li><strong>唯一键</strong>:<code>email</code>(唯一但允许NULL)。</li></ul>
<p class="maodian"><a name="_label5"></a></p><h2>总结</h2>
<ul><li><strong>主键是表的唯一标识符</strong>,通过唯一性和非空性保障数据完整性。</li><li>通过表定义、数据库工具或系统表可快速识别主键。</li><li>优先选择代理主键(如自增ID)以避免业务逻辑耦合。</li></ul>
頁: [1]
查看完整版本: 一文详解如何区分数据库中的主键