Oracle getting started Tutorial: alter usage of db2

Source: Internet
Author: User

I am used to the alter statements of Oracle and cannot use these statements in db2.

The example table is as follows:

Create table emp (id int, name char (20), salary decimal (); -- create table emp

The alter statement usage of db2 is as follows:

(1) Add Columns

Alter table emp add sex char (1); --- add sex column

(2) Change the field type

Alter table emp alter column sex set data type char (3); --- change the sex column to char (3)

Alter table emp alter column sex set data type char (2); --- change the sex column back to char (2)

Insert into emp values (1, 'Smith ', 18.23, '22'); --- insert a record to the emp table

Alter table emp alter column sex set data type char (3); --- modify the sex column to char (3) if there is a record.

Alter table emp alter column sex set data type char (1); --- change the sex column to char (1) when there is a record ), failed --- "the specified attribute is incompatible with the existing column"

Alter table emp alter column sex set data type decimal (); --- modify the sex column to decimal () when there is a record ), success (originally '22' was converted to 22.00)

Alter table emp alter column sex set data type char (2); --- modifying the sex column back to char (2) failed --- "the specified attribute is incompatible with the existing column"

Alter table emp alter column sex set data type char (5); --- modify the sex column to char (5) successfully (the original number 22.00 is converted to the character '22. 00', 5 bytes)

Delete from emp; insert into emp values (1, 'Smith ', 18.23, 'dd'); ---- delete the original data and insert new data

Alter table emp alter column sex set data type decimal (); --- modify the sex column to decimal () when there is a record ), failed --- "the specified attribute is incompatible with the existing column" ('dd' and number are incompatible)

The preceding statement is run on windows + db2v9.5.

As with the above statements, I should be able to have a more detailed understanding of the alter Statement of db2,

To change the db2 type,

(A) Ensure that the target type and all data types in the current table are compatible. For example, if you want to change char to decimal, the original field cannot contain characters, for example, abcd can only contain data characters, such as 1, 2, and 3. Otherwise, db2 reports an error saying "The format is not compatible ".

(B) If you want to change only the size while the data type remains unchanged, ensure that the field is increased, such as from char (5) To char (10 ), decimal () to decimal.

To reduce the size of a field, make sure that the data in all tables is smaller than the value you have reduced. Otherwise, an error is returned. If you need to change the value, you can only delete the column and recreate the column. Of course, you can also recreate the table.

(3) Delete A column

Alter table emp drop column sex;

(4) add default values for fields

Alter table emp add sex char (1 );

Alter table emp alter column sex set default 'M ';

(5) add fields with default values

Alter table emp add addr char (20) not null with default 'beijing ';

 

In addition, when adding, deleting, or modifying a field, you are advised to run the reorg command to ensure security.

Reorg table table_name;

Otherwise, other operations may be reported with an error.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.