mysq基础

 数据库,文件夹

    数据库表,文件

    数据行,文件中的一行数据

2、初始:

show databases;    #查看当前mysql都有哪些数据,根目录都有哪些文件夹

create database 数据库名;创建文件夹

use 数据库名;使用选用的数据库,进入目录

show tables; 查看当前数据库下都有哪些表

create table 表名(nid int,name varchar(20));  #创建数据库表

select * from 表名;  #查看表中的所有数据

insert to 表明(nid,name) values(1,'davide',,'123')

3、授权以及创建用户

Mysql> select Host,User from user;

+-----------+---------------+

| Host      | User          |

+-----------+---------------+

| localhost | MYSQL.session |

| localhost | mysql.sys    |

| localhost | root          |

+-----------+---------------+

3 rows in set (0.00 sec)

用户管理特殊命令:

创建用户

create user  '用户名' @ 'IP地址' identified by  '密码' ;

删除用户

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

修改用户

rename user  '用户名' @ 'IP地址' ; to  '新用户名' @ 'IP地址' ;;

修改密码

set password  for '用户名' @ 'IP地址' = Password( '新密码' )

PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)

创建一个用户

mysql> create user xiaohu@localhost identified by '123';

Query OK, 0 rows affected (0.01 sec)

mysql> select Host,User from user;

+-----------+---------------+

| Host      | User          |

+-----------+---------------+

| localhost | mysql.session |

| localhost | mysql.sys    |

| localhost | root          |

| localhost | xiaohu        |

+-----------+---------------+

4 rows in set (0.00 sec)

删除一个用户

mysql> drop user xiaohu@localhost;

Query OK, 0 rows affected (0.00 sec)

更改名字

mysql> rename user davide@localhost to eric@127.0.0.1;

Query OK, 0 rows affected (0.00 sec)

mysql> select Host,User from user;

+-----------+---------------+

| Host      | User          |

+-----------+---------------+

| 127.0.0.1 | eric          |

| localhost | mysql.session |

| localhost | mysql.sys    |

| localhost | root          |

+-----------+---------------+

4 rows in set (0.00 sec)

给用户设置密码

mysql> set password for root@localhost = Password('6666');

Query OK, 0 rows affected, 1 warning (0.00 sec)

登录失败 默认登录的指定的是localhost

C:\Users\Administrator>mysql -u eric -p

Enter password: ***

ERROR 1045 (28000): Access denied for user 'eric'@'localhost' (using password: YES)

使用-h指定登录的主机

C:\Users\Administrator>mysql -u eric -h 127.0.0.1 -p

Enter password: ***

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

5、权限、

默认什么都没有

show grants for '用户'@'IP地址' -- 查看权限

grant  权限 on 数据库.表 to    '用户' @ 'IP地址' -- 授权

revoke 权限 on 数据库.表 from  '用户' @ 'IP地址' -- 取消权限

select,查

数据库.表

      test.tb1

      test.*  数据库test下的所有表

      *.*      所有的库和所有的表

                 小虎@localhost

权限:

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下才能访问

            用户名@192.168.1.%  用户只能在改IP段下才能访问(通配符%表示任意)

            用户名@%            用户可以再任意IP下访问(默认IP地址为%)

#清空表的内容#清空表的内容#清空表的内容#清空表的内容忘记密码

# 启动免授权服务端

mysqld --skip-grant-tables

# 客户端

mysql -u root -p

# 修改用户名密码

update mysql.user set authentication_string=password('666') where user='root';

flush privileges;

######################总结:

a.解放说收,在重复操作文件,直接将命令发送给mysql服务器,自动操作

b.数据库表

c.创建用户和授权

  密码:必须用

  其他:推荐用

d.客户端连接MySQL提供客户端

  1.mysql -u root -h 192.168.1.1 -P 3306

4、SQL语句

a、数据库级别

show databases;查看当前数据库

create databases 数据库名称;

CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

use 数据库名称;进入数据库

drop  database 数据库名称

b、表级别

show tables;查看当前数据库有哪些表

desc 表名;

########create tables 表名(nid int,name varchar2(20));

#事务,原子性操作,回滚

a、默认值

b、是否可以为空

c、自增列(一张表只能有一个,必须是索引-主键)

d、主键

一张表只能有一个主键,唯一不能重复,不能为空。一般情况下自增列设置成主键

唯一索引:

可以为null,一张表可以有多个唯一列

----约束

----索引,加速查找

create table student(

        name varchar2(20) not null,

        num int not null,

        age int,

        gender int,

        primary key (name,num)

)

create table tb5(

        nid int not null auto_increment primary key,

        name varchar2(20),

        age int defualt 19

        )engine=innodb default charset=utf8;

约束:

                            name num  age

      a  88  9

       a  99  9

#主键:

      不能为null,

      不能重复,

      一张表只有一个主键(可以多列组成主键)

#一般用法:

        nid int auto_increment primary key

        drop table 表名; 删除表

        delete from 表名;#清空表的内容

        truncate table 表名  #清空表内容,性能高速度快,删除自增

select * from 表名;查看表的内容

e:外键:foreign key,一对多

  两张表建立约束,

      -----约束

    -----foreign key,一对多

mysql> alter table userinfo add constraint fk_u_p foreign key userinfo(part_nid) references part(nid);

      数据行级别

select * from tb1;

insert into tb1(name,age) values('joy',19);

insert into tb1(name.age) values('davide',19),('jolin',18)#插入多条数据

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

insert into tb31(name,age) select caption,gender from tb32;

delete from 表

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

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

select * from 表

select * from 表 where id > 1

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

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

5、其他

连表操作:

#连表

select * from a,b where a.x = b.x;

#left join

select * from a left join b on a.x = b.x;

#inner join    #永远不会出现NULL

select * from part inner join userinfo on userinfo.part_nid = part.nid;

a、条件

    select * from 表 where id > 1 and name != 'davide' 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

mysql> select part_nid,count(nid) from userinfo group by part_nid;

+----------+------------+

| part_nid | count(nid) |

+----------+------------+

|        1 |          2 |

|        2 |          3 |

|        4 |          1 |

+----------+------------+

mysql> select part_nid as a,count(nid) as b from userinfo group by part_nid;##起一个别名

+------+---+

| a    | b |

+------+---+

|    1 | 2 |

|    2 | 3 |

|    4 | 1 |

+------+---+

mysql> select part_nid as a,count(nid) as b from userinfo group by part_nid having count(nid)>1;###对聚合函数适应having进行筛选

+------+---+

| a    | b |

+------+---+

|    1 | 2 |

|    2 | 3 |

+------+---+

2 rows in set (0.00 sec)

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 A.num, A.name, B.name

    from A,B

    Where A.nid = B.nid

    无对应关系则不显示

    select A.num, A.name, B.name

    from A inner join B

    on A.nid = B.nid

    A表所有显示,如果B中无对应关系,则值为null

    select A.num, A.name, B.name

    from A left join B

    on A.nid = B.nid

    B表所有显示,如果B中无对应关系,则值为null

    select A.num, A.name, B.name

    from A right join B

    on A.nid = B.nid

g、组合

    组合,自动处理重合

    select nickname

    from A

    union

    select name

    from B

    组合,不处理重合

    select nickname

    from A

    union all

    select name

    from B

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

        bigint[(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)

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。