MySQL-table基本操作

Table 表的组成

  • 表头 (由多个字段组成)
  • 数据

MySQL数据库 字段(列)常见的数据类型

int / integer : 整数

float / double : 小数

decimal : 代表数字的类型,涉及到 资金的时候,会使用

varchar : 字符串,长度可变的字符串,最多能存 4000多个字符
char : 字符串,固定长度字符串 (不常用)

date : 用来存储日期,精确到 年-月-日
datetime : 用来存储日期, 精确到 年-月-日-时-分-秒
time : 用来存储时间 时-分-秒

text / longtext : 用来存储 大字符串

blob / longblob : 以 流的形式来存储数据、一般存储 图片,音频,视频、word文档等。(不常用)

表的基本操作 DDL

  • 表的创建
  • 表的删除
  • 表的查询
  • 表的修改
  • 查看表结构

表的创建

必须切换到指定的数据库中

use db2022

语法:

create table 表名(
   
   字段名  字段类型  comment 注释信息, 
   ...
   字段名  字段类型 
);

– 创建一个学生表、存储学生的基本信息
在这里插入图片描述

create table t_student(
	
	name varchar(20) comment '用户名' , 
	
	sex char(1) comment '性别', 
	
	stuNo varchar(50) comment '学号', 
	
	birth date comment '出生日期', 
	
	tel varchar(11) comment '手机号',
	
	email varchar(50) comment '电子邮件', 
	
	card varchar(18) comment '身份证号', 
	
	job_time datetime comment '工作时间'   -- school_time
	
);

删除表

在这里插入图片描述

drop table 表名 ; 

查询 当前数据库下 所有的 表

在这里插入图片描述

show tables ;

表的修改 Alter

  • 新增一个字段
  • 修改字段名
  • 修改字段类型
  • 删除一个字段
  • 修改表名

在这里插入图片描述

新增字段

-- 向 t_student表中,新增 age 年龄 字段 

alter table t_student add age int comment '年龄' ;

修改字段名

alter table t_student change job_time school_time datetime comment '入学时间';

修改字段类型

alter table t_student modify school_time date comment '入学时间' ;

PS : 如果要修改的字段 在表中 有 对应的数据,则有可能会 修改失败

删除字段

alter table t_student drop column age ;

修改表名

  • 方式一
alter table t_student rename to student ;
  • 方式二
rename table student to t_student ;

查看表结构

desc 表名 ;

或者

describe  表名 ;


单元的空气比烟还烂肺😀

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值