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

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

Jun 07, 2016 pm 03:12 PM
oracleimportExportEstablishdatauserconstraint

-- : 建立一个用户,以下的操作在本用户下(例如用户名为 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
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool