oracle 定义数据完整性
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 定义数据完整性的更多相关文章
- SQL Server-数据库架构和对象、定义数据完整性(二)
前言 本节我们继续SQL之旅,本节我们如题来讲讲一些基本知识以及需要注意的地方,若有不妥之处,还望指出,简短的内容,深入的理解,Always to review the basics. 数据库架构和对 ...
- SQL Server-数据库架构和对象、定义数据完整性
前言 本节我们继续SQL之旅,本节我们如题来讲讲一些基本知识以及需要注意的地方,若有不妥之处,还望指出,简短的内容,深入的理解,Always to review the basics. 数据库架构和对 ...
- Oracle数据库 数据完整性和DML语句
数据完整性和DML语句 数据完整性 数据完整性(Data Integrity)是指数据的精确性(Accuracy) 和可靠性(Reliability).它是应防止数据库中存在不符合语义规定的数据和防止 ...
- 问题:oracle DECLARE 变量重复利用;结果:Oracle 定义变量总结
首先,当在cmd里办入scott密码提示错误时,可以这样改一下,scott的解锁命令是: 以system用户登录:cmdsqlplus system/tigertigeralter user scot ...
- Oracle 定义变量总结
首先,当在cmd里办入scott密码提示错误时,可以这样改一下,scott的解锁命令是: 以system用户登录:cmdsqlplus system/tigertigeralter user scot ...
- Oracle定义varchar2()类型存储汉字的长度问题
varchar2最大是4000字节,那么就看你的oracle字符集:(select userenv('language') from dual;)如果字符集是16位编码的,ZHS16GBK,那么每个字 ...
- Oracle定义两个变量,并对两个变量的值的长度进行判断
这个例子其实很简单,但是往往简单的东西如果不用心就会漏洞百出,简单的一个逻辑判断,是为了给复杂逻辑判断做出铺垫 语法格式: if<condition_expression> then pl ...
- Oracle定义常量和变量
1.定义变量 变量指的就是可变化的量,程序运行过程中可以随时改变其数据存储结构 标准语法格式:<变量名><数据类型>[(长度):=<初始值>] 示例: declar ...
- oracle 定义临时表
创建Oracle 临时表,可以有两种类型的临时表: 会话级的临时表 事务级的临时表 . 1) 会话级的临时表因为这这个临时表中的数据和你的当前会话有关系, 当你当前SESSION不退出的情况下,临时表 ...
随机推荐
- linux系统如何限制远程登录ip
在Linux系统上限制远程登录的IP,使用系统自带的配置文件. /etc/hosts.allow /etc/hosts.deny 匹配原则 先allow 后deny. 要求: 只允许 192.168 ...
- css设置
box-size允许您以特定的方式定义匹配某个区域的特定元素. content-box(默认):宽度和高度分别应用到元素的内容框.在宽度和高度之外绘制元素的内边距和边框.border-box:为元素设 ...
- DataGridView显示行号
//可以在DataGirdView的RowPostPaint事件中进行绘制. //如:添加以下方法代码 private void DrawRowIndex(object sender, DataGri ...
- LAMP介绍及安装
LAMP介绍及安装 1. LAMP是什么? LAMP,包含Linux + Apache + PHP + Mysql. LAMP适用环境 适用于追求极致稳定的WEB环境,缺点是需要消耗更多资源. 除了L ...
- E - Number Sequence(第二季水)
Description A single positive integer i is given. Write a program to find the digit located in the p ...
- H - Ones
Description Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a ...
- python时间函数
import time print time.time() print time.localtime() fm = '%Y-%m-%d %H:%M:%S' YMD = time.strftime(fm ...
- C语言--关键字、标识(zhi)符、注释
一.关键字 1. 关键字 是C语言中提供的有特殊含义的符号,同时也叫做保留字,在C语言中关键字一共有32个,它们分别被赋予了特殊的含义.如:main.int.struct等等. 2. 关键字的特征 1 ...
- YUI的模块化开发
随着互联网应用越来越重,js代码越来越庞大,如何有效的去组织自己的代码,变得非常重要.我们应该学会去控制自己的代码,而不是到最后一堆bug完全不知道从哪冒出来.前端的模块化开发可以帮助我们有效的去管理 ...
- 从一个Bug说开去--解决问题的思路,Linked Server, Bulk Insert, DataTable 作为参数传递
声名— 部分内容为杜撰,如有雷同,不胜荣幸! 版权所有,如要引用,请标明出处! 如果打赏,请自便! 1 背景介绍 最近一周在忙一个SQL Server 的Bug,一个简单的Bug,更新两张 ...