English 中文(简体)
在矿石中添加一栏的自动生成数
原标题:insert auto generated number of one column into another column in oracle sql

I have a database table with two columns customer_id Number and first_name VARCHAR2
For customer_id, the number will be auto incremented. I need first_name with the value of customer_id in a specific format.

See the below example:
image

If the customer_id is auto incremented to value 1, the first_name should be NAME-1 if the customer_id is auto incremented to value 2, the first_name should be with NAME-2

The only feasible option I got so far is performing 3 sql statements.
One for insert and the next one is to read the incremented value and perform update to the first_name column.

I greatly appreciate the help and guidance if anyone can suggest a better way of handling this in minimum possible sql queries.

问题回答

One option is to create an identity column (for the ID column) and a database trigger which sets first name to desired value.

表:

SQL> create table test (customer_id number generated always as identity,
  2                     first_name varchar2(10));

Table created.

Trigger:

SQL> create or replace trigger trg_bi_test
  2    before insert on test
  3    for each row
  4  begin
  5    :new.first_name := :new.first_name || - || :new.customer_id;
  6  end;
  7  /

Trigger created.

测试:

SQL> insert into test (first_name) values ( Scott );

1 row created.

SQL> insert into test (first_name) values ( Mike );

1 row created.

结果:

SQL> select * from test;

CUSTOMER_ID FIRST_NAME
----------- ----------
          1 Scott-1
          2 Mike-2

SQL>

由于“NAME-1”的体质,或者使用一种表述:

select
  *,
  concat(name,  - , id) as name_id
from mytable

或形成一种观点:

create view mytable_x as (
  select
    id,
    name as raw_name,
    concat(name,  - , id) as name,
    -- other columns
  from mytable
)

希望:

select Customer_id, concat(first_name, CONCAT( - , customer_id)) as first_name
from Customers;




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签