Oracle 复习 约束

创建表:create table 表名(字段属性)

create table student (
sno number(3),
sname varchar2(30),
studytime date,

);

修改表

(1)添加列

Alter table 表名 add 列名 列类型 default 值,……
alter table student add score number(4,1) DEFAULT 60;

(2)修改列名(修改宽度,不建议做)

Alter table 表名 rename column 旧列名 to 新列名
alter table student rename column score to grade;

(3)删除列

Alter table 表名 drop column 列名
alter table student drop column grade;

(4)修改表名

Rename 旧表名 to 新表名
rename student to ss;

(5)删除表

Drop  table 表名
drop table student;

约束:

约束就是保证数据库完整性的方法,数据库的完整性就是为了保证数据的正确性。
概念: 在数据表上强制执行的一些数据校验规则。当执行增、删、改操作时,数据必须符合在相关字段上设置的校验规则 作用:保证数据的准确性
内容: 非空、唯一、主键、外键和检查
分类:列级约束\表级约束

primary key: 主键 -要求主键列数据唯一,且不能为空,可以是单字段或多字段组合
unique :要求该列唯一,可以为空,但不能出现一个空值
foreign key: 用于两表间建立连接,需要指定引用主表的哪一列
check : 某列取值范围限制,格式限制等,如有关年龄的约束
not null: 非空。
default : 默认约束,就是默认值。
创建约束:
建表时:直接加在列名后面(+add constraint 约束名 约束类型(字段名)
)
建表后:使用alter table 表+add constraint 约束名 约束类型(字段名)
建表同时创建列级主键约束,由系统自动赋予约束条件名称

create table book(bid number(4) primary key,title varchar2(20));
create table usertable1(userid number(3),username varchar2(10),salary number(7,2) 
check (salary>800));

Create table student(sid number(4),user_name varchar2(10),
constraint student_sid_pk primary key(sid));
alter table SYSTEM.student add constraints pk_student primary key(sno);
alter table SYSTEM.student add constraints unq_student unique(sname);

添加列时指定约束

alter table student add salary number(5,2) unique;

或使用修改方式添加约束
Alter table 表名 modify 字段名 约束类型

alter table book modify(title NOT null);
alter table book modify(title varchar(2) NOT null);

可以在多列上建立约束,必须是表级约束。

alter table student add(ri varchar2(4),qq varchar2(3));
alter table student add constraint ss unique(ri,qq);

1、列级约束:作用于一个列,定义在列的后面,可以有多个约束,用空格隔开,有六种可定义的约束
2、表级约束:作用于一个列或者多列,定义在表的后面,用逗号隔开,有四种可定义的约束(主键、外键、唯一、检查)
列级约束例子:

  create table student (
  stuno number(3) constraint PK_stuno primary key,
  stuname varchar2(10) constaint UQ_stuname unique constraint NN_stuname not null,
  stuaddress varchar2(20) default '广东省广州市'  constraint CK_stuaddress check(length(stuaddress)>2);
  );

表级约束例子:

   create table student2(
  stuno number(3) ,
  stuname varchar2(10) ,
  stuaddress varchar2(20) ,
  stubid number(3),
  constraint PK_sno primary key(stuno) ,
  constraint UQ_sname_subid unique(stuname,stubid),
  constraint CK_saddress check( length(stuAddress)>2)
  );

默认值约束
创建表时创建:

Create table table_name(columnu data_type default value);

修改表时创建:

Alter table table_name modify column data_type defaule value;

创建外键约束:

(1)主表要有主键:
(2)子表上对应主表主键的列建立外键约束
(3)验证:
不能删除主表上的值(如果子表使用了这个值)
子表中也不能插入主表主键没有的值
子表中可以插入空值(主表主键上对应的列)

第一种:

create table books(
    bookid number(10) not null primary key,
    bookName varchar2(20) not null,
    price number(10,2),
    categoryId number(10) not null references Category(id)  --外键约束
);

第二种:

create table books(
    bookid number(10) not null primary key,
    bookName varchar2(20) not null,
    price number(10,2),
    categoryId number(10) not null
);

ALTER TABLE  books 
ADD CONSTRAINT FK_Book_categoryid 
FOREIGN KEY(categoryId ) REFERENCES Category(id);

在数据字典中查看约束信息

select owner,constraint_name,constraint_type from user_constraints
where table_name='STUDENT';

删除操作中关键字on的使用

在创建约束时指定
(1)禁止删除,这是oracle默认的。
(2)on delete casecade:用于指定级联删除选项。
(3)on delete set null:用于删除主表时,将从表的外键列的值设置为null

约束的启用与禁用

Alter table 表名 disable|enable constraint constraint_name [cascade]

先查看约束:select table_name,constraint_name from user_constraints;
禁用:alter table student disable constraint 约束名
验证:
重新启用: alter table student enable constraint student_sid_pk;

修改约束名称

alter table 表名 rename constraint旧名 to 新名

删除约束

alter table 表名 drop constraint 约束名
Alter table 表名 drop primary key
Alter table表名 drop unique(字段名)
Alter table 表名 modify 字段名 数据类型 null;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值