1、创建表空间
--设置自动扩展autoextend on next 100M --
create tableSpace xxxxx datafile 'D:/test/test.dbf' size 1G autoextend on next 100M maxsize unlimited;
2、创建用户
--创建用户--
create user user_name identified by password default tablesapce xxxxx;
--授权--
--授予所有权限--
grant all priveges to user_name ;
--授予数据库连接权限--
grant connect to user_name ;
--授予创建session的权限--
grant session to user_name ;
--授予创建表的权限--
grant create table to user_name ;
--授予删除表的权限--
grant drop table to user_name ;
--授予插入表/更新表的权限--
grant insert/update table to user_name ;
--授予创建视图的权限--
grant create view to user_name ;
--授予创建视图的权限--
grant create view to user_name ;
--授予管理员权限--
grant dba to user_name with admin option;
3、操作数据表
--数据导出 包括lob字段--
exp 用户名/密码@IP/SID file=c:\ YWPTGL-2008-12-24.dmp(数据文件路径) owner=YWPTGL(导出该用户的所有对象)
eg:exp YWPTGL(用户名)/YWPTGL(密码)@192.9.30.33/orcl(IP/SID) file=c:\ YWPTGL-2008-12-24.dmp(数据文件路径) owner=YWPTGL(导出该用户的所有对象)
--数据导入--
imp YWPTGL(用户名)/YWPTGL(密码)@orcl(服务名) fromuser= YWPTGL(文件导出用户名) touser= YWPTGL(新表空间的用户名) file=c:\ YWPTGL-2008-12-24.dmp(数据文件路径)
--导出指定表 teachers,students--
exp demo/demo@orcl file=d:\backup2.dmp tables=(teachers,students)
--按条件过滤导出--
exp demo/demo@orcl file=d:\back.dmp tables=(table1) query=\" where filed1 like 'fg%'\"
--添加压缩--
exp demo/demo@orcl file=d:\back.dmp tables=(table1) query=\" where filed1 like 'fg%'\" compress=y
--添加日志--
exp demo/demo@orcl file=d:\back.dmp tables=(table1) query=\" where filed1 like 'fg%'\" log=d:\log.txt
--远程服务器备份--
exp 用户名/密码@远程的IP:端口/实例 file=存放的位置:\文件名称.dmp full=y
--还原到远程服务器--
imp 用户名/密码@远程的IP:端口/实例 file=存放的位置:\文件名称.dmp full=y
4、创建表
create table 表名称 (
id varchar2(50) primary key ,--设置主键
name NVCHAR2(200) not null,--中文字段用
NVCHAR2 phone number(11) unique,--唯一值字段
class carchar(10),
foreign key (name)
)
--复制表结构--
create table tableA select * from tableB where 1>1;
--复制表结构和数据--
create table table1 select * from table2;
--复制指定字段--
create table tableA as select id, name from tableB where 1>1;
5、删除数据表
--删除数据表,但可恢复--
drop table table_name
--恢复--
flashback table user_recyclebin.object_name [user_recyclebin.original_name] to before drop [rename to new_table_name];
--永久删除,也称为称为级联删除 ,何相关视图和完整性约束一并被删除--
drop table table_name purge;
--删除含有外键的表--
drop table table_name cascade constraints;
6、清除数据表数据
--清除表内容,但不能直接删除外键,需要先取消外键再删除--
truncate table table_name;
--释放表空间--
alter table table_name deallocate UNUSED KEEP 0; alter table emp deallocate UNUSED KEEP 0;
--在清除数据时,一并回收存储空间--
TRUNCATE TABLE table_name DROP(REUSE) STORAGE;
7、常用应用
--添加主键约束(将stuNo作为主键)--
alter table stuInfo add constraint PK_stuNo primary key (stuNo)
--添加外键约束 (主表stuInfo和从表stuMarks建立关系,关联字段stuNo)--
alter table stuInfo add constraint FK_stuNo foreign key(stuNo) references stuinfo(stuNo)
--添加唯一约束(身份证号唯一)--
alter table stuInfo add constraint UQ_stuID unique(stuID)
--添加默认约束(如果地址不填 默认为“地址不详”)--
alter table stuInfo
add constraint DF_stuAddress default (‘地址不详’) for stuAddress
--添加检查约束 (对年龄加以限定 15-40岁之间)--
alter table stuInfo
add constraint CK_stuAge check (stuAge between 15 and 40)
--添加表注释:学生信息表--
comment on table STUINFO
is '学生信息表';
--添加列名称:学号--
comment on column STUINFO.stuid
is '学号';
comment on column STUINFO.stuname
is '学生姓名';
8、字段类型
varcha2 ----0-4000,可变长度
char() ----0-2000,固定长度,用空格在数据的右边补到固定长度
number(6,2) ---6位整数、2位小数
number(2) --2位整数
clob ---txt文本
blob ---图片、视频、声音等转换过来的二进制对象
date ---sysdate