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. FFMPEG中最关键的结构体之间的关系

    FFMPEG中结构体很多.最关键的结构体可以分成以下几类: a)        解协议(http,rtsp,rtmp,mms) AVIOContext,URLProtocol,URLContext主要 ...

  2. web前端技术归类

    1.以屏幕可用宽和高的百分比来定义弹出框的宽和高 var trueWidth = $(top.window).width() * 0.9;var trueHeight = $(top.window). ...

  3. React-Native ListView加载图片淡入淡出效果的组件

    今天练习项目中需要给listview在加载图片时增加一个淡入淡出的效果,因此干脆就自己封装了一个组件: 'use strict' import React from 'react-native' va ...

  4. UIImageView~动画播放的内存优化

    我目前学到的知识,播放动画的步骤就是下面的几个步骤,把照片资源放到数组里面,通过动画animationImage加载数组,设置动画播放的 时间和次数完成播放. 后来通过看一些视频了解到:当需要播放多个 ...

  5. C++程序设计实践指导1.4正整数转换为字符串改写要求实现

    改写要求1:改为适合处理超长整数 #include <cstdlib> #include <iostream> #include <string> using na ...

  6. python 和 c# 连接数据库 (Access)

    模块pypyodbc 1.3.3下载:   https://pypi.python.org/pypi/pypyodbc/ 安装:解压文件找到pypyodbc.py复制到python安装目录Lib文件夹 ...

  7. 解决在IE浏览器下 boder边框出现断裂或虚线的问题

    ie6.0下面经常会出现border边框断断续续的问题,等深一步了解了div之后自然会经常碰到这种问题了,不过初学div+css 的一般不会用遇到这个问题,因为初学者不会偷懒,等我们觉得用的很熟了,各 ...

  8. PHP截取汉字乱码问题解决方法mb_substr函数的应用

    首先 1.确保你的Windows/system32下有php_mbstring.dll这个文件,没有就从你Php安装目录extensions里拷入Windows/system32里面. 2.在wind ...

  9. JDBC之一:JDBC快速入门

    (1)下载Oracle的JDBC驱动,一般放在$ORACLE_HOME/jdbc/lib目录,关于驱动的版本请见: http://elf8848.iteye.com/blog/811037 随Orac ...

  10. http://www.cnblogs.com/stephen-liu74/archive/2012/08/01/2561557.html

    http://www.cnblogs.com/stephen-liu74/archive/2012/08/01/2561557.html