oralce常用对象

表是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 nn_name not null ,
168 salary number ( 6 , 2 )
169 );
170 -- 正确如下:
171 insert into emp01
172 values ( 1 , ' aa ' , 1000 );
173
174 -- 有错
175 insert into emp01
176 values ( 1 , null , 1000 );
177
178
179 -- ---------------------------------------------------
180
181 -- 2)定义unique约束
182 create table emp02
183 (
184 eno int ,
185 name varchar2 ( 10 ),
186 salary number ( 6 , 2 ),
187 constraint u_name unique (name)
188 );
189
190 insert into emp02
191 values ( 1 , ' scott ' , 1000 );
192 insert into emp02
193 values ( 2 , ' scott ' , 1000 );
194 -- 不能重新插入,用于唯一标示
195 -- --------------------------------------------------
196
197 -- 3)定义primary key 约束
198 create table dept04
199 (
200 dno int primary key ,
201 dname varchar2 ( 10 ),
202 loc varchar2 ( 20 )
203 );
204
205 insert into dept04
206 values ( 1 , ' sales ' , ' dallas ' );
207 insert into dept04
208 values ( 1 , ' admin ' , ' dallas ' );
209 -- -插入有错,主键不能重复
210
211 drop table dept04;
212 -- ---------------------------------------------------------
213 -- 4)定义foreign key约束
214
215 create table emp04
216 (
217 eno int ,
218 name varchar2 ( 10 ),
219 salary number ( 6 , 2 ),
220 dno int constraint ck_dno references dept04(dno)
221 );
222
223 insert into emp04
224 values ( 111 , ' scott ' , 1000 , 2 );
225 insert into emp04
226 values ( 111 , ' scott ' , 1000 , 2 );
227 -- -插入出错,违反完整约束条件
228
229 -- -------------------------------------------------------
230
231 -- 5)定义check约束
232
233 create table emp05
234 (
235 eno int ,
236 name varchar2 ( 10 ),
237 salary number ( 6 , 2 ),
238 check (salary between 1000 and 5000 )
239 );
240
241 insert into emp05
242 values ( 1111 , ' scott ' , 800 );
243 -- 违反条件约束
244
245 -- ------------------------------------------------------
246
247 -- 6) 定义复合约束
248
249 create table item
250 (
251 order_id number ( 3 ),
252 item_id number ( 3 ),
253 product varchar2 ( 20 ),
254 primary key (order_id,item_id)
255 );
256
257 -- -------------------维护约束----------------------------------
258
259 -- 1)增加约束
260 /*
261 如果增加unique, primary key, foreign key,和check约束,那必须使用alter table 语句的add子句
262 如果增加not null约束,那么必须使用alter table语句的modify子句。
263 */
264
265 alter table table add [ constraint conatraint_name ]
266 constraint_type ( column ,...);
267
268 alter table table modify column
269 [ constraint constraint_name ] not null ;
270
271 -- --1增加not null约束
272 alter table emp02 modify name not null ;
273
274 -- --2增加unique约束
275 alter table emp04 add constraint u_emp04 unique (name);
276
277 -- --3增加primary key约束
278 alter table dept01 add primary key (dno);
279
280 -- --4增加foreign key约束
281 alter table emp01 add dno number ( 2 ) references dept01(dno);
282 -- ---------------------------------------------------------------------------------------------------------------------------------
283
284 select * from emp01;
285
286 select * from dept01;
287
288 -- --5增加check约束
289 alter table emp01 add check (salary between 800 and 5000 );
290
291
292 -- 2)修改约束名
293 -- 格式如下:
294 alter table table rename constraint old_constraint_name
295 to new_constraint_name;
296
297 -- 3)删除约束
298
299 -- 格式如下:
300 alter table table drop
301 constraint constraint_name | primary key [ cascade ] ;
302
303 -- 4)禁止约束
304
305 -- 格式如下:
306 alter table table disable constraint constraint_name [ cascade ] ;
307
308 -- 5激活约束
309
310 -- 格式如下:
311 alter table table enable constraint constraint_name;
312
313 -- ------------------------------------------------------------------
314
315 -- 显示约束信息
316
317 /*
318 1,user_constraints :存在于dd之中,可以显示当前用户的所有约束信息
319
320 2,user_cons_columns: 用于显示当前用户约束所对应的表列
321
322 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值