Some new features have been added to v5r2 OS/400 SQL. One of these new features is introduced here: Automatic Generation of ID columns.
The solution is as follows:
1. Enter the strsql command on the AS/400 command line.
2. Enter the following command to create a new table:
Create Table xqlib/employee1 (
Empno int generated always as identity,
Name char (20 ),
Salary int,
Bonus int
)
The predicate "generated always as identity" indicates that empno is an automatically labeled column.
3. input values using the insert statement:
Insert into xqlib/employee1 (name, salary, bonus)
Values ('bill)
Note:
The insert into statement cannot be used to specify a value for the empno column. By default, the value of this column starts from 1 and increments with 1 as the step size. You can also customize it according to your requirements, such:
"Generated always as identity (start with 100, increment by 10)", meaning "auto-marking column starts with 100, step size increases by 10"
View the result, as shown in:
----------------------------------------- Display data
Position to line .....
.... + .... 1 .... + .... 2 .... + .... 3 .... + .... 4 .... + .... 5 .... + .... 6 .... +...
---- Empno ---- name -------- salary ---- bonus
-------- 1 ---- George ------ 20,000 ---- 2,000
-------- 2 ---- Bill -------- 30,000 ---- 5,000
-------- 3 ---- Tom -------- 25,000 ---- 3,000
-------- 4 ---- Smith ------- 10,000 ---- 6,000
-------- 5 ---- black ------- 15,000 ---- 5,000
-------- 6 ---- grant ------- 25,000 ---- 5,000
-------- 7 ---- John -------- 25,000 ---- 5,000
* ******* End of Data ********
In the figure, the empno column automatically generates the ID column.