Get the App
SLTechnology News&Howtos  ›  Database  › 

How to write the statement of adding field in sql

Shulou Source: shulou.com Published: 2022-05-31 23:52:46 10月03日 Update

This article mainly introduces how to write the sentence of adding fields in sql, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

What is the statement for adding fields to sql?

The standard SQL statement for adding a field to a data table is written as follows:

Alter table table name add (field type) [default 'input default'] [null/not null]

For example:

ALTER TABLE employee ADD spbh varchar (20) NOT NULL Default 0

It means to add the field spbh to the table employee, which is of type varchar, size 20, and is not allowed to be empty, and the initial default value is 0.

Extended data:

Other commonly used SQL statements:

1. Modify a field property in the data table and add comments to it.

Statement format:

Comment on column library name. Table name. Field name is' comments entered'

Example: if I want to add a comment to the document_type field of the test table in the ers_data library, the sql statement is:

Comment on column ers_data.test.document_type is' File Type'

2. Modify a field type in the data table.

Statement format:

Alter table table name modiy (field type [default 'input default'] [null/not null], field type [default 'input default'] [null/not null])

Modify multiple fields separated by commas.

Example: if you want to change the type of office classroom of a field in the teacher teacher table to char (20), and the default value is "office", the corresponding sql is:

ALTER TABLE teacher ALTER COLUMN classroom VARCHAR (20) NOT NULL default "Office"

3. Delete a field in the data table.

Statement format:

Alter table table name drop (field)

Example: to delete the field age in table student, you can use the following sql:

Alter table student drop age; thank you for reading this article carefully. I hope the article "how to write sentences for adding fields in sql" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Fields statements types data datasheets articles inputs offices comments formats examples offices values interests writing at the same time multiple size that is attributes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia macOS vpn Huawei