XT 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
|