表关系+表的详细操作+字段详细操作+特殊表--day40

表的详细操作

'''
1.修改表名
alter table 旧表名 rename 新表名;
​
2.修改表的引擎与字符编码
alter table 表名 engine="引擎名" charset="编码名";
​
3.复制表 *
# 结构
create table 新表名 like 旧表名;
eg:1
# 将tt的表结构复制到新表nt中, 约束条件一并复制
create table nt like tt;

eg:2
# 将tt的表结构复制到新表nt1中, 约束条件不会复制
create table nt1 select * from tt where 1=2; 
​
# 结构+数据
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 旧字段名 新字段名 类型[(宽度) 约束];
alter table t2 change y c char(10) not null; # 模式不同, 涉及类型转换问题
​
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 表名 add 字段名 类型[(宽度) 约束] after 旧字段名;
alter table t2 add y int after x;
​
4.删除字段名
alter table 表名 drop 字段名;
alter table t2 drop y;
'''

特殊表 (mysql.user) => 用户管理(*****)

'''
# 操作前提:登录root用户
​
1.重要字段
Host | User | Password
​
2.新建用户
create user 用户名@主机名 identified by '密码'; # 正确
create user zero@localhost identified by 'zero';

# 错误​
注:insert into mysql.user(Host,User,Password) values("主机名","用户名",password("密码")); 
​
3.设置用户权限
grant 权限们 on 数据库名.表名 to 用户名@主机名 [with grant option];
grant create on db1.* to zero@localhost with grant option;
注:权限有select,delete,update,insert,drop..., all代表所有权限
注:数据库名,表名可以用*替换,代表所有
注:设置权限时如果没有当前用户,会自动创建用户,提倡使用
重点: grant all on db1.* to owen@localhost identified by 'owen'; # (创建用户)设置权限
​
4.撤销权限
revoke 权限名 on 数据库名.表名 from 用户名@主机名;
revoke delete on db1.* from owen@localhost;
​
5.修改密码
set password for 用户名@主机名 = password('新密码');
set password for owen@localhost = password('123');
​
6.删除用户
drop user 用户名@主机名;
'''

表关系

社会中存储需要可以构建成表的数据, 它们形成的表,往往之间存储某种或某些社会关系,
mysql数据库建立表结构就是社会中产生的各种数据, 分门别类管理 但mysql建立的(代码层次的)表之间, 同样需要处理表与表之间的关系 形成了 多对一
| 多对多 | 一对一 三种关系 多对一 ''' 案例:员工employees表 | 部门department表 ​ 建表规则: 先建立主表,再建立从表,在从表中设置主表的唯一字段(通常为主键)作为外键 ​ 建表语法: create table 主表( id int primary key auto_increment, ... ); create table dep( id int primary key auto_increment, name varchar(16), work varchar(16) ); create table 从表( id int primary key auto_increment, ... 主表_id int, # 只是在从表中起了一个名字, 该名字和主表主键对应,所有起了个见名知义的名字 foreign key(主表_id) references 主表(唯一字段名id) on update cascade on delete cascade ); create table emp( id int primary key auto_increment, name varchar(16), salary float, dep_id int, foreign key(dep_id) references dep(id) # 设置关联 on update cascade # 设置级联 on delete cascade ); ​ 插入记录规则: 先插入主表数据,再插入从表数据 insert into dep values(1, '市场部', '销售'), (2, '教学部', '授课'); insert into emp(name, salary, dep_id) values('egon', 3.0, 2), ('yanghuhu', 2.0, 2),('sanjiang', 10.0, 1),('owen', 88888.0, 2), ('liujie', 8.0, 1); ​ 更新删除数据: 两表间相互影响,先从依赖数据入手,再进行更新删除操作 eg:1 删除主表dep中一个部门 delete from dep where id=1; => 从表emp中属于该部门的员工都被删除了 ​ 更新从表emp中一个员工的部门 update emp set dep_id=3 where name='egon'; <= 部门必须存在 insert into dep values(3, '管理部', '吃饭睡觉打豆豆, 明确团队方针'); '''

多对多

'''
案例:作者author表 | 书book表
​
建表规则:
新建第三张表,通过两个外键形成多对多关系
​
建表语法:
create table 表1(
id int primary key auto_increment,
...
);
create table book(
id int primary key auto_increment,
name varchar(16),
price int
);
create table 表2(
id int primary key auto_increment,
...
);
create table author(
id int primary key auto_increment,
name varchar(16)
);
create table 关系表(
id int primary key auto_increment,
表1_id int,
表2_id int,
foreign key(表1_id) references 表1(id)
on update cascade
on delete cascade,
foreign key(表2_id) references 表2(id)
on update cascade
on delete cascade
);
create table book_author(
id int primary key auto_increment,
book_id int,
author_id int,
foreign key(book_id) references book(id) # 设置关联
on update cascade
on delete cascade,
foreign key(author_id) references author(id) # 设置关联
on update cascade
on delete cascade
);
'''

一对一

'''
案例:丈夫husband表 | 妻子wife表
​
建表规则:
未存放外键的表被依赖,称之为左表;存放外键的表示依赖表,称之为右表;先操作左边再操作右表
​
建表语法:
create table 左表(
id int primary key auto_increment,
...
);
create table husband(
id int primary key auto_increment,
name varchar(16)
);
create table 右表(
id int primary key auto_increment,
...
左表_id int unique, # 一对一的外键需要唯一性
foreign key(左表_id) references 左表(id)
on update cascade
on delete cascade
);
create table wife(
id int primary key auto_increment,
name varchar(16),
husband_id int unique, # 一对一的外键需要唯一性
foreign key(husband_id) references husband(id)
on update cascade
on delete cascade
);
'''

猜你喜欢

转载自www.cnblogs.com/wenchen/p/10243872.html