搜索
首页数据库mysql教程【Oracle篇】约束和数据泵导入导出

【Oracle篇】约束和数据泵导入导出

Jun 07, 2016 pm 03:12 PM
oracle导入导出建立数据用户约束

-- : 建立一个用户,以下的操作在本用户下(例如用户名为 test) create user test identified by test; -- : 进入sys用户,分别在test下建立emp和dept表 create table test.emp as select * from scott.emp; create table test.dept as select * from scott.


-- : 建立一个用户,以下的操作在本用户下(例如用户名为  test)
create user test identified by test;

-- : 进入sys用户,分别在test下建立emp和dept表
 create table test.emp as select * from scott.emp;
 create table test.dept as select * from scott.dept;

--  使用命令给上题的表建立上主外键
alter table emp add constraints pk_empno primary key(empno);
alter table dept add constraints pk_deptno primary key(deptno);
alter table emp add constraints fk_de_em  foreign key(deptno) references dept(deptno);

-- 导出这个用户
--1、连接Oracle数据库
SQL> conn / as sysdba

--2、创建一个操作目录
SQL> create directory chenfeng_dir as 'D:\北民2014\7.1\dump';

--注意同时需要使用操作系统命令在硬盘上创建这个物理目录。

--目录已创建。

--3、分配目录对象my_dir使用权限,给需要导出的用户
SQL>grant read,write on directory chenfeng_dir to chenfeng;

C:\>expdp chenfeng/chenfeng directory=chenfeng_dir dumpfile=20140701_schema_chenfeng.dmp schemas=chenfeng logfile=chenfeng.log;

--: 删除这个用户
SQL> drop user chenfeng cascade;

--: 导入这个用户看看是否成功。
-- 用户的导入 ----------
--1、连接Oracle数据库
SQL> conn / as sysdba

--2、创建一个导入操作目录
SQL> create directory chenfeng_dir as 'D:\北民2014\7.1\dump';

--3、创建用户,并分配权限
SQL> create user chenfeng identified chenfeng;
SQL> grant connect,resource to chenfeng;
SQL> grant read,write on directory chenfeng_dir to chenfeng;

--4、导入用户
C:\>impdp chenfeng/chenfeng  directory=chenfeng_dir dumpfile=20140701_schema_chenfeng.dmp schemas=chenfeng;

--------------------------------------------

-- 熟悉对约束操作的命令:
--建立列时、建立表时间、建立表后。

-- 一、主键加约束
--1-建立列时
create table student(
     sno number primary key,
     sname varchar2(20)
);
drop table student;

--2-建立表时间
create table student(
     sno number,
     sname varchar2(20),
     primary key(sno)
);

--3-建立表后。
create table student(
     sno number,
     sname varchar2(20)
);
alter table student add constraints pk_s primary key(sno);

-- 二、外键加约束
--1-建立列时
drop table teacher;

create table teacher(
     tno number  references student(sno),
     tname varchar2(20), 
     primary key(tno)
);

--2-建立表时间
create table teacher(
     tno number,
     tname varchar2(20),
     constraints  fk_t foreign key(tno) references student(sno)
);

--3-建立表后。
create table teacher(
     tno number,
     tname varchar2(20)
);
alter table teacher add constraints  fk_t foreign key(tno) references student(sno);

-- 二、非空加约束
--1-建立列时
drop table teacher;

create table teacher(
     tno number,
     tname varchar2(20) not null
);

--2-建立表后
create table teacher(
     tno number,
     tname varchar2(20)
);
alter table teacher modify  tname not null;

-- 二、独立加约束
--1-建立列时

drop table teacher;

create table teacher(
     tno number unique,
     tname varchar2(20)
);

--2-建立表时间
create table teacher(
     tno number,
     tname varchar2(20),
     constraints  uq_t unique(tno)
);

--2-建立表后
create table teacher(
     tno number,
     tname varchar2(20)
);
alter table teacher modify  tname unique;


-- 3.5、检查约束:用户表列数据必须要满足的自定义条件。

-- a、定义列的时候定义:
create table tab(
       tid number,
       tage number constraint ck_tage check(tage>=18)
);
-- b、定义表的时候定义:
create table tab(
       tid number,
       tsex number(1),
       constraint ck_tage check(tsex in(0,1))
);
-- c、定义表后定义:
create table tab(
       tid number,
       tsex number(1));
       
alter table tab add constraint ck_tage check(tsex in(1,0));



-- 查看约束
select constraint_name,constraint_type 
    from user_constraints where table_name='EMP';

-- 外键约束的“级联删除”、“级联置空” 设置

create table dept2 as select * from dept;
alter table dept2 add constraint pk_dept2 primary key(deptno);
create table emp2 as select *from emp;
alter table emp2 add constraint pk_emp2 primary key(empno);

