mysql命令大全

it2022-05-08  9

用户相关

创建用户

# 语法:create user 用户名@'ip地址(%表示无限制)' indentified 密码 create user kingfan@'%' identified by '123'

删除用户

drop user '用户名'@'ip地址';

修改用户

rename user '用户名'@'ip地址' to '新用户名'@'ip地址'

修改密码:

set password for '用户名'@'ip地址' =password('密码'); update mysql.user set password=password('密码') where user='kingfan';

授权相关

授权

# 语法: grant 权限 on 库.表 to 用户名@ip地址; #给kingfan用户对test库有查看和插入权限 grant select,insert, on test to kingfan@'%'; #所有权限 grant all privileges on '*' to kingfan@'%'; #刷新权限,使权限修改立马生效 flush privileges; # 查看权限 show grants for '用户'@'ip地址' revoke 权限 on 数据库.表 from '用户'@'ip地址'

库相关

# 创建库:create database 库名 default charset utf8; create database db1; # 查看库: show databases; # 删除库: drop database 库名; # 进入库: use 库名;

表相关

# 查看表 show tables; # 创建表 create table 表明(字段 数据类型 约束条件) engine=innodb default charset=utf8 # 删除表 drop table 表名; # 清空表的数据 delete from 表名; truncate table 表名; #初始化表的设置,比如自增主键会重新从1开始。效率比delete高

引擎

innodb 支持事物,回滚,原子性操作 myisam

约束条件

# 表里面只能有1个主键和自增列,一般搭配使用 auto_increment #自增默认不为空,从1开始 primary key #索引唯一标识,唯一且不能为空,加速查找 not null #不能为空

数据类型

# 数字: tinyint int bigint smallint float double decimal(底层按字符串存储) decimal(10,5) #10是总长,5是小数位数 # 字符串: char(num) #定长,不足则补充至定长 vachar(num) #不定长,节省空间,但是查询速度没char快 ## char和vachar最大字符255 ## text 存较大文本,文章 # 时间类型 time datatime year timestamp # enum枚举类型(多选1) enum(选项1,选项2,选项2) # set 集合,可多选 create table t1( hobby set(a,b,c,d) ); insert into t1 values('a'/'a,b'/'a,b,c')

表操作

增 insert into t1 values(id,name) 删: delete from t1 where id<6 改 update t1 set age =18 where id=12; 查 select * from t1; # 查看表信息 show create table 表名 desc 表名

### 外键foreinkey

create table department( id int auto_increment primary key, title char(12) ) engine=innodb default charset=utf8; create table userinfo( uid int auto_increment primary key, name varchar(12), department_id int, constraint fk_userinfo_dep foreign key(department) references department(id) ) engine=innodb default charset=utf8 ;

### 修改自增列的起始(auto_increment)

alter table 表名 auto_increment=num; # 设置的num一定满足比当前表中数据最大的数值大 # 自增步长 # mysql:基于会话 # 查看当前会话的步长 show session variables like 'auto_inc%'; # 修改步长,重新登陆会重置 set session auto_increment_increment=2; # 全局步长,修改之后重新登录所有用户都变了 show global variables like 'auto_inc%';

## 约束条件相关 唯一索引unique

create table db1( id int primary key auto_increment, name char(12), phone char(11) unique );

联合唯一索引

create table db1( id int primary key auto_increment, name varchar(12), phone char(11), unique uq1 (name,phone) )

注意:唯一可以为null,主键是唯一且不为null

## 外键的额外使用 一对一关系 foreign key和unique联合使用

# 用户表 create table userinfo( id int primary key auto_increment, username varchar(32) not null unique, password varchar(32) not null unique, ); # 博客表 create table blog( id int primary key auto_increment, title varchar(32) not null, user int unique not null, constraint fk1 foreign key(user) references userinfo(id) );

多不多实例

# 用户表 create table user( id int primary key auto_increment, name varchar(32) ); # 电脑表 create table computer( id primary key auto_increment, name varchar(32) ) # 用户电脑关系表 create table user_cpu( id primary key auto_increment, user int not null, cpu int not null, constraint fk1 foreign key(user) references user(id), constraint fk2 foreign key(cpu) references computer(id), unique u1 (user,cpu) ); # 当多不多关系时需要建立第三张表,建立两个外键关系并且这两个外键联合唯一。

转载于:https://www.cnblogs.com/Kingfan1993/p/10307647.html


最新回复(0)