Sql-alter-change and Modify Differences

Source: Internet
Author: User

Alter for column application: 1. Change the format of column names: changes old_col_name new_col_name column_definition     Preserve old and new column names: A---->b column type ALTER TABLE T1 change a b INTEGER;     2. Change the column type without changing the column name to preserve the column name of old and new, even if it is the same. ALTER TABLE T1 change b b BIGINT not NULL;         3. Change the column type without changing the column name.     Old column names are not preserved. ALTER TABLE T1 MODIFY b BIGINT not NULL; Note: < 1> When using change or MODIFY, the definition of a column must include: the type of data and the attributes applied to the column, but not: index, for example: PRIMARY KEYOr UNIQUEFor example: the definition of a column: INT UNSIGNED DEFAULT 1 COMMENT ‘my column‘Change: Alter TABLE t1 MODIFY col1 BIGINT UNSIGNED DEFAULT 1 COMMENT ' my column ';<2> When you use change or modify to alter the data type, the value of the column is also converted to the corresponding column type whenever possible. Data may be lost, preferably set before using ALTER TABLE; strict SQL mode <3> Adds a column of the specified position, which can be First,after column_name, <4> use change  , modify shortens the length of the column, the column length is less than the corresponding index length, and the index is shortened automatically.           4. Change the default value for the column: Alter TABLE tb_name ALTER COLUMN_NAME. CREATE TABLE test2 (id INT, NAME VARCHAR (4));          Without specifying a default value, after the table is built: default NULL.           Change default value: Alter TABLE test2 alter name SET default '; Note: Changing the default value is only valid for the newly inserted data after the change. The previous data in the table, null or null, empty or empty.

Sql-alter-change and Modify Differences

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.