1 Fork me on GitHub

41. SQL--alter table语句

1. 前言

sql alter table 语句用来修改、添加、删除、修改与表相关的各种要素,比如:

  • 给表添加新的字段
  • 修改现有字段的名字
  • 修改现有字段的类型
  • 添加约束
  • 删除约束

2. 语法

使用 alter table 为表添加一个新字段的基本语法如下:

alter table table_name add column_name datatype;

使用  alter table 删除表中某个字段的基本语法如下:

alter table table_name drop column column_name;

使用  alter table 修改字段数据类型的基本语法如下:

alter table table_name modify column column_name datatype;

使用  ALTER TABLE 添加 NOT NULL 约束的基本语法如下:

alter table table_name modify column_name datatype not null;

使用  alter table 添加 unique 约束的基本语法如下:

alter table table_name
add constraint myuniqueconstraint unique(column1, column2...);

使用  alter table 添加 check 约束的基本语法如下:

alter table table_name
add constraint myuniqueconstraint check (condition);

使用  alter table 添加主键约束的基本语法如下:

alter table table_name
add constraint myprimarykey primary key (column1, column2...);

使用  alter table 删除 unique 约束的基本语法如下:

 

posted @   v_jjling  阅读(260)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
AmazingCounters.com
点击右上角即可分享
微信分享提示