建立表
自定义建表
表有名字,由行和列组成,行称之为记录,列称之为字段
表是存放数据的基本对象
一般的表叫做heap table堆表 就是数据杂乱无章的码放 没有顺序
表的名字由a-Z 0-9 _ $ # 连续字符组成 长度30
以字母开头 用_-连接 以数字表示数量 带有拓展名
长度限制
在一个用户模式不要和其他对象重名 即使能重名 我们也要极力避免.
不要建立中文名称的表
建立表的语法很复杂 这里只说基本语法
create table [schema.]table_name(
column_name datetype [default expr][,...]
);
schema
指特定模式(就是在哪个用户模式中)
table_name
表名
column_name
列名(最少1列 最多1000个列,但没人建那么大的表)
datatype
数据类型
default
默认值
参照建表
参照一个以存在的表的列和数据 来建立新表
create table TABLE_NAME as select * from emp;SCOTT@ora10g> purge recyclebin; 清空回收站.
这相当于复制emp表 但并不是复制所有 通过描述(desc)可以看出 emp的NOT NULL约束是不被复制的
只复制表结构 不带表数据
create table TABLE_NAME as select * from emp where 0=9;
加个false条件即可 因为没有选择行 即没数据
查询自己模式下都有哪些表
SCOTT@ora10g> show user
USER is "SCOTT"
SCOTT@ora10g>
SCOTT@ora10g> select * from tab;
TNAME
TABTYPE
CLUSTERID
------------------------------ ------- ----------
DEPT
TABLE
EMP
TABLE
BONUS
TABLE
SALGRADE
TABLE
SCOTT@ora10g>
SCOTT@ora10g> create table t1(id number,name char(10),loc varchar2(40) default 'BingJing');
Table created.
SCOTT@ora10g>
这样就建立了一个t1表 三列 id name loc
SCOTT@ora10g> desc t1
Name
Null? Type
----------------------------------------------------------- -------- ----------------------------------------
ID
NUMBER
NAME
CHAR(10)
LOC
VARCHAR2(40)
SCOTT@ora10g>
这样就建立了一个t1表 三列 id name loc
修改表
修改表名
增加列
修改列的定义
删除列
unused列(隐藏列)
修改列名
增加注释
truncate
drop
回收站
修改表名
rename old_name to new_name
SCOTT@ora10g> desc t1
Name
Null? Type
----------------------------------------------------------- -------- ----------------------------------------
ID
NUMBER
SCOTT@ora10g> rename t1 to t4;
Table renamed.
SCOTT@ora10g> desc t4
Name
Null? Type
----------------------------------------------------------- -------- ----------------------------------------
ID
NUMBER
SCOTT@ora10g>
增加列
alter table table_name add(column datatype [default expr] [,column datatype...])
SCOTT@ora10g> desc t4
Name
Null? Type
----------------------------------------------------------- -------- ----------------------------------------
ID
NUMBER
SCOTT@ora10g> alter table t4 add (name varchar2(10));
Table altered.
SCOTT@ora10g> desc t4
Name
Null? Type
----------------------------------------------------------- -------- ----------------------------------------
ID
NUMBER
NAME
VARCHAR2(10)
SCOTT@ora10g>
修改列的定义
alter table table_name modify(column datatype [default expr] [,column datatype...)
SCOTT@ora10g> alter table t4 modify(name char(20));
Table altered.
SCOTT@ora10g> desc t4
Name
Null? Type
----------------------------------------------------------- -------- ----------------------------------------
ID
NUMBER
NAME
CHAR(20)
SCOTT@ora10g>
删除列
alter table drop (column)
SCOTT@ora10g> alter table t4 drop(name);
Table altered.
SCOTT@ora10g> desc t4
Name
Null? Type
----------------------------------------------------------- -------- ----------------------------------------
ID
NUMBER
SCOTT@ora10g>
隐藏列
SCOTT@Seker_db> select count(*) from o1;
COUNT(*)
----------
1302336
SCOTT@Seker_db> set timing on
SCOTT@Seker_db> alter table o1 drop column object_type;
Table altered.
Elapsed: 00:00:23.32
SCOTT@Seker_db> alter table o1 set unused column object_name;
Table altered.
Elapsed: 00:00:00.18
SCOTT@Seker_db> desc o1; 这样就看不到这个列了
Name
Null?
Type
----------------------------- -------- --------------------
OWNER
NOT NULL VARCHAR2(30)
SUBOBJECT_NAME
VARCHAR2(30)
OBJECT_ID
NOT NULL NUMBER
DATA_OBJECT_ID
NUMBER
CREATED
NOT NULL DATE
LAST_DDL_TIME
NOT NULL DATE
TIMESTAMP
VARCHAR2(19)
STATUS
VARCHAR2(7)
TEMPORARY
VARCHAR2(1)
GENERATED
VARCHAR2(1)
SECONDARY
VARCHAR2(1)
SCOTT@Seker_db> select * from USER_UNUSED_COL_TABS;
TABLE_NAME
COUNT
------------------------- ----------
O1
1
Elapsed: 00:00:00.12
SCOTT@Seker_db> alter table o1 drop unused columns; 业务不忙时再来删除
Table altered.
Elapsed: 00:00:29.17
SCOTT@Seker_db>
修改列名
alter table table_name rename column old_column_name to new_column_name
SCOTT@ora10g> alter table t4 rename column id to aa;
Table altered.
SCOTT@ora10g> desc t4
Name
Null? Type
----------------------------------------------------------- -------- ----------------------------------------
AA
NUMBER
SCOTT@ora10g>
增加注释
comment on table table_name is 'text'
comment on column table_name.columb is 'text'
SCOTT@ora10g> select * from user_tab_comments where table_name='T4';
TABLE_NAME
TABLE_TYPE COMMENTS
------------------------- ----------- ----------------------------------------
T4
TABLE
SCOTT@ora10g> comment on table t4 is 'AAAAAAAAAAAAAAAAA';
Comment created.
SCOTT@ora10g> select * from user_tab_comments where table_name='T4';
TABLE_NAME
TABLE_TYPE COMMENTS
------------------------- ----------- ----------------------------------------
T4
TABLE AAAAAAAAAAAAAAAAA
SCOTT@ora10g>
SCOTT@ora10g> select * from user_col_comments where table_name='T4';
TABLE_NAME
COLUMN_NAME
COMMENTS
------------------------- ------------------------- ----------------------------------------
T4
AA
SCOTT@ora10g> comment on column t4.aa is 'bbbbbbbbbbb';
Comment created.
SCOTT@ora10g> select * from user_col_comments where table_name='T4';
TABLE_NAME
COLUMN_NAME
COMMENTS
------------------------- ------------------------- ----------------------------------------
T4
AA
bbbbbbbbbbb
SCOTT@ora10g>
截断表
保留表结构
清空表的数据
tuncate table table_name
SCOTT@ora10g> select * from t2;
EMPNO ENAME JOB
MGR HIREDATE
SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7782 CLARK MANAGER
7839 09-JUN-81
2450
10
7839 KING PRESIDENT
17-NOV-81
5000
10
7934 MILLER CLERK
7782 23-JAN-82
1300
10
SCOTT@ora10g> truncate table t2;
Table truncated.
SCOTT@ora10g> select * from t2;
no rows selected
SCOTT@ora10g>
删除表
drop table table_name [purge]
10G中增加了回收站功能,表没有被完全删 只是放到回收站里去了
SCOTT@ora10g> drop table t4;
Table dropped.
SCOTT@ora10g> select * from tab;
TNAME
TABTYPE
CLUSTERID
------------------------------ ------- ----------
DEPT
TABLE
EMP
TABLE
BONUS
TABLE
SALGRADE
TABLE
T5
TABLE
T3
TABLE
T2
TABLE
BIN$mKoYM02QCojgQAB/AQAvyw==$0 TABLE
8 rows selected.
SCOTT@ora10g> 不放入回收站 直接删除
SCOTT@ora10g> drop table t3 purge;
Table dropped.
SCOTT@ora10g>
从回收站中恢复表
SCOTT@ora10g> show recycl
ORIGINAL NAME
RECYCLEBIN NAME
OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
T4
BIN$mKoYM02QCojgQAB/AQAvyw==$0 TABLE
2010-12-31:07:32:09
SCOTT@ora10g> flashback table t4 to before drop;
Flashback complete.
SCOTT@ora10g> select * from tab;
TNAME
TABTYPE
CLUSTERID
------------------------------ ------- ----------
DEPT
TABLE
EMP
TABLE
BONUS
TABLE
SALGRADE
TABLE
T5
TABLE
T4
TABLE
T2
TABLE
7 rows selected.
SCOTT@ora10g>
多次删除同一个表的恢复
SCOTT@ora10g> flashback table "BIN$mKoYM02RCojgQAB/AQAvyw==$0" to before drop;
Flashback complete.
SCOTT@ora10g>
SCOTT@ora10g> flashback table t4 to before drop rename to t7; 新改名不用加引号.
Flashback complete.
SCOTT@ora10g>
清空回收站
SCOTT@ora10g> sho recycle;
ORIGINAL NAME
RECYCLEBIN NAME
OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
T4
BIN$mKoYM02UCojgQAB/AQAvyw==$0 TABLE
2010-12-31:08:35:06
SCOTT@ora10g> purge table t4;
Table purged.
SCOTT@ora10g> show recycle; 查看回收站.
SCOTT@ora10g> purge recyclebin; 清空回收站.
SCOTT@ora10g> purge table t2 ; 清空回收站中t2的表.
SQL> purge dba_recyclebin; 清空所有回收站里的内容.???
SQL> alter session set recyclebin=off; 关闭当前会话的回收站;
SQL> alter system set recyclebin=off; 关闭系统的回收站;
sys管理回收站:
purge dba_recyclebin; 删除所有人的回收站资源
purge tablespace XXX; 删除xxx表空间中的回收站资源
purge tablespace xxx user USERNAME;删除xxx表空间中的某个用户的回收站资源
select owner,ts_name,OBJECT_NAME,ORIGINAL_NAME,DROPTIME
from dba_recyclebin;
create table t1 as select * from emp where 0=9;
修改表名
增加列
修改列的定义
删除列
unused列(隐藏列)
修改列名
增加注释
truncate
drop
回收站