<5>mysql:数据表操作

表操作

命令 作用
show tables [like <expression>]; 查看当前数据库中所有的表
create table [if not exists] <tbname>(<column> <type> <constraint>, ...); 创建表
create table [if not exists] <tbname> as <tbname>; 复制表
truncate table [if exists] <tbname>; 删除表数据
drop table [if exists] <tbname>; 删除表
alter table [if exists] <tbname> <alter content>; 修改表结构
describe/desc [if exists] <tbname>; 查看表结构

说明

命令 作用
like <expression> 使用模糊匹配,过滤数据库
if not exists 数据库不存在时,才进行操作
if exists 数据库存在时,才进行操作
type 数据类型,见下方type说明
constraint 数据类型,跳转详细说明
alter content 表修改内容,见下方alter content说明

type

数据类型 说明
int 整型
float 浮点型
numeric(n,s) 浮点型,n-总长度,s-小数长度
decimal(n,s) 浮点型,n-总长度,s-小数长度
char(n) 定长字符型,n-长度
varchar(n) 可变长字符型,n-长度

alter content

alter content 说明
add <column> <type> <constraint> 增加字段
modify <column> <type> 修改字段数据类型
modify <column> <type> first 修改字段位置为第一个
modify <column> <type> after <column> 修改字段位置为目标字段后面
change <column> <name> <type> 修改字段名称、类型
rename <dbname> 修改表名
drop <column> 删除字段
posted @ 2022-10-20 08:28  漓白  阅读(23)  评论(0编辑  收藏  举报