MySQL详细操作

 用户管理 

创建用户
create user "用户名"@"IP地址" identified by "密码";
create user "pd"@"192.168.1.1" identified by "123456";
create user "pd"@"192.168.%" identified by "123456";
create user "pd"@"%" identified by "123456";

删除用户
drop user "用户名"@"IP地址";

修改用户
rename user "用户名"@"IP地址" to "新用户名"@"IP地址";

修改密码
set password for "用户名"@"IP地址" = Password("新密码");

PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)。
    查看所有用户和IP地址
        select user,host from user;

 授权管理 

查看权限
    show grants for "用户名"@"IP地址";

授权
    grant 权限 on 数据库名.表名 to "用户名"@"IP地址";
    grant select,insert,update on db.tb to "pd"@"%";
    grant all privileges on db.tb to "pd"@"%";

取消权限
    revoke 权限 on 数据库名.表名 from "用户"@"IP地址";

关于权限

关于权限
    all privileges          除grant外的所有权限
    select                  仅查权限
    select,insert           查和插入权限
    ...
    usage                   无访问权限
    alter                   使用alter table
    alter routine           使用alter procedure和drop procedure
    create                  使用create table
    create routine          使用create procedure
    create temporary tables 使用create temporary tables
    create user             使用create user、drop user、rename user和revoke  all privileges
    create view             使用create view
    delete                  使用delete
    drop                    使用drop table
    execute                 使用call和存储过程
    file                    使用select into outfile 和 load data infile
    grant option            使用grant 和 revoke
    index                   使用index
    insert                  使用insert
    lock tables             使用lock table
    process                 使用show full processlist
    select                  使用select
    show databases          使用show databases
    show view               使用show view
    update                  使用update
    reload                  使用flush
    shutdown                使用mysqladmin shutdown(关闭MySQL)
    super                   使用change master、kill、logs、purge、master和set global;还允许mysqladmin调试登陆
    replication client      服务器位置的访问
    replication slave       由复制从属使用
关于权限

关于数据库

关于数据库
    数据库名.*               数据库中的所有表
    数据库名.表名        指定数据库中的某张表
    数据库名.存储过程         指定数据库中的存储过程
    *.*                     所有数据库

关于用户和IP

关于用户和IP
    "用户名"@"IP地址"      用户只能在该IP下才能访问
    "用户名"@"192.168.%"     用户只能在该IP段下才能访问(通配符%表示任意)
    "用户名"@"%"             用户可以在任意IP下访问(默认IP地址为%)

 数据库操作 

显示数据库
show databases;

创建数据库
create database 数据库名 default charset utf8;
create database 数据库名 default charset gbk;

删除数据库
drop database 数据库名;

使用数据库
use 数据库名;

显示当前使用的数据库中所有表
show tables;

 数据表操作 

