MySQL学习笔记

来源:互联网 发布:2017电视直播软件港台 编辑:程序博客网 时间:2024/05/21 12:46

Mysql 学习笔记


  • Mysql 学习笔记
    • 表操作
    • 查询操作
    • 索引
    • 外键
    • Mysql 权限操作
    • 其他操作

表操作

  • 显示当前数据库下所有表:show tables
  • 显示表的描述信息:desc 表名
  • 显示创建表的SQL语句:show create table 表名
  • 修改表名:alter table 表名 rename to 新表名
  • 创建外键:alter table 表名 add foreign key () references ()
    注意:外键字段和关联字段要具有相同的类型和长度,都必须建立索引;
    表必须是InnoDB引擎,MyISAM引擎不支持外键也不支持
  • 复制表数据:insert into 表1 select * from 表2
  • 复制表结构:create table 表1 like 表2
  • 从另外一个数据库表复制数据:
    1 create table 表1 like 数据库名.表名2
    2 insert into 表1 select * from 数据库名.表2
  • 将表数据以文本形式导出:select * from 表名 into out outfile 文件名 fields terminated by ‘,’ lines terminated by ‘\r\n’
  • 插入数据:insert into 表名(字段,字段) values(值,值)
  • 更新数据:update 表名 set 字段名=值 where 条件
  • 删除数据:delete from 表名 [where] 如果不用where,将删除所有的记录

查询操作

  • 查询结果排序:select * from 表名 order by 字段名 desc 或者 asc
  • 查询结果分组:select * from 表名 group by 字段名 (可用count,sum,avg等函数)
  • 查询条件:where 字段名 like

    '%abc' 以abc结尾
    'abc%' 以abc开头
    '_bc' “-“是通配符
    '[abc]%' 以a或b或c开头
    '[!abc]' 不以a或b或c开头

  • 查询条件:where 字段名 in (‘abc’,’edf’)

  • 查询条件:where 字段名 between ‘首字母’ and母’ 顺序是 a到z
  • 内连接:select a.字段,b.字段 from 表名 a join 表名 b on a.字段=b.字段 where条件
  • 左连接:select a.字段,b.字段 from 表名 a letf join 表名 b on a.字段=b.字段 where条件
  • 右连接:select a.字段,b.字段 from 表名 a right join 表名 b on a.字段=b.字段 where条件
  • 显示结果时改变字段:select 字段名 as 名字
  • 合并两个或多个select结果集,两个表要具有相同的列并且具有类似的数据类型,结果显示的字段是表1的字段:select * from 表1 union(不显示重复的)或union all(显示所有) select * from 表2

索引

  • 创建普通索引:alter table 表名 add index (字段名)
  • 创建唯一索引:alter table 表名 add unique (字段名)
  • 添加主键:alter table 表名 add primary key (字段名)
  • 删除索引:drop index 索引名 on 表名
  • 设计索引的原则:1最适合创建索引的列是出现在where字句中的列或连接字句on语句后面的列2使用短索引:对于varchar和char列,只用它的一部分来创建索引可以节省空间,查询更快,例如:create index 索引名 on 表名 (字段(10)) 表示为前10个字符创建索引
  • on delete 四个参数:代表删除主记录时所做的约定
    restrict(限制):如果删除主表对应从表有相应的记录,则主表无法删除
    cascade(级联):主表删除,从表记录也删除
    set null:主表删除,从表外键设置为空
    no action:从表什么也不做

外键

  • Mysql自连接:
    create table category
    (
    id int not null auto-increment,
    name varchar(255),
    foreign key (parent_id) references category (id),
    on delete no action
    )
    注意:on delete 必须为no action,如果为restrict,则无法删除记录

Mysql 权限操作

  • Mysql创建用户:create user 用户名 identified by 密码
  • 给用户赋予权限1:gran

    all 代表所有权限
    sdfas
    select 查权限
    insert 改权限

  • 给用户赋予权限2on

    *.* 代表所有数据库的所有表
    数据库名.* 代表指定数据库的所有表

  • 给用户赋予权限3to

    用户名@localhost 代表只能本地连接
    用户名@% 代表任意IP地址连接

  • 给用户赋予权限4

    identified by 密码

  • 取消权限:revoke 用法和 grant 一样

其他操作


  • 连接到SQL数据库:mysql -u 用户名 -p
  • 创建数据库:create 数据库名
  • 显示所有数据库:show databases
  • 切换到数据库:use 数据库名
  • 显示当前数据库:select database()
  • MySQL视图:视图是可视化的表,创建视图相当于创建一张虚拟的表,不会对数据库中的表及结构带来任何影响,其实就是查询结果的可视化表,方便用来做常用的查询的。
  • 创建视图:create view 视图名 as select * from 表名
  • 删除视图:drop view 视图名
  • 用where时注意,当出现where sum(字段名)>100 不可用,因为sum(字段名)是不存在的,此时where 换成having 有用
  • Mysql处理重复数据3种方法:

1 create table temp select * from 表名 group by 字段名
drop table 表名
alter table temp rename to table
2 alter ignore table 表名 add unique (字段名)
3 alter ignore table 表名 add primary key (字段名)

0 0
原创粉丝点击