Home Database Mysql Tutorial 【Oracle篇】约束和数据泵导入导出

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

Jun 07, 2016 pm 03:12 PM
oracle import Export Establish data user constraint

-- : 建立一个用户,以下的操作在本用户下(例如用户名为 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操作,而修改物理数据。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to export oracle view How to export oracle view Apr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

See all articles