数据库约束知多少

本文详细介绍了数据库中的五种约束:非空(NotNull)、唯一(Unique)、主键(PrimaryKey)、外键(ForeignKey)及检查约束(Check)。每种约束都有具体的业务场景应用实例,帮助读者更好地理解如何使用这些约束来维护数据的完整性和一致性。

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

一、数据库约束知多少

非空 、唯一 、主键  、外键  、检查约束

二、各约束具体含义

Not Null, which prevents a value from being null

In the EMPLOYEES table, the column LAST_NAME has the NOT NULL constraint, which enforces the business rule that every employee must have a last name.

•Unique, which prevents multiple rows from having the same value in the same column or combination of columns, but allows some values to be null

In the EMPLOYEES table, the column EMAIL has the UNIQUE constraint, which enforces the business rule that an employee can have no email address, but cannot have the same email address as another employee.

•Primary Key, which is a combination of NOT NULL and UNIQUE

In the EMPLOYEES table, the column EMPLOYEE_ID has the PRIMARY KEY constraint, which enforces the business rule that every employee must have a unique employee identification number.

•Foreign Key, which requires values in one table to match values in another table

In the EMPLOYEES table, the column JOB_ID has a FOREIGN KEY constraint that references the JOBS table, which enforces the business rule that an employee cannot have a JOB_ID that is not in the JOBS table.

•Check, which requires that a value satisfy a specified condition

The EMPLOYEES table does not have CHECK constraints. However, suppose that EMPLOYEES needs a new column, EMPLOYEE_AGE, and that every employee must be at least 18. The constraint CHECK (EMPLOYEE_AGE >= 18) enforces the business rule.

Tip:
Use check constraints only when other constraint types cannot provide the necessary checking.
•REF, which further describes the relationship between the column and the object that it references
For information about REF constraints, see Oracle Database Concepts.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值