sql 新建表 alter_SQL ALTER表

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关键字。 现在,让我们考虑以下情况以进行进一步讨论。

  1. Adding a new column to the table

    在表中添加新列
  2. Dropping a column from the table

    从表格中删除一列
  3. Changing data type of a column

    更改列的数据类型
  4. Add a constraint to a table

    向表添加约束
  5. 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

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

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

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

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

ALTER Table For Removing Constraint To Column

ALTER表,用于删除列约束

翻译自: https://www.journaldev.com/25422/sql-alter-table

sql 新建表 alter

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值