码迷,mamicode.com
首页 > 其他好文 > 详细

表管理

时间:2016-01-22 03:24:51      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:表的管理

主键 外键


主键:唯一标识一条记录,不能有重复的,不允许为空.

     用来保证数据完整性

     主键只能有一个


外键:表的外键是另一表的主键, 外键可以有重复的, 可以是空值

     用来和其他表建立联系用的

     一个表可以有多个外键

   

1.创建一张学生表


SQL> create table t_stu(  

     stuid number(10) primary key,  

     stuname varchar2(20) not null,  

     stusex varchar2(3) default ‘nan‘ check(stusex in(‘nan‘,‘nv‘)));  


2.创建一张课程表


SQL> create table t_couse(  

    couseid number(10) primary key,  

    cousename varchar2(20) not null,  

    cousetype varchar2(4)); 



3.创建一张学生课程成绩表(包括主外键)


SQL> create table t_score(  

    scoreid number(10) primary key,  

    stuid number(10) references t_stu(stuid),  

    couseid number(10),  

    constraint fk_couseid foreign key(couseid)  

    references t_couse(couseid)  

    on delete cascade);        

__________________________________________________________


创建表空间

cd /oracle/app/oracle/oradata/orcl/

[oracle@sq orcl]$ ls


SQL> create tablespace aa datafile ‘/oracle/app/oradata/TEST/aaa.dbf‘ size 50M;


[oracle@sq orcl]$ ls (目录下多出一个aaa.dbf文件)


SQL> create table a01 tablespace aaa as select * from sys.dba_objects;



----------------------------------------------------------

验证数据类型:

DUAL 表用途

常用没有目标表的select语句中

SQL> select user from dual;(查看当前连接用户)


SQL> select sysdate from dual;(查看当前系统时间)


SQL> select systimestamp from dual;(查看当前系统时间)


SQL> select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘) from dual;

(查看当前系统时间)


SQL>  select to_char(systimestamp,‘yyyy-mm-dd hh24:mi:ss.ff‘) from dual;(查看当前系统时间)


SQL> select 1+2 from dual;

==========================================================

查询rowid


查看t3表中的rowid

SQL> select rowid,t.* from t10 t;(rowid为表中的伪列 不能插入数据)


1.scott登陆

2.对象编号,所有的对象在数据库里面都有一个编号

SQL>  select dbms_rowid.rowid_object(rowid) from emp t;

结果中dbms_rowid.rowid_object = 51148


2.验证 51148 是scott用户下的 emp表

SQL> conn sys/123456 as sysdba

SQL> select * from dba_objects t where t.object_id=51148;


SQL> select * from dba_data_files;(查看文件号)


-------------------------------

查看t10表中 伪列rowid中 id,块,数据文件:

SQL> select dbms_rowid.rowid_row_number(rowid) ROWID#,dbms_rowid.rowid_block_number(rowid) block#,dbms_rowid.rowid_relative_fno(rowid) FILE# from t10;



------------------------------------------

1.scott登陆

2.查看所在的数据文件

select dbms_rowid.rowid_object(rowid),dbms_rowid.rowid_relative_fno(rowid),t.*from emp t;

结果:dbms_rowid.rowid_relative_fno=4 (表示对应4号文件)


sys登陆(对应的文件)

SQL> select FILE_NAME,FILE_ID from dba_data_files;


3.查询所在块

select dbms_rowid.rowid_block_number(rowid),t.* from emp t;


dbms_rowid.rowid_block_number 对应的是所在的块


4.查看行号

select dbms_rowid.rowid_object(rowid),dbms_rowid.rowid_relative_fno(rowid),dbms_rowid.rowid_row_number(rowid),t.* from emp t;


dbms_rowid.rowid_row_number(rowid) 对应的是行号


====================================================

创建表前的思考

1 将表存放在不同的表空间

通常单用户模式下表对于表空间的分散存放并不能提升io性能

便于管理

有利于并发性能提升

2 使用本地管理表空间存储表避免块碎片

本地管理表空间自动进行碎片整理一直提升查询性能

3 使用合适的数据类型

     不痛的数据类型性能不同(整型长高于字符串型)

4 使用分区表提升性能

      适合超过10G大小的表


1.创建普通表

create table t1(id int);


2.分区表

create tablespace ts01 logging datafile ‘/oracle/app/oradata/TEST/ts01.dbf‘ size 10m;


