mysql-表的使用-用户管理-表关系

本文介绍了MySQL中表的详细操作,包括表中字段的管理,以及用户管理的重点——通过特殊表mysql.user进行用户管理。此外,文章深入探讨了数据库中的表关系,如多对一、多对多和一对一的关系类型,阐述了如何在MySQL中构建和管理这些关系。
摘要由CSDN通过智能技术生成

一、表的使用

表的详细操作
1. 修改表名
alter table 旧表名 rename 新表名;

2. 修改表的引擎与字符编码
alter table 表名 engine="引擎名" charset="编码名";

3. 复制表
#结构
create table 新表名 like 旧表名;
eg:1
create table nt like tt; # 将tt的表结构复制到新表nt中,约束条件一并复制

eg:2
create table nt1 select * from tt where 1=2; # 将tt的表结构复制到新表nt1中,约束条件不会复制

#结构+数据
create table 新表名 select * from 旧表名;
注:会复制表结构+数据,但不会复制约束条件

4.清空表
truncate 表名
注:表被重置,自增字段重置
表中字段的详细操作
create table t2(
	id int primary key auto_increment,
    x int,
    y int
);
insert into t2(x, y) values(10, 20), (100, 200), (1000, 2000);

1. 修改字段信息
alter table 表名 modify 字段名 类型[(宽度) 约束];
alter table t2 modify x bigint default 0; #模式不同,涉及精度问题

2. 修改字段名及信息
alter table 表名 change 旧字段名 新字段名 类型[(宽度) 约束]; # 模式不同,涉及类型转换问题

3. 添加字段名
#末尾添加
alter table 表名 add 字段名 类型[(宽度) 约束], ..., add 字段名 类型[(宽度) 约束];
alter table t2 add age int, add gender enum("male", "female", "wasai") default "wasai";

#首尾添加
alter table 表名 add 字段名 类型[(宽度) 约束] first;
alter table t2 add yyy int first;

#指定位添加:指定字段后
alter table 表名 add 字段名 类型[(宽度) 约束] after 旧字段名;
alter table t2 add y int after x;

4. 删除字段名
alter table 表名 drop 字段名;
alter table t2 drop y;

二、用户管理

特殊表(mysql.user) => 用户管理
  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值