DB2 Database Basic Add Delete table field summary

Source: Internet
Author: User
Tags db2

1. Add a field

ALTER TABLE [table_name] add [column_name] [Column_type]

2. Change the field type

ALTER TABLE [TABLE_NAME] ALTER COLUMN [COLUMN_NAME] Set data type [Column_type]

Note: Changes to the field type are limited, such as changing the field to be larger than the previous type, if you want to change the size or change the decimal length, you must first drop the original column and then add it again.

For example, I want to change the column of a varchar (10) to varchar (6) or change the column of a decimal (16, 2) to Decimal (16, 4), and so on, all of which cannot be modified using the above statement, but also to a different type. You also need to drop the column first.

3. Remove the field

ALTER TABLE [TABLE_NAME] drop column [column_name]

Note: After dropping the field, it may cause the table query/insert operation to not execute, and the table needs to be reorg.

4. Add a default value for a field

ALTER TABLE [TABLE_NAME] ALTER COLUMN [COLUMN_NAME] SET default [value]

5. Add a field with default values

ALTER TABLE [TABLE_NAME] Add column [column_name] [column_type] NOT NULL with default [value]

6. Set the default time for the field to the current time

ALTER TABLE [TABLE_NAME] ALTER COLUMN [COLUMN_NAME] set default current date;

DB2 Database Basic Add Delete table field summary

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.