码迷,mamicode.com
首页 > 数据库 > 详细

你不可不知的数据库操作

时间:2016-10-27 00:53:46      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:icm   sdh   ext   where   img   百分号   css   iar   gif   

一、数据库操作

1、查看数据库

 

SHOW DATABASES;

 

# 默认数据库:

  mysql - 用户权限相关数据

  test - 用于用户测试数据

  information_schema - MySQL本身架构相关数据

 

2、创建数据库

 

# utf-8 编码

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

 

# gbk 编码

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

 

3、使用数据库

 

USE db_name;

 

# 可以不使用分号

 

4、用户管理

 

# 创建用户

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

# 删除用户

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

# 修改用户

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

# 修改密码

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

 

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

 

# 查看当前用户

select user();

# 查看所有用户

select host,user from mysql.user;

# 人性化显示所有用户

SELECT DISTINCT CONCAT(‘User: ‘‘‘,user,‘‘‘@‘‘‘,host,‘‘‘;‘) AS query FROM mysql.user;

# 查看用户的所有权限

show grants for ‘nick‘@‘%‘;

 

  • View Code

 

技术分享

技术分享

 

5、授权管理

 

# 查看权限

    show grants for ‘用户‘@‘IP地址‘

# 授权

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

# 取消权限

    revoke 权限 on 数据库.表 from ‘用户‘@‘IP地址‘

 

常用权限:

 

all privileges   除grant外的所有权限

select              仅查权限

select,insert    查和插入权限

usage              无访问权限

 

对于目标数据库以及内部其他:

 

技术分享

 

对于用户和IP:

 

技术分享

 

  • 更多权限

 

技术分享

技术分享

 

  • 添加额外管理员

 

技术分享

 

  • 简单示例

 

技术分享

 

  • 创建用户一般流程

 

技术分享

 

6、授权局域网内主机远程连接数据库

 

#百分号匹配法

    grant all on *.* to ‘test‘@‘192.168.200.%‘ identified by ‘test123‘;

#子网掩码配置法

    grant all on *.* to ‘test‘@‘192.168.200.0/255.255.255.0‘ identified by ‘test123‘;

#刷新权限

    flush privileges;

#远程登陆连接

    mysql -utest -ptest123 -h 192.168.200.96

二、表操作

1、创建表

 

# 基本语法:

create table 表名(

    列名  类型  是否可以为空  默认值  自增  主键,

    列名  类型  是否可以为空

)ENGINE=InnoDB DEFAULT CHARSET=utf8

 

not null                # 不可以为空

default 1              # 默认值为1

auto_increment   # 自增

primary key         # 主键

constraint 外键名 foreign key (从表字段’自己‘) references 主表(主键字段)    # 外键

 

是否可空,null表示空,非字符串

not null    - 不可空

null          - 可空

 

默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值

            create table tb1(

                nid int not null defalut 2,

                num int not null

            )

 

自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)

            create table tb1(

                nid int not null auto_increment primary key,

                num int null

            )

            或

            create table tb1(

                nid int not null auto_increment,

                num int null,

                index(nid)

            )

 

注意:1、对于自增列,必须是索引(含主键)。

          2、对于自增可以设置步长和起始值

          技术分享

 

主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。

          技术分享

 

外键,一个特殊的索引,只能是指定内容

         技术分享

 

2、删除表

drop table 表名

 

3、清空表

 

# 表还存在,表内容清空

delete from 表名

truncate table 表名

 

4、修改表

 

# 添加列:

        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 从表 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 原表名 to 新表名;

 

  • 增删改表的字段

 

#增加表字段,altertable法。

1>    语法: altertable 表名 add 字段 类型 其他;

2>    插入列,名为sex。

技术分享

3>    插入名为suo列在name后面。

技术分享
4>    插入名为qq列在第一。

技术分享

 

#更改表名字,rename法。

1>    语法: rename table 原表名 to 新表名;

2>    更改oldsuo表为oldning。

技术分享

#删除表

1>    语法:drop table <表名>;

2>    删除表名为oldsuo表。

技术分享

 三、表内容操作

1、增

 

语法:insert into 表 (列名,列名...) values (值,值,值...)

 

# 插入单条数据

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

# 插入多条数据

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

# 插入另一条语句的查询结果

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

 

2、删

 

语法:delete from 表

 

delete from 表;

delete from 表 where id=1;

 

3、改

 

语法:update 表 set name = ‘nick‘ where id>1

 

update 表 set name = ‘nick‘ where id>1

 

4、查

 

语法:select * from 表

 

select * from 表

select * from 表 where id > 1

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

# as 做别名

 

5、条件

 

语法:select * from 表 where id > 1

 

技术分享

 

6、通配符

 

语法:select * from 表 where name like ‘_n%‘

 

技术分享

 

7、限制

 

语法:select * from 表 limit 9,5;

 

技术分享

 

8、排序

 

语法:select * from 表 order by 列1 desc,列2 asc

 

技术分享

 

9、分组

 

语法:select num from 表 group by num

 

技术分享

 注:group by 必须在where之后,order by之前

 

技术分享

 

10、连表

 

语法:inner join . on、left join . on、right join . on

 

技术分享

 

11、组合

 

语法:union、union all

 

技术分享

 

  • 查询表数据

 

1>    命令语法:select<字段1,字段2,…>from<表名>where<表达式>

2>    查询所有

技术分享
3>    查询某列。不用*,查询的列列出来。

技术分享
4>    指定条件查询

技术分享

    #升序

技术分享

    #倒叙

技术分享

 

  • 表中插入数据

 

