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;

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滴塔表

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表

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表,用于向列添加约束

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表,用于删除列约束

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

sql 新建表 alter

sql 新建表 alter_SQL ALTER表相关推荐

  1. SQL Server建库建表命令

    数据库建库建表 1.直接右键数据库,选择新建数据库: 2.通过新建查询,输入命令建库建表. 使用CREATE DATABASE创建数据库school. 数据文件的逻辑名称自定义,需要注意的点是主数据文 ...

  2. SQL server学习日志(二)创建表!手把手教你创建表,修改表,了解数据类型!超详细!

    一.简单了解表(创建表之前一定要先了解数据类型与约束哦,这样我们才能创建正确的表!) 1.定义:基本表是数据库中组织和管理数据的基本单位,数据库的数据保存在一个个基本表中. 对于关系型数据库系统而言, ...

  3. SQL学习(3)——表的复杂查询与函数操作

    SQL学习(3)--表的复杂查询 1.视图 1.1.创建视图 1.1.1.基于单表的视图 1.1.2.基于多表的视图 1.2.查询视图 1.3.修改视图 1.4.更新视图 1.5.删除视图 2.子查询 ...

  4. sql查询:单表、多表、左连接、外连接、高级查询

    sql查询 一.sql语句 标准SQL包含了4种基本的语句类别: (1)DDL语句,数据定义语句,主要用来定义数据库,表名,字段,例如create,drop,alter. (2)DML语句,数据操作语 ...

  5. mybatis对数据库的操作,删除表,新建表,修改表。

    mybatis对数据库操作 事情是这样的,今天一不小心接到一个这样的需求,调用远程接口获取数据,每周定时更新全部数据,这不挺好整的嘛!!!!!!!!! 但是,更新之前需要把之前的那个表的数据做备份,把 ...

  6. sql server 数据库分区分表

    sql server 数据库分区分表 作为演示,本文使用的数据库 sql server 2017 管理工具 sql server management studio 18,,创建数据库mytest,添 ...

  7. mysql 更改建表语句_MySql:如何通过Sql语句创建表,更改表?,这几步你要了解...

    mysql在网站开发中,越来越多人使用了,因为方便部署,方便使用. 我们在平时使用中,通常都是通过客户端软件去创建表和更改表,比如用 Navicat.很多人都不会用sql语句去创建表,或者语法忘记了. ...

  8. db2关闭下一句sql的日志_MySQL性能管理及架构设计:SQL查询优化、分库分表

    作者:唐成勇 来源:https://segmentfault.com/a/1190000013781544 一.SQL查询优化(重要) 1.1 获取有性能问题SQL的三种方式 通过用户反馈获取存在性能 ...

  9. SQL Server 2014 内存优化表(1)实现内存优化表

    内存优化表(Memory-Optimized Tables)是SQL Server 2014的新特性,目前仅适用于评估版(Evaluation Edition).开发版(Developer Editi ...

最新文章

  1. php html 变量,PHP与HTML混编,使用PHP变量代替数据--20190221
  2. 把 Bug 晾几天就能解决了!!! | 每日趣闻
  3. ubuntu apache php mysql phpmyadmin_Ubuntu下Apache+PHP+MySQL+phpMyAdmin的快速安装步骤
  4. 用diag直接使用错误_用python学量子力学(1)
  5. FormView在什么情况下自动生成模板项?
  6. #3551. [ONTAK2010]Peaks加强版(kruskal 重构树 + 主席树)
  7. Mac/Ubuntu 上编译、搭建 WebRtc/licode 服务器
  8. SSM(Spring+Spring MVC+Mybatis)整合 1:整体概述、目录内容及实验环境介绍
  9. mysql 开启事务_MySQL可重读隔离级别的底层实现原理
  10. 使用C#和Excel进行报表开发(5)
  11. 手机麦克风声音太大_手机麦克风没声音怎么设置?瞬间声音变大,一键设置即可...
  12. 区块链架构与扩容方案
  13. Unity3D上路_01-2D太空射击游戏
  14. 为了找出羞羞视频,百度云居然有“娇喘模式”
  15. 显示 wordpress 文章摘要函数the_excerpt
  16. 两句css 搞定页面滚动时的卡顿问题?
  17. Rviz中控制机器人模型运动(arbotix)
  18. Android Window系列(一)- window与decorview
  19. java 拉姆达 lamdba get
  20. 一个很好的机会股票的价格是向南移动

热门文章

  1. oracle函数listagg的使用说明(分组后连接字段)
  2. [转载] python 短网址_使用Python生成url短链接的方法
  3. [转载] Python数据分析之Matplotlib数据可视化实例
  4. 聊一聊FPGA的片内资源相关知识
  5. 项目案例模板之登录注册的实现
  6. 在consul上注册web服务
  7. 在LINUX上部署SOFA
  8. python实现:用类实现一个图书馆,实现借书,入库,还书,查书,等功能,要求数据可以保存到文件中,退出后下次可以找回数据...
  9. maven 项目 spring mvc + jdbc 配置文件
  10. 汇编语言学习之汇编语言源程序的输入