一、数据库基础知识:
1、数据库三级模式结构,它包括外模式、模式和内模式。
在一个数据库系统中,『模式』与『内模式』都只能有一个,但『外模式』可以有很多个。
『外模式』:视图,模式的子集。(也叫用户模式)外模式DDL 『视图』view
『模式』:定义模式时要定义数据逻辑结构、数据项构成、数据项的名称、类型、取值范围等,还要定义数据之间的联系、定义与诗句有关的安全性、完整性要求。(也叫逻辑模式)schema 模式DDL 『表』table
『内模式』:数据物理结构和存储方式的描述(也叫存储模式)DSDL 『索引』index 加快查询速度
一个用户一般对应一个schema,该用户的schema名等于用户名,并作为该用户缺省schema。这也就是我们在企业管理器的方案下看到schema名都为数据库用户名的原因。Oracle数据库中不能新创建一个schema,要想创建一个schema,只能通过创建一个用户的方法解决。
2、关系数据库标准语言SQL:集数据定义语言DDL、数据操纵语言DML、数据控制语言DCL的功能于一体。
数据查询:select
DDL关键词:create、alter、drop
DML关键词:insert、update、delete
DCL关键词:grant、revoke
3、Oracle中的类型有很多种,主要可以分为以下几类:
字符串类型。如:char、nchar、varchar2、nvarchar2。
数值类型。如:int、number(p,s)、integer、smallint。
日期类型。如:date、interval、timestamp。
PL/SQL类型。如:pls_integer、binary_integer、binary_double(10g)、binary_float(10g)、boolean。plsql类型是不能在sql环境中使用的,比如建表时。
自定义类型:type / create type。
常用: f_org_code VARCHAR2(n) default 0 not null--非空 默认0
f_id NUMBER(n) not null
f_create_time TIMESTAMP(6) default sysdate
4、表的操作
create table tongxue
(
s_id VARCHAR2(12) not null primary key,--主键
name VARCHAR2(12) not null,
sex VARCHAR2(32) ,
score NUMBER(12) not null
);--建表table
comment on column tongxue.s_id is '学号';--comment
4、数据查询
--左连接:左边有的,右边没有的为null
select * from student left join student_class on student.s_id=student_class.s_id;
--右连接:左边没有的,右边有的为null
select * from student right join student_class on student.s_id=student_class.s_id;
--内连接:显示左边右边共有的
select * from student a,student_class b where a.s_id=b.s_id;
并union:
select * from student where sex='女'
union
select * from student where sex='男';
交intersect:
select * from student where sex='女' and score>'80';--and
select * from student where sex='女' intersect select * from student where score>'80';--intersect
排序
select * from Student order by score desc;--降序
select * from Student order by score asc;--升序
去重
delete from Student where s_id in (select s_id from Student group by s_id having count(s_id)>1)
and rowid not in (select min(rowid) from Student group by S_ID having count(S_ID)>1) ;--去重,保留最小行号
select * from student;---学生信息表s_id
select * from class;--班级信息表c_id
select * from student_class;--学生-班级表s_id、c_id
--1、查询一班得分在70分以上的学生
select * from Student where score>70 and s_id in (select s_id from student_class where c_id=(select c_id from class where c_name='101班') );
5、数据更新
insert into tongxue (S_ID, SEX, SCORE, NAME) values ('0801080201', '女', '81', '张1');--新增
delete from tongxue where s_id='0801080203';--删除
select * from tongxue;--查询
update tongxue set s_id='0801080201' where s_id='0801080204';--修改
drop table tongxue;--删表
列、类型:
alter table tongxue2 drop column name;--删除一列
alter table tongxue2 add( name VARCHAR2(12) );--新增一列
alter table tongxue2 rename column name to newname; --修改列名
alter table tongxue2 modify name varchar(25); --修改列的类型
6、视图:虚表
索引:加快查询速度
一、数据库建库
1、数据库脚本从SVN取到本地;
2、新增用户+赋予权限
create user 用户名 identified by 密码;
grant connect,resource,dba to 用户名;
3、建表顺序如下:
create_table.sql --建表;
create_sequence.sql --建序列;
create_type.sql--建类型;
4、初始化数据init
5、存储procedure和视图view
6、增量脚本
二、导出:服务器103上的数据库c##lcp_002导出命名为c##lcp_002copy
su - oracle
cd /home/oracle
expdp c##lcp_002/test123@orcl schemas=c##lcp_002 dumpfile=c##lcp_002copy.dmp
导出后目录文件:/home/u01/app/oracle/admin/orcl/dpdump/c##lcp_002copy.dmp
三、导入:将数据库c##lcp_002copy.dmp上传到103服务器命名为c##lcp_003.dmp
在目录 /home/u01/app/oracle/admin/orcl/dpdump上传dmp文件,
这里直接用刚刚导出数据库:cp c##lcp_002copy.dmp c##lcp_003.dmp
建新库:create user C##LCP_003 identified by test123; grant connect,resource,dba to C##LCP_003;
impdp C##LCP_003/test123@orcl remap_schema=C##LCP_002:C##LCP_003 dumpfile=c##lcp_003.dmp transform=OID:N