栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

oracle 数据库12c 9.24知识点整理

oracle 数据库12c 9.24知识点整理

oracle 数据库12c 9.24知识点整理

约束(完整性约束条件)
作用:保证数据的准确性
内容:非空、唯一、主键、外键和检查
分类:列级约束、表级约束

1、非空约束
含义:除了默认情况下,其他情况字段值不为空
实现语句:
(1)建表时创建非空约束
create table book(bid number(4) not null,
title varchar2(20)
);

(2)建表后创建
alter table book modify(title not null);
或者alter table book modify(title varchar(2) not null);

(3)添加列创建
alter table book add author varchar2(10) not null;

注意:创建非空约束之前确定数据是否已经存在null值。

2、唯一约束
含义:某一列的值不允许重复,允许值为空值
(1)建表时创建
create table student(
sid number(4) constraint student_sid_nn not null,
user_name varchar2(10),
email varchar2(20),constraint student_email_uk unique(email)
);
(2)建表后创建
alter table student add salary number(5,2) unique;

(3)添加列时创建
alter table student add salary number(5,2) unique;

3、主键约束
操作:
建表:
create table customers(
c_num number(11) not null,
c_name varchar(50),
c_contact varchar(50),
c_city varchar(50),
c_birth date,
constraint unq_number unique(c_num)
);

(1)在c_num字段上添加主键约束
alter table customers add constraint pk_c_num primary key(c_num);(约束名尽量规范命名,保证完整性)
//修改后:
alter table customers add constraint customers_c_num_pk primary key(c_num);、
(2)建表同时创建约束
create table customers(
c_num number(11) primary key,
c_name varchar(50),
c_contact varchar(50),
c_city varchar(50),
c_birth date,
constraint unq_number unique(c_num)
);

4、外键约束

一、外键约束(即引用完整性约束)

例:有如下两张表他们之间的引用完整性

create table student (–学生表

s_id number(5) primary key,

s_name varchar2(20),

t_id varchar2(5),

constraint fk_stu foreign key (t_id) references team (t_id)

);

create table team (–班级表

t_id varchar2(5) primary key,

t_name varchar2(20)

);

其中constraint fk_stu foreign key (t_id) references team (t_id)中,第一个t_id指的是学生表中的t_id,第二个t_id指的是班级表中的t_id
外键约束:是一种子父关系,且可以很好的保护两表之间的关系。

5、检查约束

1. 检查约束是什么
检查约束的作用就是让表中的数据更有意义。检查约束就是为了检测和过滤不符合实际意义的数据,比如说年龄1000,工资-500等这些数据。

2.在创建表时设置检查约束
1)列级检查约束
语法:
CREATE TABLE table_name ( column_name datatype CHECK(expressions)) ;

CHECK 是检查关键字
expressions 是约束的条件
实例:
将salary 设置检查约束,使其>0 。

create table userinfo_c(
id varchar2(10) primary key ,
username varchar2(20) ,
salary number(5,0) check(salary>0)) ;

执行
先执行实例,插入语句当 salary<0 的时候报错。

SQL> create table userinfo_c(
2 id varchar2(10) primary key ,
3 username varchar2(20) ,
4 salary number(5,0) check(salary>0)) ;

表已创建。

SQL> insert into userinfo_c values(1,‘shao’-500) ;
insert into userinfo_c values(1,‘shao’-500)
*
第 1 行出现错误:
ORA-00947: 没有足够的值
SQL>

2)表级级检查约束
语法

ConSTRAINT constraint_name CHECK(expressions)

实例

create table userinfo_c1(
id varchar2(10) primary key ,
username varchar2(20),
salary number(5,0),
constraint ck_salary check(salary>0) );

总结:
1、创建约束时候命名约束名要规范
2、constraint关键字出现时,必须有一个约束名称,或者都不写上。
3、如何修改字段名、字段类型:


oracle 约束练习:

建立表customers,表内容如下

create table customers(
c_num number(11) not null,
c_name varchar(50),
c_contact varchar(50),
c_city varchar(50),
c_birth date,
//constraint unq_number unique(c_num) 创建表时添加唯一约束
);

**1、**在c_num字段上添加主键约束,在c_birth字段上添加非空约束
alter table customers add constraint pk_c_num primary key(c_num);(约束名尽量规范命名,保证完整性)

//修改后:
alter table customers add constraint customers_c_num_pk primary key(c_num);
//删除唯一约束(下一步再添加主键约束):
alter table customers drop constraint unq_number;

//alter table customers modify(c_birth not null);

2、将c_name字段类型修改为varchar(70)
//修改字段类型的语句:
alter table customers modify(c_name varchar(70));

3、将c_contact字段改名为c_phone
//修改字段的语句:
alter table customers rename column c_contact to c_phone;

4、增加c_gender字段,数据类型为char(1)
//alter table customers add c_gender char(1);

5、将表名修改为customers_info
//rename customers to customers_info

6、删除字段c_city
//alter table customers_info drop column c_city;

7、对c_birth设置检查约束,出生日期必须是2000年以后
//alter table customers_info add constraint customers_c_birth_ck check (c_birth>2000);
//修改后
alter table customers_info add constraint customers_c_birth_ck check (c_birth>to_date(‘2000-01-01’,‘yyyy-mm-dd’)); (注意把表名修改为customers_info)
//删除检查约束
alter table customers_info drop constraint customers_c_birth_ck;

8、在c_name字段上添加唯一约束
//alter table customers_info add constraint customers_c_name_unq unique(c_name);(注意把表名修改为customers_info)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286048.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号