Oracle数据库-建库、建表空间,建用户

本文详细介绍如何在Oracle数据库中创建数据库、表空间及用户,并通过实例演示如何创建表及设置约束条件,最后介绍了如何使用SQL*Loader进行数据导入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    Oracle安装完后,其中有一个缺省的数据库,除了这个缺省的数据库外,我们还可以创建自己的数据库。

    对于初学者来说,为了避免麻烦,可以用'Database Configuration Assistant'向导来创建数据库。

    创建完数据库后,并不能立即在数据库中建表,必须先创建该数据库的用户,并且为该用户指定表空间。

    下面是创建数据库用户的具体过程:

 

    如下功能完成是在:sqlplusw 下完成的;

 

    1.假如现在已经建好名为'news'的数据库,此时在F:\oracle\product\10.1.0\oradata\目录下已经存在news目录(注意:我的Oracle10g安装在F:\oracle下,若你的Oracle安装在别的目录,那么你新建的数据库目录就在*\product\10.1.0\oradata\目录下)。

 

    2.在创建用户之前,先要创建表空间:

    其格式为:格式:  create tablespace 表间名 datafile '数据文件名' size 表空间大小;

    如:

    SQL> create tablespace news_tablespace datafile 'F:\oracle\product\10.1.0\oradata\news\news_data.dbf' size 500M;

    其中'news_tablespace'是你自定义的表空间名称,可以任意取名;'F:\oracle\product\10.1.0\oradata\news\news_data.dbf'是数据文件的存放位置,'news_data.dbf'文件名也是任意取;'size 500M'是指定该数据文件的大小,也就是表空间的大小。

 

    3.现在建好了名为'news_tablespace'的表空间,下面就可以创建用户了:

    其格式为:格式:  create user  用户名 identified by 密码  default tablespace 表空间表;

    如:

    SQL> create user news identified by news default tablespace news_tablespace;

    默认表空间'default tablespace'使用上面创建的表空间。

 

    4.接着授权给新建的用户:

    SQL> grant connect,resource to news;  --表示把 connect,resource权限授予news用户

    SQL> grant dba to news;  --表示把 dba权限授予给news用户

    授权成功。

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

 

 

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

 

2007-12-12 09:39

1.-- 连接

conn hu/aaa;

-- 创建表空间

Create tablespace computer2005 nologging datafile 'd:\oracle\product\10.2.0\oradata\test\computer.dbf' size 50m blocksize 8192 extent management local uniform size 256k segment space management auto;

-- 创建学生基本信息表

create table student(学号 varchar2(20 byte) not null,

                          姓名 varchar2(8 byte),

                          性别 varchar2(4 byte),

                          民族 varchar2(8 byte),

constraint pk_stud primary key(学号) using index tablespace computer2005

pctfree 10 initrans 2 maxtrans 255);

-- 创建课程信息表

create table course(课程编号 varchar2(5 byte) not null,

                      课程类型 varchar2(6 byte),

                      课程名 varchar2(20 byte),

                      周学时 integer,

                      任课教师 varchar2(10 byte),

                      开设学期 integer,

                      考核方式 varchar2(4 byte),

constraint pk_cour primary key(课程编号) using index tablespace computer2005

pctfree 10 initrans 2 maxtrans 255);

-- 创建学生成绩表

create table score(学号 varchar2(20 byte) not null,

                      课程编号 varchar2(5 byte) not null,

                      成绩 number,

constraint pk_sc primary key(学号,课程编号)

using index tablespace computer2005 pctfree 10 initrans 2 maxtrans 255);

-- 添加外键

alter table score add constraint fk_stud_score foreign key(学号) references student(学号);

alter table score add constraint fk_cour_score foreign key(课程编号) references course(课程编号);

--移动表到工作表空间

alter table student move tablespace computer2005;

alter table course move tablespace computer2005;

alter table score move tablespace computer2005;

-- 使用SQL loader导入数据

host sqlldr hu/aaa control=d:\oracle\insert4.ctl log=d:\oracle\4.log

host sqlldr hu/aaa control=d:\oracle\insert5.ctl log=d:\oracle\5.log

host sqlldr hu/aaa control=d:\oracle\insert6.ctl log=d:\oracle\6.log

2.-- 创建用户

create user hu identified by aaa;

-- 赋予权限

grant dba,connect to hu with admin option;

-- 连接

conn hu/aaa;

-- 创建表空间

create tablespace student_app nologging datafile 'd:\oracle\product\10.2.0\oradata\test\student.dbf' size 50m blocksize 8192 extent management local uniform size 256k segment space management auto;

-- 创建学生基本信息表

create table 学生基本信息(学号 varchar2(20 byte) not null,

                          姓名 varchar2(8 byte),

                          性别 varchar2(4 byte),

                          民族 varchar2(8 byte),

constraint pk_baseinfo primary key(学号) using index tablespace student_app

pctfree 10 initrans 2 maxtrans 255);

-- 创建课程信息表

create table 课程信息(课程编号 varchar2(5 byte) not null,

                      课程类型 varchar2(6 byte),

                      课程名 varchar2(20 byte),

                      周学时 integer,

                      任课教师 varchar2(10 byte),

                      开设学期 integer,

                      考核方式 varchar2(4 byte),

constraint pk_course primary key(课程编号) using index tablespace student_app

pctfree 10 initrans 2 maxtrans 255);

-- 创建学生成绩表

create table 学生成绩(学号 varchar2(20 byte) not null,

                      课程编号 varchar2(5 byte) not null,

                      成绩 number,

constraint pk_grade primary key(学号,课程编号)

using index tablespace student_app pctfree 10 initrans 2 maxtrans 255);

-- 添加外键

alter table 学生成绩 add constraint fk_info_grade foreign key(学号) references 学生基本信息(学号);

alter table 学生成绩 add constraint fk_course_grade foreign key(课程编号) references 课程信息(课程编号);

--移动表到工作表空间

alter table 学生基本信息 move tablespace student_app;

alter table 课程信息 move tablespace student_app;

alter table 学生成绩 move tablespace student_app;

-- 使用SQL lpader导入数据

host sqlldr hu/aaa control=d:\oracle\insert1.ctl log=d:\oracle\1.log

host sqlldr hu/aaa control=d:\oracle\insert2.ctl log=d:\oracle\2.log

host sqlldr hu/aaa control=d:\oracle\insert3.ctl log=d:\oracle\3.log

 

 

(请您对文章做出评价)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值