1. 定义主键约束

1.1 在创建表时定义主键约束

create table student
(
name varchar2(8),
studentid varchar2(10) primary key,
sex char(2)
);

1.2 创建表后,使用alter table命令添加约束

1.2.1 创建表

create table student
(
name varchar2(8),
studentid varchar2(10),
sex char(2)
);

1.2.2 添加主键约束

alter table student
add constraint pk_student primary key(studentid);

其中 constraint pk_student 用于给该主键约束定义名称(可省略)

其中 pk_student   为约束名,用于修改约束,

例如:删除该主键约束

alter table student

drop constraint pk_student  ;

2 定义not null约束

not null 约束只能定义为列级约束

2.1 在创建表时定义not null约束

同样的not null约束可以在创建表时定义

eg:

create table tset
(
name varchar2(8) NOT NULL,
studentid varchar2(10) primary key,
sex char(2)
);

2.2 创建表后,在修改表添加not null约束

2.2.1 创建表

create table tset
(
name varchar2(8),
studentid varchar2(10) primary key,
sex char(2)
);

2.2.2添加not null约束

alter table test modify name not null;

3 定义唯一性 unique约束

唯一性约束是指:被约束的字段不能出现重复输入的值

唯一性与逐渐的区别:

(1)unique一个表可以定义多个,而primary key 一个表只能定义一个

(2)unique允许被约束的字段为空,而primary key不允许为空

3.1 创建表示定义unique约束

create table test
(
name varchar2(8) NOT NULL,
studentid varchar2(10) primary key,
sex char(2),
idnum char(18) unique
);

3.2创建表后,为表添加unique约束

3.2.1 创建表

create table test
(
name varchar2(8) NOT NULL,
studentid varchar2(10) primary key,
sex char(2),
idnum char(18)
);

3.2.2 添加约束

alter table test

add constraint un_idnum unique(idnum);

4 定义检查check约束

检查约束用来指定某列的可取值得范围,只有符合条件的值才能输入到表中

4.1 在创建表时定义check

create table test
(
name varchar2(8) NOT NULL,
studentid varchar2(10) primary key,
sex char(2) constraint ch_sex check(sex in ('男','女')),
idnum char(18)
);

4.2.1 创建表

create table test
(
name varchar2(8) NOT NULL,
studentid varchar2(10) primary key,
sex char(2),
idnum char(18)
);

4.2.2 添加检查约束

alter table test add constraint ch_sex

check(sex in('男','女'));

5 定义外键约束

5.1 创建表示定义外键

5.1.1 在定义字段时定义外键

create table course
(
id varchar2(10) primary key,
classname varchar2(10),
studentid VARCHAR2(10) references test(studentid)
)

5.1.2  定义字段后,再定义外键

create table course
(
id varchar2(10) primary key,
classname varchar2(10),
studentid VARCHAR2(10),
constraint fk_course foreign key(studentid) references test(studentid)
);

*************注意两种方式的区别*****************

5.2 通过alter table 命令创建外键约束

alter table course

add constraint fk_course foreign key(studentid) references test(studentid);

oracle 定义数据完整性的更多相关文章

  1. SQL Server-数据库架构和对象、定义数据完整性(二)

    前言 本节我们继续SQL之旅,本节我们如题来讲讲一些基本知识以及需要注意的地方,若有不妥之处,还望指出,简短的内容,深入的理解,Always to review the basics. 数据库架构和对 ...

  2. SQL Server-数据库架构和对象、定义数据完整性

    前言 本节我们继续SQL之旅,本节我们如题来讲讲一些基本知识以及需要注意的地方,若有不妥之处,还望指出,简短的内容,深入的理解,Always to review the basics. 数据库架构和对 ...

  3. Oracle数据库 数据完整性和DML语句

    数据完整性和DML语句 数据完整性 数据完整性(Data Integrity)是指数据的精确性(Accuracy) 和可靠性(Reliability).它是应防止数据库中存在不符合语义规定的数据和防止 ...

  4. 问题:oracle DECLARE 变量重复利用;结果:Oracle 定义变量总结

    首先,当在cmd里办入scott密码提示错误时,可以这样改一下,scott的解锁命令是: 以system用户登录:cmdsqlplus system/tigertigeralter user scot ...

  5. Oracle 定义变量总结

    首先,当在cmd里办入scott密码提示错误时,可以这样改一下,scott的解锁命令是: 以system用户登录:cmdsqlplus system/tigertigeralter user scot ...

  6. Oracle定义varchar2()类型存储汉字的长度问题

    varchar2最大是4000字节,那么就看你的oracle字符集:(select userenv('language') from dual;)如果字符集是16位编码的,ZHS16GBK,那么每个字 ...

  7. Oracle定义两个变量,并对两个变量的值的长度进行判断

    这个例子其实很简单,但是往往简单的东西如果不用心就会漏洞百出,简单的一个逻辑判断,是为了给复杂逻辑判断做出铺垫 语法格式: if<condition_expression> then pl ...

  8. Oracle定义常量和变量

    1.定义变量 变量指的就是可变化的量,程序运行过程中可以随时改变其数据存储结构 标准语法格式:<变量名><数据类型>[(长度):=<初始值>] 示例: declar ...

  9. oracle 定义临时表

    创建Oracle 临时表,可以有两种类型的临时表: 会话级的临时表 事务级的临时表 . 1) 会话级的临时表因为这这个临时表中的数据和你的当前会话有关系, 当你当前SESSION不退出的情况下,临时表 ...

随机推荐

  1. C语言头文件组织

    一.全局变量单独编写(很值得借鉴). 一般习惯将不同功能模块放到一个头文件和一个C文件中. 例如是写一些数学计算函数: //mymath.h #ifndef _mymath_H #define _my ...

  2. 【贪心+中位数】【UVa 11300】 分金币

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  3. uploadify按钮中文乱码问题

    uploadify是一款基于jQuery库的上传插件,但很可惜的是无论你怎么设置参数buttonText ,它的中文按钮都会出现乱码的情况,现把出现原因及解决方法总结如下.       那么出现这种的 ...

  4. js库开发

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  5. Appium项目搭建 For windows

    1.appium又安装了最新版本,更新了,1.4.16.1,然后整理电脑的时候发现自动更新的时候不是在原来的地方进行覆盖,所以就重新安装了一遍,注意需要看下环境变量是否配置了(用户变量:C:\Appi ...

  6. 论i++与++i

    网上看到好多人问i++与++i到底怎么理解,网友给出的答案几乎都是一样的.如下: i++:先进行计算,然后i自增1 ++i:i自增1,然后进行计算 并且课本上给出的解释跟这个也差不多,不过这样记起来既 ...

  7. js页面换行与空格

    1.换行 +'<br/>\n': 2.空格 1#JS——输出内容document.write#用于直接向 HTML 输出流写内容.简单的说就是直接在网页中输出内容.1.输出内容用“”括起, ...

  8. Http 信息头

    Http协议定义了很多与服务器交互的方法,最基本的有4种,分别是GET.POST.PUT.DELETE.一个URL地址用于描述一个网络上的资源,而HTTP中的GET.POST.PUT. DELETE就 ...

  9. 容器vector的使用总结 容器stack(栈)

    0.头文件:#include<vector>; using namespace std; 1.定义: vector<type> vec; 2.迭代器 vector<typ ...

  10. If-Modified-Since和If-None-Match

    If-Modified-Since & If-None-MatchIf-Modified-Since,和 Last-Modified 一样都是用于记录页面最后修改时间的 HTTP 头信息,只是 ...