创建表
    create table 表名(
        字段名 类型 是否为空,
        字段名 类型 是否为空
        )engine=innodb default charset=utf8;
    ----------------------------------------------------------------------------    
    是否为空
            null        可空,非字符串
            not null    不可空
    ----------------------------------------------------------------------------
    默认值
        创建字段时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
            create table test(id int not null default 2)engine=innodb default charset=utf8;
    ----------------------------------------------------------------------------
    自增
        如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)
            create table test(
                id int not null auto_increment primary key,
                name char(10) not null
            )engine=innodb default charset=utf8;
            或者
            create table test1(
                id int not null auto_increment,
                name char(10) not null,
                index(id)
            )engine=innodb default charset=utf8;
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        修改自增从多少开始
            alter table 表名 auto_increment=20;
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        对于MYSQL的自增步长
            基于会话级别:修改仅对此次登录有效(一次登录就表示一次会话)
                show session variables like "auto_inc%";    查看全局变量
                set session auto_increment_increment=2;     设置步长
                set session auto_increment_offset=10;       设置起始值(一般不用)
            基于全局级别:永久有效
                show global variables like "auto_inc%";        查看全局变量
                set global auto_increment_increment=2;        设置步长
        对于SQL server的自增步长
            基于表级别(未接触,不懂)
    ----------------------------------------------------------------------------
    唯一索引
        create table 表名(
            id int not null auto_increment primary key,
            ss int,
            xx int,
            unique 唯一索引名称 (ss,xx)
        )engine=innodb default charset=utf8;
        ----------------------------------------
        唯一索引作用:
            索引列的值必须唯一,允许有空值
            与主键索引区别:一种特殊的唯一索引,不允许有空值
    ----------------------------------------------------------------------------
    主键
        一种特殊的唯一索引,不允许有空值,一个表只能有一个主键。如果主键使用单个列,则它的值必须唯一;如果是多列,则其组合必须唯一。
        create table 表名(
            id int not null auto_increment primary key
        )engine=innodb default charset=utf8;
        或者
        create table 表名(
            id int not null,
            num int not null,
            primary key(id,num)
        )engine=innodb default charset=utf8;
    ----------------------------------------------------------------------------    
    外键
        create table department(
            id int not null auto_increment primary key,
            name varchar(32)
        )engine=innodb default charset=utf8;
        
        create table userinfo(
            id int not null auto_increment primary key,
            name varchar(32),
            department_id int,
            constraint fk_usinfo_depam foreign key (department_id) references department(id)
        )engine=innodb default charset=utf8;
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        create table tb11(
              id1 int not null auto_increment,
              id2 int not null,
              name varchar(32),
              primary key(id1,id2)
        )engine=innodb default charset=utf8;
        
        create table tb22(
              id int not null auto_increment primary key,
              name varchar(32),
              ida int,
              idb int,
              constraint fk_tb22_tb11 foreign key (ida,idb) references tb11(id1,id2)
        )engine=innodb default charset=utf8;
查看表结构
    desc 表名;

查看表示怎么创建的
    show create table 表名;
    show create table 表名 \G;

删除表
    drop table 表名;
修改表
    
    添加字段
        alter table 表名 add 字段名 类型;
    
    删除字段
        alter table 表名 drop column 字段名;
        
    修改字段
        修改字段类型
            alter table 表名 modify column 字段名 类型;
        修改字段名
            alter table 表名 change 原字段名 新字段名 类型;
            
    添加主键
        alter table 表名 add primary key (字段名);
    
    删除主键
        alter table 表名 drop primary key;
        
    添加外键
        alter table 从表名 add constraint 外键名 foreign key (从表字段名,可多个) references 主表(主表字段名,可多个);
        
    删除外键
        alter table 表名 drop foreign key 外键名;
        
    修改字段默认值
        alter table 表名 alter 字段名 set default 数值;
        
    删除字段默认值
        alter table 表名 alter 字段名 drop default;

 建立表与表之间的关系 

#一对一
两张表:微信号与电话号,一个微信号只能对应一个电话号
关联方式:foreign key + unique
create table phone_tb(
    id int auto_increment primary key,
    phone_num bigint(50) not null unique
)engine=innodb default charset=utf8;

create table wechat_tb(
    id int auto_increment primary key,
    wechat_name varchar(32) not null,
    phone_id int unique,  #该字段一定要是唯一的
    constraint fk_wechat_phone foreign key(phone_id) references phone_tb(id)
)engine=innodb default charset=utf8;

insert into phone_tb(phone_num) values
    ("18820051111"),
    ("18820052222"),
    ("18820053333"),
    ("18820054444");

insert into wechat_tb(wechat_name,phone_id) values
    ("熊大","1"),
    ("熊二","3"),
    ("光头强","2"),
    ("凹凸曼","4");
一对一

#一对多或称为多对一
两张表:班级与学生,一个班级可以对应多个学生,但一个学生只能对应一个班级
关联方式:foreign key
create table class(
    id int auto_increment primary key,
    name char(32) not null
)engine=innodb default charset=utf8;

create table student(
    id int auto_increment primary key,
    name char(32) not null,
    gender char(32) not null,
    classid int not null,
    constraint fk_stu_cla foreign key(classid) references class(id)
)engine=innodb default charset=utf8;

insert into class(name) values
    ("高三1班"),
    ("高三2班"),
    ("高三3班");
    
insert into student(name,gender,classid) values
    ("A","male","1"),
    ("B","female","1"),
    ("C","male","2"),
    ("D","female","2"),
    ("E","male","3"),
    ("F","female","3");
一对多

