Mysql数据库操作命令

MySQL 数据库

数据库:关系型 和 非关系型数据库

关系型数据库的优势

  1. 复杂查询

    可以使用SQL语句 在一个或者多个表之间 进行复杂的查询

  2. 事物支持

    可以提高安全性能

非关系型数据库

  1. 性能

    NOSQL 是基于键值对 不需要SQL层的解析 性能高

  2. 可扩展性

    因为基于键值对 所以水平扩展非常的容易

一 进入MySQL数据库

标准写法

mysql -h主机名 -u用户名 -p

输入密码

mysql -hlocalhost -uroot -p

mysql -h127.0.0.1 -uroot -p

本地ip 不走网络 127.0.0.1-127.255.255.254

简写

mysql -uroot -p

二 介绍数据库

MySQL->小数据库->数据表->字段->数据

三 对于数据库的操作

命令:

库和表 createdropaltershow

  1. 查看所有数据库

    show databases;

  2. 进入数据库

    use 库名

  3. 查看当前所在的数据库

    select database();

  4. 创建数据库

    create database 库名;

  5. 查看当前所创建的数据库

    show create database 库名

  6. 创建一个不存在的库

    create database if not exists 库名

  7. 删除数据库

    drop database 库名

    drop database if exists 库名;

  8. 创建库 并设置字符集

    create database python1710 character set utf8

  9. 修改库的字符集

    alter database python1710 character set utf8;

表的操作

  1. 创建表
> create table myint(
  -> age tinyint,   字段名 字段类型
  -> age2 tinyint unsigned 字段名 字段类型 约束条件
  -> );

    2.删除表

drop table if exists user;

    3.查看表

    show create table 表名;

