今天要介绍的是在oracle中建表。

先用管理员权限创建一个表空间:

create tablespace hellen_space datafile ‘/opt/oracle/oradata/orcl/hellen_space01.dbf’ size 20m;
查看创建的表空间:

[oracle@mophee orcl]$ cd /opt/oracle/oradata/orcl/
 [oracle@mophee orcl]$ ls
 control01.ctl redo01.log sysaux01.dbf undotbs01.dbf
 example01.dbf redo02.log system01.dbf users01.dbf
 hellen_space01.dbf redo03.log temp01.dbf


创建表:

–创建序列
 create sequence seq_t_student_info start with 1 minvalue 1 maxvalue 999999999;
 –创建表,同时建分区
 create table t_student_info(
 t_id varchar2(32) not null,
 stu_no varchar2(32),
 stu_name varchar2(32),
 stu_age number(3),
 born_date date,
 create_date date,
 constraint pk_t_student_info_id primary key (t_id)
 )
 partition by range(create_date)
 interval (numtoyminterval(1,‘MONTH’))
 store in (hellen_space)
 (
 partition t20171223 values less than (to_date(‘2017-12-01’,‘yyyy-mm-dd’)) tablespace hellen_space
 );
 –创建索引
 create index idx_student_info_001 on t_student_info(stu_no) local;
 create index idx_student_info_002 on t_student_info(create_date) local;
 –列注释
 comment on table t_student_info is ‘学生信息表’;
 comment on column t_student_info.t_id is ‘主键,自增’;
 comment on column t_student_info.stu_no is ‘学生编号’;
 comment on column t_student_info.stu_name is ‘学生姓名’;
 comment on column t_student_info.stu_age is ‘学生年龄’;
 comment on column t_student_info.born_date is ‘学生出生日期’;
 comment on column t_student_info.create_date is ‘创建日期’;删除表及删除索引:
 drop sequence seq_t_student_info;
 drop table t_student_info;

插入数据:

insert into scott.t_student_info
 values(
 scott.seq_t_student_info.nextval,
 ‘11094201’,
 ‘hellen’,
 20,
 to_date(‘1997-12-23’,‘yyyy-mm-dd’),
 sysdate
 )

表建完后的效果:
列注释

索引:

分区:

以下来说明上述创建语句的意思:

1、创建序列

1) 建立序列命令

CREATE SEQUENCE [user.]sequence_name
 [increment by n]
 [start with n]
 [maxvalue n | nomaxvalue]
 [minvalue n | nominvalue];


INCREMENT BY: 指定序列号之间的间隔,该值可为正的或负的整数,但不可为0。序列为升序。忽略该子句时,缺省值为1。

START WITH:指定生成的第一个序列号。在升序时,序列可从比最小值大的值开始,缺省值为序列的最小值。对于降序,序列可由比最大值小的值开始,缺省值为序列的最大值。

MAXVALUE:指定序列可生成的最大值。

NOMAXVALUE:为升序指定最大值为1027,为降序指定最大值为-1。

MINVALUE:指定序列的最小值。

NOMINVALUE:为升序指定最小值为1。为降序指定最小值为-1026。

2) 更改序列命令

ALTERSEQUENCE [user.]sequence_name
[INCREMENT BY n]
[MAXVALUE n| NOMAXVALUE ]
[MINVALUE n | NOMINVALUE];

修改序列可以:

  1. 修改未来序列值的增量。
  2. 设置或撤消最小值或最大值。
  3. 转变缓冲序列的数目。
  4. 指定序列号是否是有序。
  5. 删除序列命令
    DROP SEQUENCE [user.]sequence_name;

4)序列的作用:

在mysql中可以设置主键为自增,而在oralce中主键自增是使用序列来自增,如:scott.seq_t_student_info.nextval

2 建表:

1)oracle常用的数据类型

ORACLE常用的字段类型

ORACLE常用的字段类型有

VARCHAR2 (size) 可变长度的字符串, 必须规定长度

CHAR(size) 固定长度的字符串, 不规定长度默认值为1

NUMBER(p,s) 数字型p是位数总长度, s是小数的长度, 可存负数最长38位. 不够位时会四舍五入.

DATE 日期和时间类型

LOB 超长字符, 最大可达4G

CLOB 超长文本字符串

BLOB 超长二进制字符串

BFILE 超长二进制字符串, 保存在数据库外的文件里是只读的.

  1. 非空和约束条件

非空 NOT NULL

不允许重复 UNIQUE

关键字 PRIMARY KEY

按条件检查 CHECK (条件)

外键 REFERENCES 表名(字段名)

日期字段 DEFAULT SYSDATE

3 分区

在实际使用中,可以随时时间的推移,可能表的数据量会很大,所以建表的同时进行分区是有必要的.

partition by range(create_date)
 interval (numtoyminterval(1,‘MONTH’))
 store in (hellen_space)
 (
 partition t20171223 values less than (to_date(‘2017-12-01’,‘yyyy-mm-dd’)) tablespace hellen_space
 );


这里使用的是根据时间进行自动分区,每月自动建一个分区.

4 索引

索引是为了加快查询的效率