Mysql (一)

一. win安装

	#Windows:
		#可执行文件
			点点点
		#压缩包
			#放置任意目录
			#初始化
				服务端:E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld --initialize-insecure
					    # 用户名 root 密码:空
			#启动服务端:
				E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld\mysqld
				
			#客户端连接:
				E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld\mysql -u root -p 
				
				发送指令:
					show databases;
					create database db1;
				
			#环境变量的配置:
				E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin
				mysqld
				
			#windows服务:
				E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld --install
				net start MySQL
				
				E:\wupeiqi\mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld --remove
				
				net start MySQL
				net stop MySQL

二. 用户管理

a. 用户管理

#创建用户
    create user '用户名'@'IP地址' identified by '密码';
#删除用户
    drop user '用户名'@'IP地址';
#修改用户
    rename user '用户名'@'IP地址'; to '新用户名'@'IP地址';;
#修改密码
    set password for '用户名'@'IP地址' = Password('新密码')

b. 授权管理

show grants for '用户'@'IP地址'                 # -- 查看权限
grant  权限 on 数据库.表 to   '用户'@'IP地址'     # -- 授权
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       由复制从属使用
权限

三. 库操作

a. 创建数据库

# utf-8
CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
create database olddog CHARACTER SET utf8  COLLATE utf8_general_ci;

# gbk 
CREATE DATABASE 数据库名称 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

b. 数据库常用操作

show create database oldboy\G;      #查看创建库的信息

show databases;
show databases like "%old%";
select database();           #查看进入的数据库

c. 常用命令

select version();       #查看版本
select user();          #查看当前的用户
select now();           #查看当前时间
help create database    #查看创建数据库帮助
show character set;     #查看字符集

create database oldboy CHARACTER SET utf8 COLLATE utf8_general_ci;   #创建数据库
grant all on oldboy.* to tom@localhost identified by '123456';       #授权用户
show grants for tom@localhost;                                       #查看用户的权限
select user,host from mysql.user;                                    #查看有哪些用户
use test;
mysql> create table test(
    -> id int(4),
    -> name varchar(16)
    -> )ENGINE=innodb default charset=utf8;
show create table test\G;                                       #查看创建的表
desc test;                                                      #查看表结构
insert into test values(1,'oldboy');                            #插入数据
update 表名 set 字段=“” where 字段......;                       #修改字段数据
delete from 表名 where 条件;                                   #删除字段数据

2、查询(DQL)
    select user,host,password from mysql.user;        #正常查询
    select user,host,password from mysql.user order by user asc;   #升序查询
    select user,host,password from mysql.user order by user desc;  #倒序查询

3、数据操作语言(DML)INSERT UPDATE DELETE
    delete from mysql.user where user="tom";

4、事物处理语言(DPL)BEGIN TRANSACYION,COMMIT,ROLLBACK
5、数据控制语言(DCL)GRANT REVOKE
6、数据定义原因(DDL)CREATE DROP ALTER

三. 表操作 

#创建表
  
create table 表名(
    列名  类型  是否可以为空,
    列名  类型  是否可以为空
)ENGINE=InnoDB DEFAULT CHARSET=utf8
  
#删除表
  
drop table 表名
  
#清空表
  
delete from 表名
truncate 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 表名  modify  列名 int, drop primary key;
    #自增主键修改  alter table db1 AUTO_INCREMENT=10;
    
  #添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
  #删除外键:alter table 表名 drop foreign key 外键名称
    
  #修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
  #删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
  
  
#修改表名
rename table 表名old to 表名new;
alter table 表名old rename to 表名new; 

四. 表内容操作

a. 增

insert into 表 (列名,列名...) values (值,值,值...)
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from 表

b. 删

delete from 表
delete from 表 where id=1 and name='alex'

c. 改

update 表 set name = 'alex' where id>1

d. 查

select * from 表
select * from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1  

五.  anyway

注意:1、对于自增列,必须是索引(含主键)。
         2、对于自增可以设置步长和起始值

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;  #设置全局步长
             set global auto_increment_offset=10;      #设置全局起始值
自增 步长
create table db1(
    cid int not null auto_increment,
    id1 int not null,
    id2 int,
    primary key(cid,id1)
    )engine=innodb default charset=utf8;


create table db2(
    sid int not null auto_increment primary key,
    ic1 int,
    ic2 int,
    constraint db2_db1 foreign key(ic1,ic2) references db1(cid,id1)
    )engine=innodb default charset=utf8;
