关于数据库中的AK(Alternate Key)

本文详细解释了SQL中唯一性约束的概念及其在数据表中的应用,特别是通过替代键来确保数据的唯一性。并通过代码示例展示了如何在数据库中实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

替代键的解释(alternate key)

SQL技巧:唯一性约束

    所谓唯一性约束(unique constraint)不过是数据表内替代键的另一个名称而已。替代键(alternate key)可以是数据表内不作为主键的其他任何列,只要该键对该数据表唯一即可。换句话说,在唯一列内不允许出现数据重复的现象。比方说,你可以用车辆识别代号(VIN)作为汽车(Automobile)数据表的替代键,在汽车数据表里,主键是汽车识别号(Automobile Identification),这是一种由系统自动生成的ID。你可以在汽车表内对VIN施加唯一性约束,同时再创建一个需要VIN的表。在这个新表内可以声明外键指向汽车表。这样,只要汽车表内有VIN输入数据库就会检验VIN输入结果。这就是保证数据库内数据完整性的另一种有效的措施。

    以下是演示唯一性约束作为外键引用点的示例代码:

    create table parent
    (parent_id int not null,      -- Primary key
    parent_alternate_key int not null,     -- Alternate key
    parent_col1 int null,
    parent_col2 int null,
      constraint pk_parent_id primary key (parent_id),
      constraint ak_parent_alternate_key unique_
       (parent_id, parent_alternate_key)
    )
    go
    insert parent values ( 1, 10, 150, 151)
    insert parent values ( 2, 11, 122, 271)
    insert parent values ( 3, 12, 192, 513)
    insert parent values ( 4, 13, 112, 892)
    go
    create table child2
    (child2_parent_id int not null,     -- Primary key/Foreign key
    child2_id int not null,     -- Primary key
    child2_col1 int null,
    child2_parent_alternate_key int not null,  -- Foreign key
        constraint pk_child2 primary key (child2_parent_id, child2_id),
        constraint fk_child2_parent foreign key (child2_parent_id)
    references dbo.parent(parent_id),
        constraint fk_pk_ak_child2_parent foreign key _
      (child2_parent_id, child2_parent_alternate_key) _
      references dbo.parent(parent_id, parent_alternate_key)
    )
    go
    insert child2 values (1,1,34,10)
    insert child2 values (4,2,34,13)
    insert child2 values (2,3,34,11)
    go
    insert child2 values (1,4,34,23)   -- This one will fail
    go

转载于:https://www.cnblogs.com/Jaryleely/archive/2010/02/08/1665856.html

使用映射算法将 ER 架构映射到关系数据库架构。使用以下表示法表示生成的关系数据库架构:PK 表示主AK 表示备用,FK 表示外,并带有指向相应表(主)的箭头 Book Entity (Strong) - Title (single valued, simple string) - ISBN (single valued, simple alphanumeric string), pk - Edition (single valued, simple numeric) - Date of Publication (single valued, composite concatenation of characters and numbers) - Price (single valued, simple floating point number) - Book Description (single valued, simple string) Author Entity (Strong) - Author Name - Author_id, pk Publisher Entity (Strong) - Publisher id (single value, simple numeric), pk - Publisher Name (single valued, simple string) - Address (single valued, simple string) - together the publisher name and address could make an alternate key because no to publishers can have the same name and address Customer Entity (strong) - Customer_id (single valued, simple string), pk - Name (composite one value for first, middle and last name, simple string) - Mailing Address (single valued, simple string) - Credit Card Number and Expiration Date (single value, simple numeric sequence), alternate key - Phone Number (single value, simple alphanumeric string) - Email Address (single valued, simple alphanumeric string) Shipment (strong) - Date of Shipment ( single valued, composite of strings and numbers) - Tracking Number (single valued, simple alphanumeric string), pk - Date of Expected Delivery ( single valued, compoite of strings and numbers) Order (Strong) - Order Number (single valued, simple number), pk - Mailing Address (single value, simple string) - Method of Shipment (single value, simple string) - Date and Time of Order (when the order was placed) - Total Price of the Order (multivalue, composite) Promotion (strong entity type ) - Promotion id number, pk - Percentage Discount Points (single value, simple float) - Duration of Promotion (start date and end date) ( composite attributes like the dates above) Line Item(weak entity type) - Total price for each book that is ordered (single value, two place precision float) - Quantity of each item ordered Category (strong entity) - Category ID (single value, simple numeric), pk - Category Name (single value, simple string)
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值