Create Table: CREATE TABLE `myint` (
  `age` tinyint(4) DEFAULT NULL,
  `age2` tinyint(3) unsigned DEFAULT NULL,
  `myint` int(10) unsigned DEFAULT NULL,
  `money1` float(6,2) DEFAULT NULL,
  `money2` decimal(6,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

修改表结构

  1. 修改字符集

    alter table 表名 character set 字符集

  2. 给表添加新字段

    alter table 表名 add 字段名 字段类型 约束条件 after 字段名/first (默认排在最后一位)

  3. 修改表字段类型 约束条件

    alter table 表名 modify 字段名 类型 约束条件 after z字段名/first (默认排在最后一位)

    alter table 表名 modify 字段名 类型 约束条件 character set 字符集 after z字段名/first (默认排在最后一位)

  4. 修改字段名

    alter table 表名 change 旧字段名 新字段名 类型 约束调教 after z字段名/first (默认排在最后一位)

  5. 删除字段名

    alter table 表名 drop 字段名

  6. 修改表名

    alter table 表名 rename 新表名

  7. 查看表结构

    desc 表名

  8. 添加索引(不起索引名)

    alter table 表名 add 索引类型(索引字段)

    alter table a add index(username);

  9. 添加索引(起索引名)

    alter table 表名 add 索引类型 索引名称(索引字段)

    alter table a add unique uname(username);

  10. 查看当前表的所有 索引

show index from 表名

  1. 创建一个表b 和a一样

create table b like a

  1. 删除索引

alter table 表名 drop key 索引名称

注意:

  1. 分号作为命令的结束

  2. 命令不区分大小写

  3. 数据库,表不能重名

  4. 当在命令中 多输入引号以后 所有输入的内容都被认为是引号内的内容 将引号不全 \c撤销当前命令

  5. \c撤销当前命令

  6. 数据库的退出 \q/exit/quit

MySQL开启不严谨报错

修改my.ini配置文件

路径:C:\ProgramData\MySQL\MySQL Server 5.7

sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

MySQL表的创建

一 字段类型

  1. 数值类型

    类型大小范围(有符号)范围(无符号)用途
    tinyint1字节-128,1270,,255最小整数值
    smallint2字节-32768,327670,65535大整数值
    int4字节0,4294967295(10位)大整数值
    float4字节单精度浮点型
    double8字节双精度浮点型
    decimal更加精准的小数类型
创建表的语句

实例

> create table myint(
  -> age tinyint,   字段名 字段类型
  -> age2 tinyint unsigned 字段名 字段类型 约束条件
  -> );
注意
  1. int(3) 或 tinyint(2) 并不会影响数值存储的范围 只会影响显示 在配合zerofill零填充的时候

  2. 如果不需要0填充的时候 后面的值不需要给

  3. 当存储关于钱类型的 对于浮点数要求精确的时候 使用decimal 不会造成精度损耗

2.日期和时间类型

类型大小范围格式用途
date31000-01-01~999912-31YYYY-MM-DD日期值
time3-838:59:59~838:59:59HH:MM:SS时间值
year11901-2155YYYY年份值
datetime81000-01-01 00:00:00~9999-12-31 23:59:59YYYY-MM-DD HH:MM:SS混合日期
注意

在存储时间的时候 最好使用时间戳来存储 这样方便我们对时间的计算

3.字符串类型

类型大小用途
char0-255存储定长字符串
varchar0-255存储变长字符串
text0-65535长文本数据
enum('w','m')65535个成员枚举:可赋予某个枚举成员
set('w','m')64个成员集合:可赋予多个集合成员 用逗号隔开
实例
 
        
mysql> create table if not exists mystring(
    -> username varchar(10),
    -> password char(6),
    -> article text,
    -> sex enum('w','m'),
    -> hobby set('代码','game','beautiful girl')
    -> );


注意

char和varchar 的区别

char的执行相率高于varchar varchar比char更节省空间

char和varchare存储范围都在0-255

enum和set的区别

enum只能选择其中某一个值进行存储

set可以选择 一个到多个值进行存储 如果有重复的值 set会去除重复

二 字段约束

  1. unsigned 无符号 正数

    只能适用于 设置数值类型 不能有负号

    最大存储长度 增加一倍

  2. zerofill 零填充

    只能设置数值类型 当数值长度不足你设定的长度时 会使用0 自动填充到指定的长度

  3. auto_increment 主键自增

    用于设置字段值的自动增长 当每增加一条数据的时候 当前的值会自动加1

  4. default 默认值

    可以给某个字段 设置一个默认值 当不给当前字段添加值的时候 该字段的值为默认值

  5. null 和 not null

    默认为null 当给当前表添加数据的时候 不给某个字段添加值 则当前字段的值为null

    如果设置了 not null 那么在添加数据的时候 就必须给当前字段添加值

  6. comment 设置当前字段的说明

注意:

  1. 和null进行算数运算结果都为Null

  2. null 意味着没有值 或者是 位置值

三 MySQL的索引

主键索引 primary key

唯一索引 unique

常规索引 index

全文索引 fulltext

(1) 主键索引

主键索引是关系型数据库中最常见一种索引类型 用来记录一条数据的位置

注意:
  1. 每个表都最好有一个主键索引 但不是必须指定

  2. 一个表只能指定一个主键 主键的值不能为null

  3. 主键可以有多个约束条件 比如 auto_increment、not null

  4. 每次删除所有的数据 下次在添加数据 会从上次记录的位置继续自增

将主键自增归位

alter table 表名 auto_increment=1;

清空表并将自增归位

truncate 表名

(2) 唯一索引

唯一索引和主键索引相同的地方是 都可以防止创建值的重复 确保数据的唯一性

不同:一个表只能有一个主键索引 但是可以有多个 唯一索引

使用索引的关键字 unique 对应字段去添加

(3) 常规索引

常规索引就是为了提高数据库的性能(查询效率)

缺点:

  1. 多占用磁盘空间

  2. 会减慢增删改的效率

使用index 或者是 key去创建

创建表 添加索引不起名实例

 
        
mysql> create table imgtable(
    -> id int unsigned primary key auto_increment not null,
    -> username varchar(50) not null,
    -> userpass varchar(50) not null,
    -> telno varchar(20) not null,
    -> sex enum('w','m') not null default 'm',
    -> birthday date not null default '0000-00-00',
    -> index(username), #索引类型 (索引字段)
    -> index(userpass),
    -> unique(telno)
    -> );


创建表 添加索引起名实例

 
        
mysql> create table imgtable2(
    -> id int unsigned primary key auto_increment not null,
    -> username varchar(50) not null,
    -> userpass varchar(50) not null,
    -> telno varchar(20) not null,
    -> sex enum('w','m') not null default 'm',
    -> birthday date not null default '0000-00-00',
    -> index iuser(username),#索引类型 索引名称(索引字段)
    -> unique upass(userpass)
    -> );


四 数据表的存储类型

MyISAM 和 InnoDB 俩种表的类型最为重要

  1. MyISAM不支持事物处理 InnoDB支持

  2. MyISAM不支持外检 InnoDB支持

  3. MyISAM的执行效率高于InnoDB

  4. MyISAM默认会产生3个文件 innoDB只有一个

五 表产生文件的区别

.frm文件: 存储数据表的框架结构 名称与表同名 每个表都会对应一个同名的frm文件

1.MyISAM 数据库表文件

.MYD: 即My Data 表数据文件

.MYI: 即 My Index 索引文件

.log: 日志文件

通常用于对数据库要求不高的网站来使用 比如 贴吧.博客

2.InnoDB 数据库表文件

.ibd: 存储数据库的表数据和索引

通常用于对于数据安全性要求高的 比如 电商

六 InnoDB事物处理

  1. 先查看当前的 表的存储引擎是否为 innodb

    alter table 表名 engine=innodb

  2. 查看是否为自动提交

    select @@autocommit #如果为1 则自动提交

  3. 将自动提交改为手动提交

    set autocommit = 0

  4. 开启事物

    begin;

  5. 进行sql语句的操作

  6. 提交或者回滚

    commit work;提交

    rollback work;回滚

注意:
  1. innodb引擎 当开始事物处理以后 不对他进行提交或者回滚 直接退出 那么数据将不会保存

  2. 只有innoDB支持事物处理

七 建表的注意事项

  1. 表的字段 使用逗号作为分隔

  2. 字段最后一句 不需要加逗号

  3. 数据表名 最好不要和字段重名

  4. 字段名称 不能使用mysql的关键字

八 数据的增删改查

增 INSERT INTo

删 DELETE FROM

改 UPDATE

查 SELECT

INSERT 添加

  1. 指定字段添加值

    insert into 表名(字段名1,字段2...) values(值1,值2...)

  2. 不指定字段添加值(有多少个字段 就要添加多少个值 一一对应)

    insert into 表名 values(值1,值2...)

  3. 添加多个值

    • 不指定字段添加多个值

      insert into 表名 values(值1,值2...),(值1,值2...)...

    • 指定字段添加多个值

      insert into 表名(字段名1,字段2...) values(值1,值2...),(值1,值2...)...

SELECT 查询

主体结构

SELECT 字段 FROM 表名 [WHERE条件] [GROUP BY HAVING][ORDER BY]

  1. 不指定字段查询

    select * from 表名

  2. 指定字段查询

    select 字段名1,字段名2... from 表名

  3. 给查询字段起别名

    • select 字段名 别名,字段名 别名 from 表名;

    • select 字段名 as 别名,字段名 as 别名 from 表名;

WHERE 条件

(1) 比较运算符

  1. >

    select * from a where age>72;

  2. <

    select * from a where age<72;

  3. >=

    select * from a where age>=72;

  4. <=

    select * from a where age<=72;

  5. !=/<>

    select * from a where id != 1;

    select * from a where id <> 1;

  6. =

    select * from a where id = 1;

(2) 逻辑运算符

  1. and 逻辑与

    select * from a where username='张三' and age=72;

  2. or 逻辑或

    select * from a where username='张三' or age=72;

  3. between and 在...之间 包含值的本身

    select * from a where id between 3 and 9;

    select * from a where id>=3 and id<=9;

  4. not between and 不在...之间

    select * from a where id not between 3 and 9;

    select * from a where id<3 or id>9;

  5. in 在...里面

    select * from a where username in('张三','李四');

  6. not in ...不在...里面

    select * from a where username not in('张三','李四');

(3) 子查询 sql的条件还是一条SQL语句

select * from a where id in(select id from user where password=123456);

(4) order by 排序

order by 字段名 asc/desc 升序降序

select * from a order by id desc;

select * from a order by id asc;

select * from a where age>72 order by age;

注意:

  1. 不加asc 默认就是升序

  2. order by 要放在所有数据都处理完毕 在将数据排序显示

(5) is not null

因为null是一个特殊的值 不能使用比较运算符操作

select * from a where username is null;

select * from a where username is not null;

(6) limit 取值

limit y 从0取出y条数据

limit y 从0取出y条数据

select * from a order by age desc limit 0,2;

select * from a order by age desc limit 2;

select * from a where age between 112 and 255 and username is not null order by id desc limit 1;

(7) MySQl聚合函数

  1. count(字段名) 统计个数

    select count(*) from a;

  2. max(字段名) 最大值

  3. min(字段名) 最小值

  4. sum(字段名) 求和

  5. avg(字段名) 平均数

select count(*) as con,max(age) as mage,min(age) as minage,sum(age) sumage,avg(age) as avgage from a;

(8) GROUP BY 分组

select classid,count(*) as count from a group by classid

select sex,count(*) from a group by sex;

select sex,count(*) as con from a group by sex order by con desc;

select classid,sex,count(*) from a group by classid,sex;

(9) HAVING 条件

select classid,sex,count(*) as con from a group by classid,sex having con>1; 查询人数大于1人

select classid,sex,count(*) as con from a group by classid,sex having con>1 and sex='w'; 查询人数大于1人 并且性别为w

select classid,sex,count(*) as con from a group by classid,sex having classid in('python1708');

(10) LIKE 模糊查询

'%value%' 值包含的就查询

select * from a where username like '%四%';

‘value%’ 以value值作为开头的数据

select * from a where username like '四%';

‘%value’ 以value值作为开头的数据

select * from a where username like '%四';

select * from a where username like '%张%' and age>72 order by age desc limit 2;

DELETE 删除

主体结构

delete from 表名 【where】

注意:

where 如果不加 则删除所有数据

UPDATE 修改

主体结构

update 表名 set 字段名=值【,字段名=值,...】【where】

注意:

where 如果不加 则修改当前字段的所有数据

DISTINCT 去除重复数据

select distinct age from a;

select age from a group by age;

多表联查

(1) 隐式内连接

select 表名.字段名[,表名.字段名...] from 表1,表2[,表3...] where 关联条件

select u.username,g.goodsname from user as u,goods as g where u.id = g.uid and u.username=15611833906

(2) 显示内连接

select 表名.字段名[,表名.字段名...] from 表1 inner join 表2 on 关联条件

select u.username,g.goodsname from user as u inner join goods as g on u.id = g.uid and u.username=15611833906

(3) 左关联

select * from user as u left join goods as g on u.id = g.uid

注意

左关联 以 左表为主表 右表为辅表 会把主表的所有数据都查询出来 辅表没有匹配的数据 使用null来占位

(4) 右关联

select * from user as u right join goods as g on u.id = g.uid

注意

右关联 以 右表为主表 左表为辅表 会把主表的所有数据都查询出来 辅表没有匹配的数据 使用null来占位

以下作为了解

一 修改密码

set password for 用户名@localhost = password('新密码')

二 创建用户

(1) 进入mysql数据库

​ use mysql

(2) 查询 当前数据库下都有哪些用户

​ select user from user;

(3) 创建用户

​ create user 用户名 identifited by '密码'

(4) 赋予权限

​ grant all on 库名.* to 用户名

(5) 回收权限

​ revoke all on 库名.* from 用户名

(6) 删除用户

​ drop user 用户名

(7) 刷新服务

​ flush privileges;

以上内容来源于同学。






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值