gpt4 book ai didi

Oracle数据库的字段约束创建和维护示例

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Oracle数据库的字段约束创建和维护示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

创建Oracle数据库的字段约束:

  1. 非空约束
  2. 唯一约束
  3. 对字段的取值的约束
  4. 默认值
  5. 外键约束
?
1
2
3
4
create table tab_class(
  class_id number primary key ,
  class_name varchar2(10) not null unique
);
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
create table tab_stu(
stu_id number,
  --学生姓名,不能为空,不能重复
stu_name varchar2(20) not null unique ,
  --学生姓名只能是male或female
stu_gender varchar2(6) not null check (stu_gender= 'male' or stu_gender= 'female' ),
  --学生年龄只能在18到60之间
stu_age number check (stu_age >18 and stu_age <60),
  --邮箱可以不填写,填写的话不能相同
stu_email varchar2(30) unique ,
stu_address varchar2(30),
--外键约束
class_id number not null references tab_class(class_id) 
);

维护已经创建好的约束:

  1. 可添加或删除约束,但不能直接修改。
  2. 可使约束启用和禁用。
  3. 非空约束必须使用MODIFY子句增加。
  4. 为表增加主键约束:
?
1
2
3
4
5
6
7
8
9
--维护约束
--创建约束
create table tab_check(
  che_id number,
  che_name varchar2(20)
);
--为表增加主键约束
alter table tab_check
add constraints tab_check primary key (che_id);

添加唯一约束 。

?
1
2
3
--添加唯一约束,tab_check_unique表示约束的名称
alter table tab_check
add constraints tab_check_unique unique (che_name);

添加检查约束:

?
1
2
3
4
5
6
--添加一个字段
alter table tab_check
add che_age number;
--添加检查约束
alter table tab_check
add constraints tab_check_age check (che_age>18 and che_age<60);

删除约束:

?
1
2
3
--删除主键约束
alter table tab_check
drop constraints tab_check;

禁用约束:

?
1
2
--禁用约束
alter table tab_check disable constraints tab_check;

启用约束 。

?
1
2
--启用约束
alter table tab_check enable constraints tab_check;

复合约束,联合主键,也就是两个字段的组合成一个主键 。

?
1
2
3
4
5
6
7
--联合主键
create table tab_person(
  tab_firstname varchar2(10),
  tab_lastname varchar2(10),
  tab_gender varchar2(5),
  primary key (tab_firstname,tab_lastname)
);

为表添加外键约束:

?
1
2
alter table tab_stu
add constraints tab_stu foreign key (class_id) references tab_class(class_id);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:http://blog.csdn.net/facekbook/article/details/12214735 。

最后此篇关于Oracle数据库的字段约束创建和维护示例的文章就讲到这里了,如果你想了解更多关于Oracle数据库的字段约束创建和维护示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com