mysql—表维护

本文详细介绍了SQL中对数据库表的各种操作,包括如何修改表名、改变字符集、清空数据、删除表以及字段的管理。涉及的命令包括ALTER TABLE用于修改表结构,TRUNCATE用于删除所有数据,DROP TABLE用于删除表,以及ALTER TABLE对字段类型、名称的修改和添加、删除字段等。此外,还讲解了主键的管理,包括删除和添加主键以及设置字段为自增。
摘要由CSDN通过智能技术生成

学习网站: https://houdunren.gitee.io/note/

修改表

修改表名

alter table stu rename stus;

别一种操作方式

rename table stus to stu;

修改表字符集

alter table class3 charset gbk;

删除表所有数据

truncate class3;

删除数据表

drop table if exists class2;

字段管理

修改字段类型

alter table stu modify sname char(30) not null;  

修改字段时同时更改字段名

alter table stu change sname name varchar(30) not null;

添加字段

alter table stu add sex smallint default null;

在学生名称后添加邮箱字段

alter table stu add email varchar(50) after sname;

将字段添加到最前面

alter table stu add qq varchar(30) first;

删除学生邮箱字段

alter table stu drop email;

主键操作

一般主键为自增字段,需要删除自增属性后才可以删除主键

alter table stu modify id int not null;

删除主键

alter table stu drop primary key;

添加表主键

alter table stu add primary key(id);

修改为自增列

alter table stu modify id int not null auto_increment;

主键与自增列一起添加

alter table stu modify id int not null auto_increment ,add primary key(id);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值