oracle创建表增加字段sql脚本

oracle comment on的用法
oracle中用comment on命令给表或字段加以说明,语法如下:
COMMENT ON
{ TABLE [ schema. ]
{ table | view }
| COLUMN [ schema. ]
{ table. | view. | materialized_view. } column
| OPERATOR [ schema. ] operator
| INDEXTYPE [ schema. ] indextype
| MATERIALIZED VIEW materialized_view
}
IS ‘text’ ;

用法如下:
1.对表的说明
comment on table table_name is ‘comments_on_tab_information’;

2.对表中列的说明
comment on column table.column_name is ‘comments_on_col_information’;

3.查看表的说明
SQL> select * from user_tab_comments where TABLE_NAME=’EMPLOYEES’;

TABLE_NAME TABLE_TYPE COMMENTS


EMPLOYEES TABLE 员工表

SQL> select * from user_tab_comments where comments is not null;

TABLE_NAME TABLE_TYPE COMMENTS


EMPLOYEES TABLE 员工表

4.查看表中列的说明
SQL> select * from user_col_comments where TABLE_NAME=’EMPLOYEES’;

TABLE_NAME COLUMN_NAME COMMENTS


EMPLOYEES EMPLOYEE_ID
EMPLOYEES MANAGER_ID
EMPLOYEES FIRST_NAME
EMPLOYEES LAST_NAME
EMPLOYEES TITLE
EMPLOYEES SALARY 员工薪水

SQL> select * from user_col_comments where comments is not null;

TABLE_NAME COLUMN_NAME COMMENTS


EMPLOYEES SALARY 员工薪水

5.我们也可以从下面这些视图中查看表级和列级说明:
ALL_COL_COMMENTS
USER_COL_COMMENTS
ALL_TAB_COMMENTS
USER_TAB_COMMENTS

6.删除表级说明,也就是将其置为空
SQL> comment on table employees is ”;
Comment added

SQL> select * from user_tab_comments where TABLE_NAME=’EMPLOYEES’;

TABLE_NAME TABLE_TYPE COMMENTS


EMPLOYEES TABLE

7.删除列级说明,也是将其置为空
SQL> comment on column employees.salary is ”;
Comment added

SQL> select * from user_col_comments where TABLE_NAME=’EMPLOYEES’;

TABLE_NAME COLUMN_NAME COMMENTS


EMPLOYEES EMPLOYEE_ID
EMPLOYEES MANAGER_ID
EMPLOYEES FIRST_NAME
EMPLOYEES LAST_NAME
EMPLOYEES TITLE
EMPLOYEES SALARY

下面我展示一下项目开发中的实例来做参考

-- Create table
create table F_S_WASH_TASK
(
  task_id         NUMBER(16) not null,
  task_no         VARCHAR2(16) not null,
  equip_categ     VARCHAR2(8),
  task_status     VARCHAR2(8) not null,
  start_date      DATE not null,
  end_date        DATE,
  task_num        NUMBER(15),
  org_no          VARCHAR2(16) not null,
  creator_no      VARCHAR2(16),
  create_date     DATE,
  exec_org_no     VARCHAR2(16),
  team_no         VARCHAR2(16),
  executor_no     VARCHAR2(16),
  parent_task_id  NUMBER,
  priority        VARCHAR2(8) not null,
  remark          VARCHAR2(32),
  finish_remark   VARCHAR2(256),
  wiring_mode     VARCHAR2(8),
  finished_date   DATE,
  real_start_date DATE
)
tablespace MPAC_A
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64
    next 1
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table F_S_WASH_TASK
  is '清洁分选任务表';
-- Add comments to the columns 
comment on column F_S_WASH_TASK.task_id
  is '唯一标识';
comment on column F_S_WASH_TASK.task_no
  is '清洁分选任务编';
comment on column F_S_WASH_TASK.equip_categ
  is '设备类别,参见计量标准代码:设备类别VW_EQUIP_CATEG';
comment on column F_S_WASH_TASK.task_status
  is '任务状态,01初始、02待分配、03已分配、04执行中、05执行完成、06已完成';
comment on column F_S_WASH_TASK.start_date
  is '任务开始时间';
comment on column F_S_WASH_TASK.end_date
  is '任务结束时间';
comment on column F_S_WASH_TASK.task_num
  is '任务数量';
comment on column F_S_WASH_TASK.org_no
  is '创建单位编号';
comment on column F_S_WASH_TASK.creator_no
  is '创建人';
comment on column F_S_WASH_TASK.create_date
  is '创建时间';
comment on column F_S_WASH_TASK.exec_org_no
  is '执行单位';
comment on column F_S_WASH_TASK.team_no
  is '执行班组';
comment on column F_S_WASH_TASK.executor_no
  is '执行人';
comment on column F_S_WASH_TASK.parent_task_id
  is '父任务标识';
comment on column F_S_WASH_TASK.priority
  is '任务优先级 01一般 02 重要 03紧急';
comment on column F_S_WASH_TASK.remark
  is '备注';
comment on column F_S_WASH_TASK.finish_remark
  is '任务完成备注';
comment on column F_S_WASH_TASK.wiring_mode
  is '接线方式';
comment on column F_S_WASH_TASK.finished_date
  is '任务完成日期';
comment on column F_S_WASH_TASK.real_start_date
  is '任务实际开始时间';
-- Create/Recreate primary, unique and foreign key constraints 
alter table F_S_WASH_TASK
  add constraint PK_F_S_WASH_TASK primary key (TASK_ID)
  using index 
  tablespace MPAC_A
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
### 在 Oracle 数据库中向添加新字段Oracle 数据库中,可以使用 `ALTER TABLE` 语句来向现有的中添加新的字段。为了确保操作的安全性和可重复性,建议采用带有条件判断的方式进行。 对于简单的场景,可以直接执行如下 SQL 命令: ```sql ALTER TABLE 名 ADD (列名 VARCHAR2(长度)); ``` 然而,在实际应用环境中,通常希望创建一个能够多次安全运行而不引发错误的脚本。为此,可以通过 PL/SQL 编写一段逻辑代码,先检查目标字段是否存在,如果不存在则添加该字段[^4]。 下面是一个完整的例子,展示了如何在一个名为 `employees` 的里添加一个新的字符串类型的字段 `email_address` 并为其设定最大字符数为 50: ```plsql declare num number; begin -- 查询用户拥有的列信息以确认待添加的列是否已存在 select count(*) into num from user_tab_cols where table_name = 'EMPLOYEES' and column_name = 'EMAIL_ADDRESS'; -- 如果查询结果显示数量等于零,则示该列尚未被定义,因此继续执行添加动作 if num = 0 then execute immediate 'ALTER TABLE employees ADD (email_address VARCHAR2(50))'; -- 可选:为新增加的列附加上描述性的注释说明 comment on column employees.email_address is '员工电子邮箱地址'; end if; end; / ``` 这段程序首先尝试查找是否有同名列存在于给定格内;如果没有找到匹配项(即计数值为零),就调用动态 SQL 来修改结构并加入所需的新属性。此外还包含了对所添置的数据成员附加解释文字的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值