How can you add a column and assign a value in SQL?

To add a column and assign values to an existing table, you can use the ALTER TABLE statement combined with the UPDATE statement.

To begin with, use the ALTER TABLE statement to add a column. The syntax is as follows:

ALTER TABLE 表名
ADD 列名 数据类型;

For example, in order to add a column called age of integer type to a table named students, you can execute the following statement:

ALTER TABLE students
ADD age INT;

Next, use the UPDATE statement to assign values to the newly added column. The syntax is as follows:

UPDATE 表名
SET 列名 = 值;

For example, to set the age column in the students table to 20, you can execute the following statement:

UPDATE students
SET age = 20;

Before executing the UPDATE statement, make sure there is suitable data available for any new columns being added.

bannerAds