当前位置:数据库 > Oracle >>

oracle中常用的对象

oracle中常用的对象
 
表是oracle数据库最基本的对象 
在oracle数据库中,存储用户数据可以使用普通表,分区表,索引表,簇表。
sql 数据库中,存储用户数据可以
DDL:
  1 /*
  2 建表格式
  3 create table [schema.]table_name
  4 (
  5        column_name datatype [default expr]
  6        [,……]
  7 );
  8 
*/
  9 
10 
create
table dept01
11 (
12        dno number(2),
13        name varchar2(10),
14        loc varchar2(20)default
'呼和浩特'
15 );
16 
17 
18 
insert
into dept01(dno,name)
19 
values(10,'技术处');
20 
21 
select
*
from dept01;
22 
23 
/*
24 增加列:
25 alter table table_name add
26 (
27       column datatype [default expr]
28       [,column datatype ……]
29 );
30 
*/
31 
32 
alter
table dept01 add
33 (
34       eee number(4)
35 );
36 
37 
/*
38 修改列定义:
39 alter table table_name modify
40 (
41       column datatype [default expr]
42       [,column datatype ……]
43 );
44 
*/
45 
alter
table dept01 modify
46 (
47       eee  varchar2(15) default
'break'
48 );
49 
50 
51 
/*
52 删除列:
53 alter table table_name drop (column);
54 
*/
55 
56 
alter
table dept01 drop
column eee;
57 
58 
/*
59 修改列名
60 alter table table_name rename column column_name to new_column_name;
61 
*/
62 
63 
alter
table dept01 rename column eee to aaa;
64 
65 
/*
66 修改表名:
67 rename object_name to new_object_name;
68 
*/
69 
70 rename dept01 to deptt;
71 
72 
73 
select
*from deptt;
74 
75 
76 
77 
/*
78 增加注释
79 comment on table table_name is 'text';
80 comment on column table_name.column is 'text';
81 
*/
82 
83 comment on
table deptt is
'系别名';
84 
85 comment on
column deptt.name is
'名字';
86 
87 
-----------------------------------------------------------
88 /*截断表和删除表
89 
*/
90 
----截断表格式:(删除所有数据,也可以用delete 不同点是delete 可以回退,这个不可以)
91 
truncate
table table_name;
92 
93 执行如下:
94 
truncate
table deptt;  
95 
96 
----删除表
97 
drop
table table_name[cascade constraints
[purge;
98 
--cascade constraints用于指定级联删除从表的外部键约束,purge用于指定彻底删除表,即当删除主表时,必须指定cascade constraints子句
99 
drop
table deptt;
100 
101 
102 
----恢复被删除表
103 --当执行drop table 语句删除表时,oracle会将删除表存放到数据库回收站,从10g开始,使用flashback table可以快速恢复被删除表
104 --语法如下:
105 
flashback table table_name to before drop;
106 
107 flashback table deptt to before drop;
108 
109 
110 
select
*
from deptt;
111 
----------------------------------------------
112 
 
113 
--显示表信息
114 
user_tables 
115 
--该数据字典视图用于显示当前用户的所有表信息
116 
 
117 
--select deptt from user_tables;
118 
 
119 
--user_objects该数据字典视图用于显示当前用户的所有数据库对象
120 
 
121 
--user_tab_comments该数据字典用于显示当前用户所有表的注释
122 
 
123 
--user_col_comments用于显示当前用户所有表列的注释
124 
 
125 
126 
127 
--------------------------------------------------约束----------------------------------------------
128 
 
129 
/*
130 约束包括not null ,unique, primary key , foreign key 以及 check
131 1) not null:用于确保列不能为null
132 
133 2) unique(唯一约束):用于唯一地标识列的数据。默认情况下oracle会自动基于唯一约束死建立唯一索引,并且索引名与约束名完全一致
134 
135 3) primary key(主键约束):用于唯一地标识表行的数据,当定义主键约束之后,主键约束列的列值不仅不能重复,而且也不能为null
136 
137 4) foreign key(外部键约束):用于定义主从表之间的关系。外部键约束要定义在从表上,但主表必须具有主键约束或唯一约束。
138    当定义了外部键约束之后,要求外部键列的数据必须在主表键列中存在,或者为null。
139 
140 5) check(检查约束):用于强制表行数据必须要满足的条件。
141 
*/
142 
143 
144 
----------------------------------------------
145 
 
146 
/*
147 定义约束
148 
149 create table[schema.]table_name
150 (
151        column_name datatype [default expr] [column_constraint],
152        ……
153        [table_constraint][,……]
154 );
155 列级约束:
156 column [constraint constraint_name] constraint_type
157 表级约束:
158 column,……,
159 [constraint constraint_name] constraint_type
160 (column, ...)
161 
*/
162 
163 
--!)定义not null约束
164 
create
table emp01
165 (
166        eno int
not
null,
167        name varchar2(10) constraint
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,