#多对多
两张表:学生和课程,一个学生可以选择多门课程,一门课程也可以被多个学生选择
关联方式:foreign key
create table course(
    id int auto_increment primary key,
    subject char(32) not null
)engine=innodb default charset=utf8;

create table student(
    id int auto_increment primary key,
    name char(32) not null,
    gender char(32) not null,
    courseid int not null,
    unique(id,courseid),
    constraint fk_stu_cou foreign key(courseid) references course(id)
)engine=innodb default charset=utf8;

insert into course(subject) values
    ("语文"),
    ("数学"),
    ("英语"),
    ("物理"),
    ("化学"),
    ("生物");
    
insert into student(name,gender,courseid) values
    ("A","male","4"),
    ("A","male","6"),
    ("B","female","5"),
    ("B","female","1"),
    ("C","male","3"),
    ("C","male","2");
多对多

 基本数据类型 

MySQL的数据类型大致分为:数值、时间和字符串

bit[(m)]
    二进制位(101001),m表示二进制位的长度(1-64),默认m=1
    
tinyint[(m)][unsigned][zerofill]
    小整数,数据类型用于保存一些范围的整数数值范围
        有符号:-128 ~ 127
        无符号:0 ~ 255
        特别的:MySQL中无布尔值,使用tinyint(1)构造

int[(m)][unsigned][zerofill]
    整数,数据类型用于保存一些范围的整数数值范围
        有符号:-2147483648 ~ 2147483647
        无符号:0 ~ 4294967295
        特别的:整数类型中的m仅用于显示,对存储范围无限制。例如:int(5),当插入数据2时,select时数据显示为:00002
        
big[(m)][unsigned][zerofill]
    大整数,数据类型用于保存一些范围的整数数值范围
        有符号:-9223372036854775808 ~ 9223372036854775807
        无符号:0 ~ 18446744073709551615

decimal[(m[,d])][unsigned][zerofill]
    准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。
        特别的:对于精确数值计算时需要用此类型
            decaimal能够存储精确值的原因在于其内部按照字符串存储
            
float[(m,d)][unsigned][zerofill]
    单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数
        无符号:
            -3.402823466E+38 to -1.175494351E-38,
            0
            1.175494351E-38 to 3.402823466E+38
        有符号:
            0
            1.175494351E-38 to 3.402823466E+38
        特别的:###数值越大,越不准确###

double[(m,d)][unsigned][zerofill]
    双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数
        无符号:
            -1.7976931348623157E+308 to -2.2250738585072014E-308
            0
            2.2250738585072014E-308 to 1.7976931348623157E+308
        有符号:
            0
            2.2250738585072014E-308 to 1.7976931348623157E+308
        特别的:###数值越大,越不准确###
        
char(m)
    char数据类型用于表示固定长度的字符串,可以包含最多达255个字符。其中m代表字符串的长度
        PS: 即使数据小于m长度,也会占用m长度
    
varchar(m)
    varchars数据类型用于变长的字符串,可以包含最多达255个字符。其中m代表该数据类型所允
    许保存的字符串的最大长度,只要长度小于该最大值的字符串都可以被保存在该数据类型中
        PS:虽然varchar使用起来较为灵活,但是从整个系统的性能角度来说,char数据类型的处理速度更快,有时甚至可
           以超出varchar处理速度的50%。因此,用户在设计数据库时应当综合考虑各方面的因素,以求达到最佳的平衡

text
    text数据类型用于保存变长的大字符串,可以组多到65535(2**16 − 1)个字符
    
mediumtext
    一个text列,最大长度为16777215(2**24 - 1)个字符
    
longtext
    一个text列,最大长度为4294967295(2**32 - 1)个字符

enum
    枚举类型:enum列最多可包含65535个不同的元素
    示例:
        create table shirts(
            name char(32),
            size enum("x-small","small","medium","large","x-large")
        )engine=innodb default charset=utf8;
        
        insert shirts(name, size) values("dress shirt","large"),("t-shirt","medium"),("polo shirt","small");
        
set
    集合类型:set列最多可包含64个不同的成员
    示例:
        create table myset(col set("a","b","c","d"));
        
        insert into myset(col) values("a,d"),("d,a"),("a,d,a"),("a,d,d"),("d,a,d");
        
data
    YYYY-MM-DD,例如:2018-08-10
    
time
    HH:MM:SS,例如:11:11:11

