day41 mysql详细操作

复习

create table 表名(
    id int primary key auto_increment,
    字段名 数据类型[(宽度) 约束]
)engine=innodb charset=utf8;

类型:
整型|浮点型|字符型|时间|枚举|集合

整型不设置宽度
1byte -2^7~2^7-1 | 4bytes -2^31~2^31-1 字符型 char定长,存取效率高,一般占空间,但数据长度相近,推荐使用(规定宽度与数据长度一致,比varchar更省空间) varcahr 不定长, 存取效率低, 一般省空间, 数据头长度1~2字节来计算存储数据的长度 时间 枚举:多选一 集合:多选多 # 约束 # primary key:主键(唯一不为空, innodb引擎以主键来构建表, 表有且只有一个主键, 没设找第一个唯一不为空, 没找到自动创建一个隐藏主键) => 必须为表指定主键id int primary key auto_increment # foreign key:外键(其他表中的唯一字段) # unique: 唯一 # auto_increment: 自增, 跟某key后 # not null: 不为空 # default: 默认值 # unsigned: 无符号 # zerofill: 0填充

数据库要掌握的

1.启动服务
2.如何管理用户
3.sql语句(记录的增删改查)
4.其他client连接数据库

表的使用(全) => 表中字段的管理

表的详细操作
'''
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 旧字段名 新字段名 类型[(宽度) 约束];
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("密码")); # 错误 5.7版本的mysql已经没有Password字段,改成了authentication_string 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 auto_increment, ... ); create table authur( 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_authur( id int primary key auto_increment, book_id int, authur_id int, foreign key (book_id) references book(id) on update cascade on delete cascade, foreign key (authur_id) references authur(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

);

alter table wife add sex enum('female','male','wasai') default 'wasai' after name;

猜你喜欢

转载自www.cnblogs.com/shanau2/p/10246359.html