【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操作,而修改物理数据。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

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.

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

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.