datetime
    YYYY-MM-DD HH:MM:SS,例如:2018-08-10 11:11:11
    
year
    YYYY,例如:2018
    
timestamp
    YYYY-MM-DD HH:MM:SS,例如:2018-08-10 11:11:11
基本数据类型

 表内容操作 

增
    insert into 表名(字段名,字段名...) values(值,值...);
    insert into 表名(字段名,字段名...) values(值,值...),(值,值...);
    insert into 表名(字段名,字段名...) select 字段名,字段名... from 表名;
    
删
    清空表
        delete from 表名;         当再次插入数据的时候,自增将基于原纪录
        truncate teble 表名;      当再次插入数据的时候,从1开始自增
    
    条件删除
        delete from 表名 where 条件;
        delete from 表名 where id!=2;
        delete from 表名 where id!=2 and name="pd";
        delete from 表名 where id>=2 or name="pd";

改
    update 表名 set 字段名="" where 条件;
    update 表名 set 字段名="",字段名="" where 条件;
    update test set name="pd" where id=1;
    
查
    select * from 表名;
    select 字段名,字段名... from 表名;
    select 字段名 as 自定义名 from 表名;  #显示出来的相应字段为自定义名

 其他操作

条件
    select * from 表名 where 条件;
    select * from 表名 where id between 5 and 10;  #取id是5-10的值
    select * from 表名 where id in (1,2,3);
    select * from 表名 where id not in (1,2,3);
    select * from 表名 where id in (select id from 表名);
    
通配符
    select * from 表名 where 字段名 like "app%";  #app开头的所有值(多个字符串)
    select * from 表名 where 字段名 like "app_";  #app开头的所有值(一个字符)

限制
    select * from 表名 limit 5;  #取前5行
    select * from 表名 limit 2,5;  #从3开始取,往后取5行;即3-7行的数据
    select * from 表名 limit 5 offset 2;  #前2行不取,往后取5行;即3-7行的数据
    
    结合python分页
        page = input("请输入要查看的页码:")
        page = int(page)
        (page-1)*10
        select * from 表名 limit 0,10;1
        select * from 表名 limit 10,10;2
        
排序
    select * from 表名 order by 字段名 asc;   #根据"字段名"从小到大排序
    select * from 表名 order by 字段名 desc;  #根据"字段名"从大到小排序
    
    select * from 表名 order by id desc limit 10;  #排序后再取10行数据
    
分组
    select 字段名 from 表名 group by 字段名;
    select 字段名,字段名 from 表名 group by 字段名,字段名;
    select id,name from test group by name where id>5 group by id,name order by id desc;
    
    select count(name),max(id) from test group by name;
        聚合函数:count、max、min、sum、avg;灵活使用即可
    
    select count(name),max(id) from test group by name having max(id)>5;
        如果要对聚合函数结果进行二次筛选,则必须使用having

连表
    无对应关系则不显示
    select * from student,class where student.classid=class.id; 
    
    无对应关系则不显示
    select * from student inner join class on student.classid=class.id;
    
    student表所有显示,如果class表中无对应关系,则为null
    select * from student left join class on student.classid=class.id;
    
    class表所有显示,如果student表中无对应关系,则为null
    select * from student right join class on student.classid=class.id;
    
    小结:inner join将出现null的一行不显示,left join左边全部显示,right join右边全部显示
    
    连多张表
        select * from score 
            left join student on score.studentid=student.id 
            left join course on score.courseid=course.id 
            left join class on student.classid=class.id 
            left join teacher on course.teacherid=teacher.id;
        
    组合
        自动处理重合
            select 字段名 from 表A union select 字段名 from 表B;
            select name from student union select name from teacher;
            
        不处理重合
            select 字段名 from 表A union all select 字段名 from 表B;
others

 连表操作(连多张表)

这5张表的关系为:student.classid对应class.id;course.teacherid对应teacher.id;socre.studentid和socre.courseid分别对应student.id和course.id

那么怎么同时查看这五张表的相互关系呢?

select * from score 
    left join student on score.studentid=student.id 
    left join course on score.courseid=course.id 
    left join class on student.classid=class.id 
    left join teacher on course.teacherid=teacher.id;

猜你喜欢

转载自www.cnblogs.com/believepd/p/9753224.html