SQL Server Modify table structure syntax Daquan

Source: Internet
Author: User

1. Add fields

  Alter table docdsp  addchar($)


2. Delete a field

Alter Table Drop column column_name


3. Modify the field type

Alter Table table_name    altercolumn column_name new_data_type


2.6.1. Adding fields

To add a field, use this command:

Alter Table Add column text;

The new field is initially populated with null values for rows that already exist in the table.

You can also define constraints on the field at the same time, using the usual syntax:

Alter Table Add column text Check <> ");

A new field cannot be constrained with a non-null constraint, because the field must initially contain null values. But you can add a non-empty constraint later. Similarly, you cannot define a default value on a new field. Based on the description of the SQL standard, this requires that the new field of the existing row be populated with the default value, which is not yet implemented. But you can adjust the field defaults later.
2.6.2. Deleting a field

To delete a field, use this command:

Alter Table Drop column description;

2.6.3. Adding constraints

To add a constraint, use the table constraint syntax. Like what:

Alter Table Add Check <> "' ); Alter Table Add constraint Unique (product_no); Alter Table Add Foreign Key references product_groups;

To add a non-null constraint that cannot be written as a table constraint, use the following syntax:

Alter Table Alter column Set  not null;

This constraint is checked immediately, so the table must conform to the constraint before adding the constraint.
2.6.4. Deleting a constraint

To delete a constraint, you need to know its name. If you give it a name, it's a good thing. Otherwise the system assigns a generated name, so you need to find it. Psql's command \d tablename here to help; other interfaces may also provide a way to check the details of the table. And then this is the command:

Alter Table Drop constraint some_name;

All constraint types are used in addition to non-null constraints. To delete a non-empty type, use the

Alter Table Alter column Drop  not null;

(Remember that the non-null constraint has no name.) )
2.6.5. Changing the default value

To set a default value for a field, use a command such as the following:

Alter Table Alter column Set default 7.77;

To delete the default value, use the

Alter Table Alter column Drop default;

This is equivalent to setting the default to null, at least in PostgreSQL. As a result, if we delete a default value that has not yet been defined, it is not an error, because the default implication is a null value.
2.6.6. Changing a name to a field

Rename a field:

Alter Table column  to Product_number;

2.6.7. Changing a name to a table

 to Table : Alter Table  to items;

SQL Server Modify table structure syntax Daquan

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.