MySql基础语法第二篇表的操作

本文详细介绍SQL中表的创建、查看、修改及删除等关键操作,包括字段类型、约束、注释及存储引擎设置,适用于数据库管理和开发人员快速掌握表结构管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建表 关键字:create table 表名
create table 表名(
字段1 类型 [字段类型],
字段n 类型 [字段类型]

create table test(
	id 	int(10)not null  auto_increment  comment  'id',
	name 	varchar(20) not null  comment  '名字',
	age int(3)default 18 comment '年级',
	primary key(id)
)engine=InnoDB default charset utf8 comment '测试表'

1.test 表名
2.not null 字段不可以为空
3.int(10) varchar(20) 字段类型
4.default 18 字段默认值为18
5.comment 备注信息
6.engine 存储类型InnoDB
7.charset 字符集 utf8
8.auto_increment 字段自增 一般适用于主键
9.primary key主键

**2.**查看表 关键字:show

show tables  查看数据库下所有数据表

3.模糊查询数据表

show tables like '%数据库表名%'

4.查看表创建语句

show create table '数据表名'

5.查看表结构

desc 表名

6.删除数据表

drop table if exists 表名

7.修改表名

alter table 旧表名 rename to 新表名

8.增加一列 关键字 add

alter table 表名 add 新列名  字段类型   字段选项
例如:
alter table test add  age int(3) default 18 not null comment '年龄'

9.删除一列 关键字 drop

alter table test drop age

10.修改字段类型

alter table 表名 modify 字段名  新的字段类型   新的字段选项
例如:
alter table test modify age char(3) default '12' not null 

11.字段排到最前 关键字 first

alert table 表名 modify 字段名 类型  字段选项 comment  '最前面' first
例如:
alert table test modify age int not null comment  '最前面' first

12.修改字段排序

    alter table 表名  modify 字段名1  字段类型  字段选项  after  字段名2
    例如:
    
   alter table test modify age int not null after name

13.字段重命名

alter table 表名 change 原字段名 新字段名 新的字段类型 [新的字段选项];
例如:
alter table test change name  stu_name varchar(20) not null  comment '学生姓名'

14.修改表信息选项

alter table 表名 表选项信息
例如:
alter table test engine MyIsam default charset gbk;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值