Chinaunix首页 | 论坛 | 博客
  • 博客访问: 549694
  • 博文数量: 154
  • 博客积分: 4055
  • 博客等级: 上校
  • 技术积分: 1381
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-01 14:26
文章分类

全部博文(154)

文章存档

2014年(2)

2013年(2)

2011年(2)

2010年(11)

2009年(9)

2008年(35)

2007年(22)

2006年(71)

我的朋友

分类: Oracle

2007-11-08 22:14:35

SQL> spool virtual_columns.log

SQL> CREATE TABLE employees (
  2    id          NUMBER,
  3    first_name  VARCHAR2(10),
  4    last_name   VARCHAR2(10),
  5    salary      NUMBER(9,2),
  6    column1       NUMBER(3),
  7    column2       NUMBER(3),
  8    salary1     AS (ROUND(salary*(1+column1/100),2)),
  9    salary2     NUMBER GENERATED ALWAYS AS (ROUND(salary*(1+column2/100),2)) VIRTUAL,
 10    CONSTRAINT employees_pk PRIMARY KEY (id)
 11    );
 
SQL> insert into employees(id,first_name,last_name,salary,column1,column2)
  2*    values(1,'zhang','li',1000,5,10);

1 row created.

SQL> INSERT INTO employees (id, first_name, last_name, salary, column1, column2)
  2  VALUES (2, 'hbjx', 'zl', 2000, 10, 20);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from employees;

        ID FIRST_NAME LAST_NAME      SALARY    COLUMN1    COLUMN2    SALARY1    SALARY2
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
         1 zhang      li               1000          5         10       1050       1100
         2 hbjx       zl               2000         10         20       2200       2400

SQL> desc employees;
 Name                                                              Null?    Type
 ------------------------------------------------------- --------
 ID                                                                NOT NULL NUMBER
 FIRST_NAME                                                                 VARCHAR2(10)
 LAST_NAME                                                                  VARCHAR2(10)
 SALARY                                                                     NUMBER(9,2)
 COLUMN1                                                                    NUMBER(3)
 COLUMN2                                                                    NUMBER(3)
 SALARY1                                                                    NUMBER
 SALARY2                                                                    NUMBER

SQL> col column_name for a30
SQL> col data_type for a40
SQL> select column_name,data_type from user_tab_columns where table_name like 'EMPLOYEES';

COLUMN_NAME                    DATA_TYPE
------------------------------ ----------------------------------------
ID                             NUMBER
FIRST_NAME                     VARCHAR2
LAST_NAME                      VARCHAR2
SALARY                         NUMBER
COLUMN1                        NUMBER
COLUMN2                        NUMBER
SALARY1                        NUMBER
SALARY2                        NUMBER

8 rows selected.

SQL> col data_default for a40
SQL> l
  1* select column_name,data_type from user_tab_columns where table_name like 'EMPLOYEES'
SQL> c /type/default
  1* select column_name,data_default from user_tab_columns where table_name like 'EMPLOYEES'
SQL> /

COLUMN_NAME                    DATA_DEFAULT
------------------------------ ----------------------------------------
ID
FIRST_NAME
LAST_NAME
SALARY
COLUMN1
COLUMN2
SALARY1                        ROUND("SALARY"*(1+"COLUMN1"/100),2)
SALARY2                        ROUND("SALARY"*(1+"COLUMN2"/100),2)

8 rows selected.
 
阅读(958) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~