外键绑定两个主键
create table user_info(
    uid int not null auto_increment primary key,
    name varchar(32) not null,
    usertype int not null
    )engine=innodb default charset=utf8;


create table admain_info(
    id int not null auto_increment primary key,
    user_id int not null,
    unique admin_user (user_id),
    constraint admin_user foreign key(user_id) references user_info(uid)
    )engine=innodb default charset=utf8;



insert into user_info(name,usertype) values("alex",1),("egon",2),("tom",3);

insert into admain_info(user_id) values(1),(2),(3);
外键 唯一 一对一演示
create table user(
    uid int not null auto_increment primary key,
    name varchar(32) not null,
    gender ENUM("","") not null
    )engine=innodb default charset=utf8;


create table host(
    hid int not null auto_increment primary key,
    name varchar(32) not null
    )engine=innodb default charset=utf8;


create table user_host(
    id int not null auto_increment primary key,
    uid int not null,
    hid int not null,
    unique uid_hid (uid,hid),
    constraint user_host_user foreign key(uid) references user(uid),
    constraint user_host_host foreign key(hid) references host(hid)
    )engine=innodb default charset=utf8;


insert into user(name,gender) values("alex",""),("egon",""),("tom","");

insert into host(name) values("host1"),("host2"),("host3");


insert into user_host(uid,hid) values(1,1),(1,2),(1,3);


insert into user_host(uid,hid) values(2,1),(2,2),(2,3);

insert into user_host(uid,hid) values(3,1),(3,2),(3,3);

外键 唯一 一对多演示
外键 唯一 多对多演示
create table user_info(
    uid int not null auto_increment primary key,
    name varchar(32) not null,
    usertype int not null
    )engine=innodb default charset=utf8;


create table admin_info(
    id int not null auto_increment primary key,
    user_id int not null,
    constraint admin_user foreign key(user_id) references user_info(uid)
    )engine=innodb default charset=utf8;



insert into user_info(name,usertype) values("alex",1),("egon",2),("tom",3);

insert into admin_info(user_id) values(1),(2),(3);
外键 一对多演示

六. SQL语句数据 

1、增

insert into 表 (列名,列名...) values (值,值,值...);
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...);
insert into 表 (列名,列名...) select (列名,列名...) from 表;
insert into tb11(name,age) values('alex',12);
insert into tb11(name,age) values('alex',12),('root',18);
insert into tb12(name,age) select name,age from tb11;


2、删

delete from 表;
delete from 表 where id=1 and name='alex';
delete from tb12 where id >=2 or name='alex';


3、改

update 表 set name='alex' where id>1;
update tb12 set name='alex' where id>12 and name='xx'
update tb12 set name='alex',age=19 where id>12 and name='xx'


4、查

select * from tb12;			
select id,name from tb12;
select id,name as cname from tb12 where id > 10 or name ='xxx';
a、条件
    select * from 表 where id > 1 and name != 'alex' and num = 12;

    select * from 表 where id between 5 and 16;

    select * from 表 where id in (11,22,33)
    select * from 表 where id not in (11,22,33)
    select * from 表 where id in (select nid from 表)

b、通配符
    select * from 表 where name like 'ale%'  - ale开头的所有(多个字符串)
    select * from 表 where name like 'ale_'  - ale开头的所有(一个字符)

c、限制
    select * from 表 limit 5;            - 前5行
    select * from 表 limit 4,5;          - 从第4行开始的5行
    select * from 表 limit 5 offset 4    - 从第4行开始的5行

d、排序
    select * from 表 order by 列 asc              - 根据 “列” 从小到大排列
    select * from 表 order by 列 desc             - 根据 “列” 从大到小排列
    select * from 表 order by 列1 desc,列2 asc    - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序

e、分组
    select num from 表 group by num
    select num,nid from 表 group by num,nid
    select num,nid from 表  where nid > 10 group by num,nid order nid desc
    select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid

    select num from 表 group by num having max(id) > 10

    特别的:group by 必须在where之后,order by之前

f、连表
    select * from userinfo5,department5
                    
    select * from userinfo5,department5 where userinfo5.part_id = department5.id
    

    select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
    # userinfo5左边全部显示
    
    
    # select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
    # department5右边全部显示
    
    
    select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
    将出现null时一行隐藏
    
    
    
    select * from department5 
    left join userinfo5 on userinfo5.part_id = department5.id
    left join userinfo6 on userinfo5.part_id = department5.id
                
补充

 

 

 

 

  

  

  

  

  

  

Mysql测试题 

posted @ 2017-06-05 12:12  golangav  阅读(436)  评论(0编辑  收藏  举报