create tablespace ts02 logging datafile ‘/oracle/app/oradata/TEST/ts02.dbf‘ size 10m;



create tablespace ts03 logging datafile ‘/oracle/app/oradata/TEST/ts03.dbf‘ size 10m;


create tablespace ts04 logging datafile ‘/oracle/app/oradata/TEST/ts04.dbf‘ size 10m;


创建分区表(范围分区)(2001之前放在ts01,2002年之前方式ts02)

SQL> create table test123(id number,createdate date)

    partition by range(createdate)

    (

    partition p1 values less than (to_date(‘2001-01-01‘,‘yyyy-mm-dd‘)) tablespace ts01,

    partition p2 values less than (to_date(‘2002-01-01‘,‘yyyy-mm-dd‘)) tablespace ts02,

    partition p3 values less than (to_date(‘2003-01-01‘,‘yyyy-mm-dd‘)) tablespace ts03,

    partition pmax values less than (maxvalue) tablespace ts04

   );

‘2004-01-01‘

-----------------

创建hash 分区表

通常应用在不能划分范围的表

SQL> create table test6 (id number,name varchar2(10))

    partition by hash(name)

    partitions 4

    store in (ts01,ts02,ts03,ts04);



----------------------

创建列表分区

该分区的特点是某列的值只有几个,基于这样的特点我们可以采用列表分区。

比如城市,省份


SQL> create table test122 (id number,name varchar2(10),city varchar2(10))

    partition by list(city)

    (

    partition p1 values(‘bj‘,‘sh‘) tablespace ts01,

    partition p2 values(‘gz‘,‘sz‘) tablespace ts02,

    partition p3 values(‘cc‘,‘jl‘) tablespace ts03,

    partition p4 values(default) tablespace ts04

    );



查看分区表

SQL> select * from user_tab_partitions



----------------------

复合分区

SQL>create table test111 (id int,createdate date)

    partition by range(createdate) subpartition by hash(id)

    subpartitions 3 store in(ts01,ts02,ts03)  

    (partition p11 values less than(to_date(‘2001-01-01‘,‘yyyy-mm-dd‘)),

     partition p12 values less than(to_date(‘2002-01-01‘,‘yyyy-mm-dd‘)),

     partition p13 values less than(maxvalue)

);

先是根据交易日期进行范围分区,然后根据ID将记录散列地存储在三个表空间中。


insert into test111 values(22,to_date(‘2000-12-12‘,‘yyyy-mm-dd‘));

insert into test111 values(25,to_date(‘2000-12-12‘,‘yyyy-mm-dd‘));

insert into test111 values(33,to_date(‘2000-12-12‘,‘yyyy-mm-dd‘));

insert into test111 values(28,to_date(‘2000-12-12‘,‘yyyy-mm-dd‘));

insert into test111 values(39,to_date(‘2000-12-12‘,‘yyyy-mm-dd‘));

insert into test111 values(55,to_date(‘2000-12-12‘,‘yyyy-mm-dd‘));

insert into test111 values(178,to_date(‘2000-12-12‘,‘yyyy-mm-dd‘));

insert into test111 values(83,to_date(‘2000-12-12‘,‘yyyy-mm-dd‘));

insert into test111 values(99,to_date(‘2000-12-12‘,‘yyyy-mm-dd‘));

insert into test111 values(67,to_date(‘2000-12-12‘,‘yyyy-mm-dd‘));






SQL>create tablespace ts05 logging datafile ‘/oracle/app/oradata/TEST/ts05.dbf‘ size 10m;


SQL>create tablespace ts06 logging datafile ‘/oracle/app/oradata/TEST/ts06.dbf‘ size 10m;


SQL>create table test2 (time DATE,province VARCHAR2(10))

partition by range(time) subpartition by list (province)

(

partition p1 values less than (to_date(‘2004-1-1‘,‘YYYY-MM-DD‘))

(

subpartition sp1 values (‘BJ‘) tablespace ts01,

  subpartition sp2 values (‘SH‘) tablespace ts02,

  subpartition sp3 values (‘SZ‘) tablespace ts03,

  subpartition other values(DEFAULT) tablespace ts04

  ) ,

partition p2 values less than (to_date(‘2005-1-1‘,‘YYYY-MM-DD‘)) 

(

subpartition sp12 values (‘BJ‘) tablespace ts01,

  subpartition sp22 values (‘SH‘) tablespace ts02,

  subpartition sp32 values (‘SZ‘) tablespace ts03,

  subpartition other2 values(DEFAULT) tablespace ts04

  )

);



