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 表名 ;
单元的空气比烟还烂肺😀