1>    插入单个数据,student为表的名称。

技术分享

2>    批量插入数据,student为表的名称。

技术分享

 

  • 表中删除数据

 

1>    删除所有数据,student为表的名称。

技术分享

2>    删除表中的某行或某些

技术分享

3>    直接清空某张表

技术分享

四、其它命令

1、查看建表语句

 

技术分享

 

2、查看表结构

 

desc 表名;

 

技术分享

 

 

3、查看是否走索引

 

explain select * from 表名 where name =‘nick‘ \G 

 

用此命令查看是否sql语句是否还有优化的余地

 

技术分享

 五、数据类型

学习新的东西自然离不开数据类型,MySQL中的数据类型还算简单;大致分为数字、字符串、时间。

 

那就详细看看吧:

 

技术分享

技术分享

技术分享

技术分享

技术分享

 六、索引

1、索引概述

 

索引是表的索引目录,在查找内容之前先查目录中查找索引位置,从而快速定位查询数据;

 

可以理解成新华字典中的索引;

 

索引会保存在额外的文件中。

 

2、索引种类

 

一般的索引种类及功能:

 

  • 普通索引:仅加速查询

  • 唯一索引:加速查询 + 列值唯一(可以有null)

  • 主键索引:加速查询 + 列值唯一 + 表中只有一个(不可以有null)

  • 组合索引:多列值组成一个索引,专门用于组合搜索,其效率大于索引合并

  • 全文索引:对文本的内容进行分词,进行搜索 

  • 索引合并:使用多个单列索引组合查询搜索

  • 覆盖索引:select的数据列只用从索引中就能够取得,不必读取数据行,换句话说查询列要被所建的索引覆盖

 

a、普通索引

 

# 创建表 + 索引

技术分享

# 创建索引

create index index_name on table_name(column_name)

 

# 删除索引

drop index_name on table_name;

 

# 查看索引

show index from table_name;

 

#注意:对于创建索引时如果是BLOB 和 TEXT 类型,必须指定length。

create index ix_extra on in1(extra(32));

 

b、唯一索引

 

# 创建表 + 唯一索引

技术分享

# 创建唯一索引

create unique index 索引名 on 表名(列名)

 

# 删除唯一索引

drop unique index 索引名 on 表名

 

c、主键索引

 

# 创建表 + 创建主键

技术分享

# 创建主键

alter table 表名 add primary key(列名);

 

# 删除主键

alter table 表名 drop primary key;

alter table 表名  modify  列名 int, drop primary key;

 

d、组合索引

 

组合索引是多个列组合成一个索引来查询

 

应用场景:频繁的同时使用多列来进行查询,如:where name = ‘nick‘ and age = 18。

 

# 创建表

技术分享

# 创建组合索引

create index ix_name_age on mess(name,age);

 

如上创建组合索引之后,查询一定要注意:

 

  • name and email  -- >使用索引,name一定要放前面

  • name         -- >使用索引

  • email         -- >不使用索引

 

注意:同时搜索多个条件时,组合索引的性能效率好过于多个单一索引合并。

 

3、相关命令

 

# 查看索引

    show index from  表名

 

# 查看执行时间

    set profiling = 1;  # 开启profiling

    SQL...              # 执行SQL语句

    show profiles;      # 查看结果

 

4、如何正确使用索引

 

# like ‘%xx‘,避免%_写在开头

    select * from tb1 where name like ‘%n‘;

 

# 使用函数

    select * from tb1 where reverse(name) = ‘nick‘;

 

# or

    select * from tb1 where nid = 1 or email = ‘630571017@qq.com‘;

    注:当or条件中有未建立索引的列才失效,否则会走索引

 

# 类型不一致

    如果列是字符串类型,传入条件是必须用引号引起来。

    select * from tb1 where name = 999;

 

# !=,不等于

    select * from tb1 where name != ‘nick‘

    注:如果是主键,则还是会走索引

        select * from tb1 where nid != 123

 

# >,大于

    select * from tb1 where name > ‘nick‘

    注:如果是主键或索引是整数类型,则还是会走索引

        select * from tb1 where nid > 123

        select * from tb1 where num > 123

 

# order by

    select email from tb1 order by name desc;

    当根据索引排序时候,选择的映射如果不是索引,则不走索引

    注:如果对主键排序,则还是走索引:

        select * from tb1 order by nid desc;

 

# 组合索引最左前缀

    如果组合索引为:(name,email),查询使用:

    name and email       -- 使用索引

    name                 -- 使用索引

    email                -- 不使用索引

 

5、注意事项

 

# 避免使用select *

# count(1)或count(列) 代替 count(*)

# 创建表时尽量时 char 代替 varchar

# 表的字段顺序固定长度的字段优先

# 组合索引代替多个单列索引(经常使用多个条件查询时)

# 尽量使用短索引

# 使用连接(JOIN)来代替子查询(Sub-Queries)

# 连表时注意条件类型需一致

# 索引散列值(重复少)不适合建索引,例:性别不适合

 

6、执行计划

 

explain + 查询SQL  用于显示SQL执行信息参数,根据参考信息可以进行SQL优化

 

技术分享

 

id

查询顺序标识

 

如:mysql> explain select * from (select nid,name from tb1 where nid < 10) as B;

           技术分享特别的:如果使用union连接气值可能为null

 

 

技术分享

技术分享

技术分享

技术分享

 

你不可不知的数据库操作

标签:icm   sdh   ext   where   img   百分号   css   iar   gif   

原文地址:http://www.cnblogs.com/yingjie2222/p/6002196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!