📜  SQL中ALTER和UPDATE命令的区别

📅  最后修改于: 2021-09-09 11:32:48             🧑  作者: Mango

1. 更改命令:

ALTER SQL 命令是一个 DDL(数据定义语言)语句。 ALTER 用于更新数据库中表的结构(如添加、删除、修改数据库中表的属性)。

句法 :

// add a column to the existing table

ALTER TABLE tableName

ADD columnName columnDefinition;

 

// drop a column from the existing table

ALTER TABLE tableName

DROP COLUMN columnName;



// rename a column in the existing table

ALTER TABLE tableName

RENAME COLUMN olderName TO newName;



// modify the datatype of an already existing column in the table

ALTER TABLE table_name

ALTER COLUMN column_name column_type;

2. 更新命令:

UPDATE SQL 命令是一个 DML(数据操作语言)语句。它用于操作任何现有列的数据。但不能改变表的定义。

句法 :

// table name that has to update

UPDATE tableName

// which columns have to update

SET column1 = value1, column2 = value2, ...,columnN=valueN.

// which row you have to update

WHERE condition

注意:如果没有 WHERE 子句,表中的所有记录都会被更新。

SQL 中 ALTER 和 UPDATE 命令的区别:

SR.NO ALTER Command UPDATE Command
1 ALTER command is Data Definition Language (DDL). UPDATE Command is a Data Manipulation Language (DML).
2 Alter command will perform the action on structure level and not on the data level. Update command will perform on the data level.
3 ALTER Command is used to add, delete, modify the attributes of the relations (tables) in the database. UPDATE Command is used to update existing records in a database.
4 ALTER Command by default initializes values of all the tuple as NULL. UPDATE Command sets specified values in the command to the tuples.
5 This command make changes with table structure. This command makes changes with data inside the table.
6 Example : Table structure, Table Name, SP, functions etc. Example : Change data in the table in rows or in column etc.