加入收藏 | 设为首页 | 会员中心 | 我要投稿 南通站长网 (https://www.0513zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 系统 > 正文

sqlserver中check约束是哪几点?怎么创建?

发布时间:2022-04-26 08:59:15 所属栏目:系统 来源:互联网
导读:本文给大家分享的是关于sqlserver中check约束的内容,下文会给大家介绍check约束的概念、语法、使用等等,有这方面学习需要的朋友们可以借鉴参考。 0.什么是Check约束? CHECK约束指在表的列中增加额外的限制条件。 注: CHECK约束不能在VIEW中定义。CHECK约
       本文给大家分享的是关于sqlserver中check约束的内容,下文会给大家介绍check约束的概念、语法、使用等等,有这方面学习需要的朋友们可以借鉴参考。
 
        0.什么是Check约束?
 
        CHECK约束指在表的列中增加额外的限制条件。
 
        注: CHECK约束不能在VIEW中定义。CHECK约束只能定义的列必须包含在所指定的表中。CHECK约束不能包含子查询。
 
        创建表时定义CHECK约束
 
        1.1 语法:
 
CREATE TABLE table_name
(
  column1 datatype null/not null,
  column2 datatype null/not null,
  ...
  CONSTRAINT constraint_name CHECK (column_name condition) [DISABLE]
 
        验证:
        在表中插入supplier_id满足条件和不满足条件两种情况:
 
--supplier_id满足check约束条件,此条记录能够成功插入
insert into tb_supplier values(200, 'dlt','stk');
 
--supplier_id不满足check约束条件,此条记录能够插入失败,并提示相关错误如下
insert into tb_supplier values(1, 'david louis tian','stk');
        不满足条件的错误提示:
 
Error report -
SQL Error: ORA-02290: check constraint (502351838.CHECK_TB_SUPPLIER_ID) violated
02290. 00000 - "check constraint (%s.%s) violated"
*Cause:  The values being inserted do not satisfy the named check
        1.3 示例2:强制插入列的字母为大写
 
create table tb_products
(
 product_id    number not null,
 product_name   varchar2(100) not null,
 supplier_id    number not null,
 /*定义CHECK约束check_tb_products,用途是限制插入的产品名称必须为大写字母*/
 CONSTRAINT check_tb_products
 CHECK (product_name = UPPER(product_name))

(编辑:南通站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读