alter table set unused之后各种情况处理




如下实验:

 gyj@OCM> Create table emp(

  2    Empno     number(4)    not null,

  3    First_name  varchar2(20),

  4    Last_name  varchar2(20),

  5    Salary      number(10,2),

  6    Deptno     number(2)

  7  );

 

 

Table created.

 

gyj@OCM> insertinto emp values(100,'yijun','guo',35000,1);

 

1 row created.

 

gyj@OCM>commit;

 

Commit complete.

 

 

gyj@OCM> altertable emp drop column first_name; --有数据也能被删除!排除答案A

 

Table altered.

 

gyj@OCM> altertable emp drop column Empno;

 

Table altered.

 

gyj@OCM> altertable emp drop column last_name;

 

Table altered.

 

gyj@OCM> altertable emp drop column salary;

 

Table altered.

 

gyj@OCM> altertable emp drop column deptno;

alter table empdrop column deptno

*

ERROR at line 1:

ORA-12983: cannotdrop all columns in a table  ----验证了答案B是对的

 

SET UNUSED Clause   ---官方解释,排除答案C

Specify SET UNUSED to mark one or more columns as unused. Specifying this clause does not actually remove the target columns from each row in the table. That is, it does not restore the disk space used bythese columns. Therefore, the response time is faster than when you execute the DROP clause(即不带UNUSED DROP clause).

SET UNUSED Clause会撤消相关列的存储空间,并修正与存储空间字典信息,故而相关列的存储空间不可恢复。当然,这些列的列名等信息并未从系统字典信息里清除掉。

不要马上drop column,应该先set unusedcolumn无法使用,set unused命令会避开系统尖峰时间再来处理删除栏位里的资料。要注意的是一但你set unused column,这个栏位是无法再回复使用的。

用法如下:


gyj@OCM> ALTERTABLE emp SET UNUSED (first_name);

 

Table altered.

 

gyj@OCM> altertable emp drop unused column;

 

Table altered.



gyj@OCM> altertable emp add first_name varchar2(10);

 

Table altered.

 

gyj@OCM> select *from emp;

 

    DEPTNO FIRST_NAME

---------- ----------

         1

 

gyj@OCM> updateemp set first_name='yijun';

 

1 row updated.

 

gyj@OCM> commit;

 

Commit complete.

 

gyj@OCM> ALTERTABLE emp SET UNUSED (first_name);

 

Table altered.

 

gyj@OCM> altertable emp drop unused column;

 

Table altered.

 

gyj@OCM> select *from emp;

 

    DEPTNO

----------

         1

 

gyj@OCM> alter table emp add first_name varchar2(10);

 

Table altered.

 

gyj@OCM> select * from emp

  2 ;

 

    DEPTNO FIRST_NAME

---------- ----------

         1

 

gyj@OCM> update emp set first_name='yijun';

 

1 row updated.

 

gyj@OCM> commit;

 

Commit complete.

 

gyj@OCM> alter table emp add primary key(deptno,first_name);

 

Table altered.

 

yj@OCM> alter table emp drop column first_name;

alter table emp drop column first_name

                            *

ERROR at line 1:

ORA-12991: column is referenced in a multi-column constraint

 

 

gyj@OCM> alter table emp drop column first_name cascade constraints;

 

Table altered.

------验证了答案D是对的

 

答案:BD

 

### 修改表的时间间隔设置 在 SQL 中,`ALTER TABLE` 语句主要用于更改数据库表的结构。然而,直接通过 `ALTER TABLE` 设置时间间隔并不是常见的做法。通常情况下,时间间隔会作为列的数据类型属性或默认值的一部分来处理。 如果目标是在特定数据库管理系统 (DBMS) 中为某一列设定时间间隔,默认值可以通过 `ALTER TABLE` 来定义。例如,在 MySQL 或 SQL Server 中,可以创建一个具有时间戳类型的列并为其分配默认的时间表达式: #### 在 MySQL 中添加带时间间隔的列 ```sql ALTER TABLE your_table ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP; ``` 此命令会在名为 `your_table` 的表中新增一列 `created_at`,其默认值设为当前时间戳[^4]。 #### 更新已有列为带有自动更新的时间戳 为了使该列不仅记录插入时的时间点而且每次更新行时也自动刷新,则可进一步调整: ```sql ALTER TABLE your_table MODIFY COLUMN updated_at TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP; ``` 这段代码将确保每当对应行被修改时,`updated_at` 列都会自动更新至最新的时间戳[^5]。 需要注意的是,“时间间隔”的概念可能涉及不同的具体需求;上述例子展示了如何利用现有的 SQL 功能实现常见场景下的时间管理功能。对于更复杂的情况,比如定期执行某些操作(如清理旧数据),则应考虑使用事件调度器或其他定时任务机制而不是单纯依赖于 `ALTER TABLE` 命令。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值