-- 删除约束
alter table emp2 drop constraint fk_e2_d2;

-- 设定级联删除外键约束
alter table emp2 add constraint fk_e2_d2 foreign key(deptno) references dept2(deptno) on delete cascade;
-- 设定级联置空外键约束
alter table emp2 add constraint fk_e2_d2 foreign key(deptno) references dept2(deptno) on delete set null;

delete dept2 where deptno=20;



-- 4、视图 -------------------------

-- 需要分配给scott用户视图权限
grant create view to scott;
-- 视图:没有物理数据,仅仅捆绑了一个查询的虚拟数据源。

-- 建立视图:
create or replace view v_dept as select * from dept;


update dept set dname=lower(dname);


-- 视图的分类:
-- 1 简单视图:子查询仅仅涉及一张表,并且没有数据变型。
--             视图数据可以对应到物理数据的ROWID

-- 简单视图,可以通过对视图做DML操作,而修改物理数据。

update v_dept set dname=upper(dname);

-- 2 复杂视图:子查询涉及多张表,或者有数据变型。

-- 复杂视图,不能通过对视图做DML操作,而修改物理数据。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
MySQL的许可与其他数据库系统相比如何?MySQL的许可与其他数据库系统相比如何?Apr 25, 2025 am 12:26 AM

MySQL使用的是GPL许可证。1)GPL许可证允许自由使用、修改和分发MySQL,但修改后的分发需遵循GPL。2)商业许可证可避免公开修改,适合需要保密的商业应用。

您什么时候选择InnoDB而不是Myisam,反之亦然?您什么时候选择InnoDB而不是Myisam,反之亦然?Apr 25, 2025 am 12:22 AM

选择InnoDB而不是MyISAM的情况包括:1)需要事务支持,2)高并发环境,3)需要高数据一致性;反之,选择MyISAM的情况包括:1)主要是读操作,2)不需要事务支持。InnoDB适合需要高数据一致性和事务处理的应用,如电商平台,而MyISAM适合读密集型且无需事务的应用,如博客系统。

在MySQL中解释外键的目的。在MySQL中解释外键的目的。Apr 25, 2025 am 12:17 AM

在MySQL中,外键的作用是建立表与表之间的关系,确保数据的一致性和完整性。外键通过引用完整性检查和级联操作维护数据的有效性,使用时需注意性能优化和避免常见错误。

MySQL中有哪些不同类型的索引?MySQL中有哪些不同类型的索引?Apr 25, 2025 am 12:12 AM

MySQL中有四种主要的索引类型:B-Tree索引、哈希索引、全文索引和空间索引。1.B-Tree索引适用于范围查询、排序和分组,适合在employees表的name列上创建。2.哈希索引适用于等值查询,适合在MEMORY存储引擎的hash_table表的id列上创建。3.全文索引用于文本搜索,适合在articles表的content列上创建。4.空间索引用于地理空间查询,适合在locations表的geom列上创建。

您如何在MySQL中创建索引?您如何在MySQL中创建索引?Apr 25, 2025 am 12:06 AM

toCreateAnIndexinMysql,usethecReateIndexStatement.1)forasingLecolumn,使用“ createIndexIdx_lastNameEnemployees(lastName); 2)foracompositeIndex,使用“ createIndexIndexIndexIndexIndexDx_nameOmplayees(lastName,firstName,firstName);” 3)forauniqe instex,creationexexexexex,

MySQL与Sqlite有何不同?MySQL与Sqlite有何不同?Apr 24, 2025 am 12:12 AM

MySQL和SQLite的主要区别在于设计理念和使用场景:1.MySQL适用于大型应用和企业级解决方案,支持高性能和高并发;2.SQLite适合移动应用和桌面软件,轻量级且易于嵌入。

MySQL中的索引是什么?它们如何提高性能?MySQL中的索引是什么?它们如何提高性能?Apr 24, 2025 am 12:09 AM

MySQL中的索引是数据库表中一列或多列的有序结构,用于加速数据检索。1)索引通过减少扫描数据量提升查询速度。2)B-Tree索引利用平衡树结构,适合范围查询和排序。3)创建索引使用CREATEINDEX语句,如CREATEINDEXidx_customer_idONorders(customer_id)。4)复合索引可优化多列查询,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。5)使用EXPLAIN分析查询计划,避

说明如何使用MySQL中的交易来确保数据一致性。说明如何使用MySQL中的交易来确保数据一致性。Apr 24, 2025 am 12:09 AM

在MySQL中使用事务可以确保数据一致性。1)通过STARTTRANSACTION开始事务,执行SQL操作后用COMMIT提交或ROLLBACK回滚。2)使用SAVEPOINT可以设置保存点,允许部分回滚。3)性能优化建议包括缩短事务时间、避免大规模查询和合理使用隔离级别。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。