oracle建表唯一约束语句,oracle 详细建表语句

oracle建表唯一约束语句,oracle 详细建表语句1.创建一张表格createtabletest(idnumber,namevarchar2(20),class_namevarchar2(20));1.1创建表格的时候直接创建主外键createtabletest(idnumberprimarykey,namevarchar2(20)constraintt_fkreferencestable_name(column_n…

大家好,欢迎来到IT知识分享网。

1.创建一张表格

create table test(id number,name varchar2(20),class_name varchar2(20));

1.1创建表格的时候直接创建主外键

create table test(id number primary key,name varchar2(20) constraint t_fk references table_name(column_name));

create table test(id number,name varchar2(20)

constraint pk primary key(id),

constraint fk foreign key(id) references 关联表(关联表主键);

–增加主键

alter table test add constraint constraint_name primary key(column_name);

–增加外键

alter table test add constraint constraint_name foreign key(column_name) references 关联表(column_name);–这里需要注意一个表的外键必须是另一个表的主键

–增加唯一索引

create unique index index_name on table_name(column_name);

–修改列的属性

alter table test modify column_name varchar2(10);–如果以前column_name varchar2(20);

–修改表的名称

rename table_name to table_name;

–为列设定默认值

alter table test modify name varchar2(20) default ‘hello’);–这个不会影响已经插入的数据只会对新数据造成影响

create table test (id number default 2);

–增加check

alter table test add constraint ck check ((id>20) and (id <30));

alter table test add constraint ck check(name in (‘hello’,’world’);

–unique

alter table test add constraint cuq unique(id);

–修改列的默认值

alter table test modify id default sysdate+1;

–添加列

alter table test add class varchar2(20);

alter table test add user varchar2(20) unique not null;

–删除列

alter table test drop column class;

–对表添加注释

commont on table test is ‘测试表’;

–对列添加注释

commont on column table.id is ‘主键’;

–删除表

drop table test;

–清除表的数据

truncate table test;

delete from test;

truncate操作与delete操作对比区别

操作

回滚

高水线

空间

效率

Truncate

不能

下降

回收

delete

可以

不变

不回收

也就是说我们delete掉一个表格时其表格所占用的表空间时不会释放的。

–禁用约束

alter table test disable constraint constraint_name;

–启用约束

alter table test enable constraint constraint_name;

–延迟约束

alter table test drop constraint constraint_name;

alter table test add constraint constraint_name foreign key(id) references table_name(column_name)deferrable initially deferred;deferrable的两个选项区别

deferrable表示该约束是可延迟验证的.它有两个选项:

Initially immediate(默认):立即验证,执行完一个sql后就进行验证;

Initially deferred:延迟验证,当事务提交时或调用set constraint[s] immediate语句时才验证.

区别是:事务提交时验证不通过,则立即回滚事务; set constraint[s] immediate时只验证,不回滚事务.

notdeferrable与deferrable区别

区别就在于: “立即验证的可延迟约束”是可以根据需要设置成“延迟验证的可延迟约束”的,而“不可延迟验证”是不能改变的.

最后我们如果想查看我们对test表格建立了那些约束以及约束所对应的列

可以通过查看user_constraints and user_cons_columns;

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/23189.html

(0)
上一篇 2023-09-17 13:33
下一篇 2023-09-18 14:45

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信