Oracle DDL实践记实

1.创建基本表Table,语法如下

CREATE [GLOBAL TEMPORARY] TABLE [schema.]table (       column datatype [DEFAULT expr] [column_constraint(s)[,…]] [,column datatype [,…]] )          [table_constraint [,…]]            [table_ref_constraint [,…]]               [ON COMMIT {DELETE|PRESERVE} ROWS]

storage_options  [COMPRESS int|NOCOMPRESS]                 [LOB_storage_clause][varray_clause][nested_storage_clause] [XML_type_clause]                     Partitioning_clause                        [[NO]CACHE] [[NO]ROWDEPENDENCIES] [[NO]MONITORING] [PARALLEL parallel_clause]                           [ENABLE enable_clause | DISABLE disable_clause]                              {ENABLE|DISABLE} ROW MOVEMENT                                 [AS subquery] 详细见http://www.ss64.com/ora/table_c.html。连接上数据库后,输入:

create table myTestTable(id number(10),name varchar2(20),address varchar2(30));

即可完成表myTestTable的创建。

修改表Table,语法如下

Change the name, physical and logical storage parameters of an existing table. Enable or disable Constraints, Triggers and Primary Keys:

   ALTER TABLE properties 

Add, modify or drop columns:

   ALTER TABLE column_properties

Change the Constraints and Primary Key for an existing table:

   ALTER TABLE constraints

Add, modify or drop columns from a Partitioned table:

   ALTER TABLE partitioning_clause

Add, modify or drop columns from an external table:

   ALTER TABLE external_clause
示例如下:
alter table mytesttable rename to test; 修改表名为test
alter table mytesttable add (remark varchar2(50)); 添加列remark。
alter table test modify remark varchar2(50) default 'NIHAO';修改列remark的默认值。
alter table test drop column remark;删除列remark。
 2.操作索引
创建索引的语法是:
Table Index
   CREATE [UNIQUE|BITMAP] INDEX [schema.]index_name
      ON [schema.]table_name [tbl_alias]
         (col [ASC | DESC]) index_clause index_attribs
示例如下:
create unique index IDX_MYTESTTABLE on MYTESTTABLE (id); 创建索引IDX_MYTESTTABLE。
修改索引的语法是:
ALTER INDEX [schema.]index options
示例如下:
 alter index IDX_MYTESTTABLE rename to IDX1;
删除索引的语法是:
DROP INDEX [schema.]index [FORCE]
示例如下:
drop index idx1;
3.操作表的主键
创建主键的sql语句示例如下:
alter table test add constraint pri_key_mytesttable primary key (ID);创建表的主键
删除主键的sql语句示例如下:
alter table mytesttable drop constraint pri_key_mytesttable;
4.操作表的视图
创建视图的语法如下:
 CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW
     [schema.]view 
       [(alias,...) inline_constraint(s)]
       [out_of_line_constraint(s)]
       [XMLType_view_clause]
          AS subquery options 
示例如下:
create view myview as select * from test;
修改视图的语法如下:
   ALTER VIEW [schema.]view COMPILE;

   ALTER VIEW [schema.]view ADD out_of_line_constraint;

   ALTER VIEW [schema.]view MODIFY CONSTRAINT constraint {RELY | NORELY};

   ALTER VIEW [schema.]view DROP CONSTRAINT constraint;

   ALTER VIEW [schema.]view DROP PRIMARY KEY

   ALTER VIEW [schema.]view UNIQUE (column [,column,...])
示例如下:
alter view myview compile;
删除视图的语法如下:
DROP VIEW [schema.]view [CASCADE CONSTRAINTS]
示例如下:
drop view myview;
5.操作表空间,Oracle的体系结构是:首先Oracle一个数据库有若干各表空间组成,每个表空间有若干个数据文件(或设备)组成,每个数据文件有若干个盘区组成,每个盘区有若干个block组成,这是Oracle的物理结构。
逻辑结构一个数据库下有若干个方案组成,每个方案下有若干个对象组成(包括表、视图等)每种对象有不同的存储方式。当然逻辑结构和物理结构是有一定对应关系的。 
 一个用户可以跨多个表空间,一个表空间可以有多个用户。
对表空间操作需要用sysdba的身份登录:
sqlplus "sys/apple@ORADB00 as sysdba"
创建临时表空间test_tmp的语句如下:
create temporary tablespace test_temp
tempfile 'D:/oracle/oradata/ORADB00/test_temp01.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
创建表空间test_data的语句如下:
create tablespace test_data
logging
datafile 'D:/oracle/oradata/ORADB00/test_data01.dbf'
size 32M
autoextend on
next 32M maxsize 2048M
extent management local;
datafile指定表空间所在的数据文件的路径。
切换表的默认临时表空间:
ALTER DATABASE DEFAULT TEMPORARY TABLESPACE TEST_TEMP;
删除表空间test_data的语句是:
drop tablespace test_data;
删除临时表空间test_temp的语句是:
drop tablespace test_temp;
查询当前数据库中的所有的临时表空间:
select distinct tablespace_name from dba_temp_files;
查询当前数据库中的默认临时表空间:
select PROPERTY_NAME,PROPERTY_VALUE from DATABASE_PROPERTIES  where PROPERTY_NAME='DEFAULT_TEMP_TABLESPACE';
6.操作用户,这个也需要以sysdba的身份登录:
sqlplus "sys/apple@ORADB00 as sysdba"
创建用户的sql语句:
create user testuser identified by apple
default tablespace test_data
temporary tablespace test_temp;
其中testuser是用户名称,apple是登录密码,同时指定的表空间和临时表空间。
删除用户的语句:
drop user testuser;
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值