Oracle 表的创建 及相关参数

2014-11-24 17:22:14 · 作者: · 浏览: 0

补充说明:设置Autotrace的命令
用法: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]]
--关闭跟踪执行计划和统计信息功能(默认关闭)。
SQL> set autotrace off;
--执行计划和统计信息都显示
SQL> set autotrace on ;
--只显示执行计划和统计信息,不显示sql执行结果。
SQL> set autotrace traceonly;
--只显示执行计划
SQL> set autotrace on explain;
--只显示统计信息
SQL> set autotrace on statistics;
补充说明:归档模式与非归档模式间的转换命令
--1)关闭数据库
SQL>shutdown immediate
--2)把数据库启动到mount的模式
SQL>startup mount
--3)把数据库改为非归档模式 /归档模式
SQL>alter database noarchivelog;
或者
SQL>alter database archivelog;
--4)打开数据库
SQL>Alter database open;
--5)查看数据库归档模式的状态
SQL> archive log list
备注:如果在关闭归档日志时出现ORA-38774错误,请关闭flash闪回数据库模式。
SQL> alter database flashback off
案例2
创建一张基本表
Create tablespace exampletb
Datafile 'E:\ examp01.dbf' reuse;
CREATE TABLE scott.student
(id NUMBER(5) CONSTRAINT st_id_pk PRIMARY KEY,
name VARCHAR2(10) CONSTRAINT st_name NOT NULL,
phone VARCHAR2(11),
school_time DATE DEFAULT SYSDATE,
sex CHAR(1),
CONSTRAINT st_sex_ck CHECK (sex IN('F','M')),
CONSTRAINT st_ph_uk UNIQUE (name))
INITRANS 1 MAXTRANS 255
PCTFREE 20 PCTUSED 50
STORAGE( INITIAL 1024K NEXT 1024K PCTINCREASE 0 MINEXTENTS 1 MAXEXTENTS 5)
TABLESPACE exampletb
2、 修改表结构
Alter table 表名 add (列名 类型); --添加新列
Alter table 表名 modify (列名 类型); --修改列定义

Alter table 表名 drop column 列名; --删除列
Rename 表名 to 新表名 --改表名(表名前不能加方案名)
ALTER TABLE 表名 RENAME COLUMN 当前列名 TO 新列名; --修改列名
修改表结构案例
SQL> Alter table scott.student add (QQ number(10));
--为student表增加列存放QQ号
SQL> Alter table scott.student modify (QQ number(12));
--修改student表中名为QQ的列
SQL> Alter table scott.student rename COLUMN QQ to QQ_num;
--将student表中名为QQ的列改名QQ_num
SQL> Alter table scott.student drop column QQ_num;
--删除student表中名为QQ_num的列
SQL> insert into scott.student(id,name) values(1, 'lucy');
--向student表中插入一条记录
SQL> Alter table scott.student modify (sex char(1) default 'M');
--修改sex列的定义
SQL> insert into scott.student(id,name) values(2, 'Dell');
--向student表中插入一条记录
SQL> Alter table scott.student modify (sex char(1) default null);
--修改sex列的定义
SQL> insert into scott.student(id,name) values(3, 'Mary');
--向student表中插入一条记录
思考:oracle中列的默认值设置与修改。
3、 表的约束
Alter table 表名 add constraint 约束 ; --增加一个约束
Alter table 表名 drop constraint 约束名; --删除一个约束
alter table表名enable [validate/novalidate] constraint约束名;
--启用一个约束,validate/novalidate代表启用约束时是否对表中原有数据作检查。
alter table表名disable constraint约束名; --禁用一个约束
修改表约束案例
SQL> Alter table scott.student disable constraint st_sex_ck;
--禁用st_sex_ck约束
SQL> insert into scott.student(id,name,sex) values(4, 'Lily', 'N');
SQL> Alter table scott.student enable novalidate constraint st_sex_ck;
--启用st_sex_ck约束,但不检查已有数据。
SQL> select * from scott.student;
SQL> insert into scott.student(id,name,sex) values(5, 'Mark', 'N');
SQL>@$ORACLE_HOME/rdbms/admin/utlexpt1.sql --建立异常数据保存表
或者
@ G:\app\Administrator\product\11.2.0\dbhome_1\RDBMS\ADMIN\utlexpt1.sql
--具体路径可以通过搜索utlexpt1.sql获取
SQL>alter table scott.student enable validate constraint st_sex_ck exceptions into exceptions; -- 将异常数据装入异常表
SQL> select * from scott.student where rowid in(select row_id from exceptions);
--查看对应的原表中的异常数据
SQL>Alter table scott.student drop constraint st_sex_ck; --删除约束st_sex_ck