3.1, create a table space and user authorization:
1, create a table space:
CREATE TABLESPACE <tablespace name> LOGGING DATAFILE '<storage path>' SIZE 50M AUTOEXTEND ON NEXT 50M MAXSIZE 31768M EXTENT MANAGEMENT LOCAL;
#windows storage path: D: \ app \ Administrator \ oradata \ orcl \ lc_data.dbf
#linux storage path: /application/oracle/oradata/orcl/lc_data.dbf
2, create a user and specify the table space:
CREATE USER <username> IDENTIFIED BY <password> DEFAULT TABLESPACE <tablespace name>;
# A user has only one table space, and table space can have multiple users;
3, grant permissions to the user:
grant connect, resource, dba to <username>;
3.2, delete the table space:
1, delete users:
drop user <user name> cascade;
2, delete the table space:
drop tablespace <表空间名> including contents and datafiles cascade constraints;
#including contents: Delete the contents of the table space before you drop a table space table space in the content, without adding this parameter, the table space can not be deleted;
#including datafiles: data files to delete the table space;
#cascade constraints: foreign key to delete reference to tables in the tablespace;
3.3, table space query:
1, the query all the table space and the corresponding path:
select tablespace_name,file_name from dba_data_files;
2, query the status of all table space information;
select tablespace_name,status from dba_tablespaces;
3, increase table space size:
ALTER TABLESPACE <tablespace name> ADD DATAFILE '<storage path>' SIZE 50M AUTOEXTEND ON NEXT 50M MAXSIZE UNLIMITED;
4, view all users and the corresponding table space:
select username,default_tablespace from dba_users;
5, view the current user's default table space:
select username,default_tablespace from user_users;
6, the link between the table space:
(1) create a link between the table space:
create database link TO_ <tablespace name> _LINK connect to <username> identified by <password> using '<database instance name>';
(2) the link between the look-up table space:
select * from <current tablespace name> @TO_ <tablespace name> _LINK;
(3) the link between the drop table space:
drop database link TO_ <tablespace name> _LINK;
7, all the amount of table space query:
select tablespace_name,count(*) AS extends,round(sum(bytes)/1024/1024,2) AS MB,sum(blocks) AS blocks from dba_free_space group BY tablespace_name;
8, see the serial number, last_number is the current value:
select * from user_sequences;
9, to modify the table space attributes (offline):
alter tablespace <tablespace name> offline;
10、修改表空间属性(在线):
alter tablespace <表空间名> online;
11、修改表空间属性(只读):
alter tablespace <表空间名> read only;
12、修改表空间属性(读写)
alter tablespace <表空间名> read write;
13、修改session的时间格式:
alter session set nls_date_format='yyyy-mm-dd';
3.4、索引操作:
1、创建单一索引:
create index <索引名> on <表名>(<列名1>);
2、创建组合索引:
create index <索引名> on <表名>(<列名1>,<列名2>);
select * from <表名> where <列名1>='<字符>'
#走索引
select * from <表名> where <列名2>='<字符>'
#不走索引
select * from <表名> where <列名1> like '%<字符>%'
#不走索引
select * from <表名> where <列名1>='<字符>' and <列名2>='<字符2>'
#走索引
select * from <表名> where <列名1>='<字符>' or <列名2>='<字符2>'
#不走索引
drop index <索引名称>;
#删除索引
3、查看索引的方法:
(1)在当前用户中查找表名:
select * from user_tables where table_name like '<表名>%';
(2)查询该表的所有索引:
select * from user_indexes where table_name='<表名>';
(3)查询该表的所有索引列:
select * from user_ind_columns where table_name='<表名>';
(4)查询当前用户所有表的索引和索引类别:
select table_name,index_name,index_type from user_indexes order by index_name;
(5)查看当前用户下指定索引的信息:
select * from user_indexes where index_name=upper('&index_name');
(6)查看当前用户下指定的索引的索引列:
select * from user_ind_columns where index_name=upper('&index_name');
(7)查看当前用户下指定索引的大小:
select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&index_name');
(8)补充:
user_indexes #存放着当前用户所有表的索引信息;
user_segments #存放着当前用户所有表的索引大小;
user_ind_columns #存放着当前用户所有表的索引列信息;
4、索引补充:
(1)哪些列适合建索引:
1)经常出现在where子句的列;
2)经常用于表连接的列,在匹配表上进行建索引;
3)该列是高基数数据列,高基数数据列是指有很多不同的值;
4)索引里面不计null值;
5)表很大,查询结果集小;
6)在pk、uk、fk键上建立索引;
7)经常需要排序"order by"和分组"group by"的列;
(2)索引用不了的写法:
1)函数导致索引用不了 where upper(colname)= 'char';
2)可以对函数建索引:
create index <索引名> on <表名>(round(<列名1>));
3)表达式导致索引用不了 where colname*12=1200;
4)索引不是万能的;
(3)索引结构:
1)分析索引结构有效性:
analyze index <索引> validate structure;
一般来讲默认的方式是offline;
当以offline的模式analyze索引时,会对table加一个表级共享锁,对目前table的一些实时DMl操作会产生一定的影响;
而以online模式分析时候,则不会加任何lock,但在index_stats中是看不到任何信息的;
2)查看索引结构:
select NAME,HEIGHT,BLOCKS,BR_BLKS,BR_ROWS,LF_BLKS,LF_ROWS from index_stats;
3)合并索引叶级块碎片:
alter index <索引名> coalesce;
4)重建索引:
alter index <索引名> rebuild;