sql 新建表 alter
We have discussed how to create a table in the database, but there are cases when we would like to modify the table that was created. In order to modify a table SQL provides ALTER as the keyword. We will try to understand the usage of the ALTER keyword for altering a table in detail.
我们已经讨论了如何在数据库中创建表 ,但是在某些情况下,我们想修改已创建的表。 为了修改表,SQL提供了ALTER作为关键字。 我们将尝试了解ALTER关键字用于详细更改表的用法。
SQL ALTER表 (SQL ALTER Table)
There are cases when we designed and created a table based on the existing need of the application. Consider in future the requirement is changed and we need to make modifications in the existing table. In such cases, we would be using the ALTER keyword. Let us now consider the following scenarios for further discussion.
在某些情况下,我们会根据应用程序的现有需求设计和创建表。 考虑将来需要更改的情况,我们需要在现有表中进行修改。 在这种情况下,我们将使用ALTER关键字。 现在,让我们考虑以下情况以进行进一步讨论。
- Adding a new column to the table 在表中添加新列
- Dropping a column from the table 从表格中删除一列
- Changing data type of a column 更改列的数据类型
- Add a constraint to a table 向表添加约束
- Remove constraint from the table. 从表中删除约束。
Note: All the scenarios and queries mentioned below are for MySQL database.
注意 :下面提到的所有方案和查询均适用于MySQL数据库。
We will consider the following Customer table for further discussion.
我们将考虑以下客户表以进行进一步讨论。
CREATE TABLE `test`.`customer` ( `CustomrId` INT NOT NULL, `CustomerName` VARCHAR(45) NULL, `ProductId` VARCHAR(45) NOT NULL, `State` VARCHAR(45) NULL, PRIMARY KEY (`CustomrId`, `ProductId`), UNIQUE INDEX `CustomrId_UNIQUE` (`CustomrId` ASC) VISIBLE);
1.在表中添加新列 (1. Adding a New Column to the table)
Syntax
句法
ALTER TABLE table_name ADD column_name datatype;
In the syntax above, the ALTER TABLE is the keyword that tells the database that a modification is needed in the table and the ADD keyword tells that a column addition needs to be done.
在上面的语法中,ALTER TABLE是关键字,它告诉数据库表中需要修改,而ADD关键字则表明需要完成列添加。
Example
例
Adding a customer_age column of int datatype in the Customer table.
在客户表中添加int数据类型的customer_age列。
ALTER TABLE test.customer ADD customer_age int;
![ALTER Table For Adding Column](https://cdn.journaldev.com/wp-content/uploads/2019/02/ALTER-Table-for-adding-Column.jpg)
SQL ALTER Table For Adding Column
SQL ALTER表用于添加列
2.从表中删除一列 (2. Dropping a Column from Table)
Syntax
句法
ALTER TABLE table_name DROP COLUMN column_name;
The DROP COLUMN keyword tell that a column needs to be deleted.
DROP COLUMN关键字表明需要删除列。
Example: –
示例:–
Removing customer_age column of int datatype in the Customer table.
在客户表中删除int数据类型的customer_age列。
ALTER TABLE test.customer DROP COLUMN customer_age;
![ALTER Table For Dropping Column](https://i-blog.csdnimg.cn/blog_migrate/a44dc50feebe32099465c4d2f564e35f.png)
ALTER Table For Dropping Column
ALTER滴塔表
3.更改列的数据类型 (3. Changing Data Type of a Column)
Syntax: –
句法: -
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
MODIFY COLUMN keyword tells the database that a column needs to be modified.
MODIFY COLUMN关键字告诉数据库需要修改列。
Example: –
示例:–
Changing the data type of productId from Varchar to Int.
将productId的数据类型从Varchar更改为Int。
ALTER TABLE test.customer MODIFY COLUMN ProductId int;
![ALTER Table For Modify Column](https://cdn.journaldev.com/wp-content/uploads/2019/02/ALTER-Table-for-modify-Column.jpg)
ALTER Table For Modify Column
修改列的ALTER表
4.将约束添加到表 (4. Add a Constraint to a Table)
Syntax: –
句法: -
ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...columnN);
ADD CONSTRAINT keyword tells the database that a constraint needs to be added in the table. The UNIQUE keyword tells that the column after the UNIQUE keyword will have unique values in it.
ADD CONSTRAINT关键字告诉数据库需要在表中添加约束。 UNIQUE关键字告诉UNIQUE关键字后面的列将具有唯一值。
Example: –
示例:–
Adding MyUniqueConstraint to the Customer table.
将MyUniqueConstraint添加到客户表。
ALTER TABLE test.customer ADD CONSTRAINT MyUniqueConstraint UNIQUE(CustomerName);
![ALTER Table For Adding Constraint To Column](https://i-blog.csdnimg.cn/blog_migrate/9bf6db80ddae46f3eddb7b45efcee580.png)
ALTER Table For Adding Constraint To Column
ALTER表,用于向列添加约束
5.从表中删除约束 (5. Remove Constraint from the Table)
Syntax: –
句法: -
ALTER TABLE table_name DROP CONSTRAINT MyUniqueConstraint;
In the syntax above, the DROP CONSTRAINT keyword tells the database that a constraint needs to be removed from the table.
在上面的语法中,DROP CONSTRAINT关键字告诉数据库需要从表中除去约束。
Syntax for MySQL:-
MySQL语法:
ALTER TABLE table_name DROP INDEX MyUniqueConstraint;
Example: –
示例:–
Removing MyUniqueConstraint from Customer table.
从客户表中删除MyUniqueConstraint。
If you are using MySQL following Query will help.
如果您使用的是MySQL,则以下Query会有所帮助。
ALTER TABLE test.customer DROP INDEX MyUniqueConstraint;
![ALTER Table For Removing Constraint To Column](https://i-blog.csdnimg.cn/blog_migrate/746dac4bc6763d5124892562480e33a6.png)
ALTER Table For Removing Constraint To Column
ALTER表,用于删除列约束
sql 新建表 alter