SQL> insert into test2 values(to_date(‘2003-12-12‘,‘yyyy-mm-dd‘),‘BJ‘);   

多插些数据,发现ts01变大了

===========================================

分区合并

范围分区合并

SQL> alter table test123 merge partitions p1,p2 into partition p12;



----------------------

分隔分区


SQL> alter table test123 split partition pmax at(to_date(‘2004-01-01‘,‘yyyy-mm-dd‘))

     into (partition p5,partition pmax);

把原来的pmax分区 拆分成2个分区


-----------------------

创建新的分区

create tablespace ts05 logging datafile ‘/oracle/app/oradata/TEST/ts05.dbf‘ size 10m;



SQL> alter table test123 drop partition pmax;


SQL> alter table test123 add partition p15 values less 

  2  than(to_date(‘2005-01-01‘,‘yyyy-mm-dd‘))tablespace ts05;


create tablespace ts06 logging datafile ‘/oracle/app/oradata/TEST/ts06.dbf‘ size 10m;


SQL> alter table test123 add partition p16 values less 

  2  than(maxvalue)tablespace ts06;

============================================

高水位线

SQL> create table t1 as select * from dba_objects;


SQL> insert into t1 select * from t1;



SQL> /


SQL> commit;


SQL> select segment_name,blocks from user_segments where segment_name=‘T1‘;


SQL> analyze table t1 compute statistics;

(analyze分区表,即产生统计信息)


SQL> select table_name,blocks,empty_blocks,avg_space from tabs where table_name=‘T1‘;


**

 SQL> delete from t1 where OBJECT_ID>1000;


SQL> analyze table t1 compute statistics;



SQL> select table_name,blocks,empty_blocks,avg_space from tabs where table_name=‘T1‘;

发现水位线没变



创建表空间

SQL> create tablespace k1 logging datafile ‘/oracle/app/oracle/oradata/orcl/kkk1.dbf‘ size 64m autoextend on next 65m maxsize 1000m extent management local;


SQL> alter table t1 move tablespace k1;


SQL> analyze table t1 compute statistics;


SQL>  select table_name,blocks,empty_blocks,avg_space from tabs where table_name=‘T1‘;



发现水位线已经收缩

===========================================

临时表

1.创建会话级别临时表

SQL> create global temporary table temp_tbl(col_a varchar2(30)) 

  2  on commit preserve rows;


插入数据

insert into temp_tbl values(‘test session table‘);


SQL> commit;


SQL> select * from temp_tbl;


退出会话再重新登陆

SQL> exit

[oracle@sq ~]$ sqlplus sys/123456 as sysdba

SQL> select * from temp_tbl;  (没有数据了)


2.创建事务级别临时表

SQL> create global temporary table temp_tbl1(col_a varchar2(20))

  2  on commit delete rows;


插入数据

SQL> insert into temp_tbl1 values(‘transaction table‘);


提交

SQL> commit;


SQL> select * from temp_tbl1;

(没有数据了)


===================================================

截断表

删除表中的所有数据

删除速度快,不生成undo数据,只生成很少的日志

删除表中所有数据并回收使用的空间

对应的索引数据也被清除


SQL> create table test19 as select * from dba_objects;



SQL> insert into test19 select * from dba_objects;


SQL> select count(*) from test19;


  COUNT(*)

----------

     99490


SQL> truncate table test19 reuse storage;


SQL> select extent_id,block_id,blocks from dba_extents where segment_name=‘TEST19‘;

发现数据块并没有释放,但降低了水位线


SQL> select count(*) from test19;


  COUNT(*)

----------

         0

================================================

删除表

drop table t1;

delete from t1;


Truncate 清空表所有数据,生成最少量日志与undo数据

Delete 不带where条件,清空表所有数据,生成大量日志undo数据

Drop   在删除数据的同时删除表结构



====================================================

添加列

SQL> alter table t1 add (age1 number(3));


修改列属性

SQL> alter table t1 modify (age int);


======================================

SQL> create table tg (rr date);

SQL> insert into tg values(to_date(‘2012-03-12 11:11:11‘,‘yyyy-mm-dd hh24:mi:ss‘));

SQL> select * from tg;


本文出自 “linux” 博客,谢绝转载!

表管理

标签:表的管理

原文地址:http://chenjisong.blog.51cto.com/7858025/1737385

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!