zoukankan      html  css  js  c++  java
  • MySQL基本知识学习笔记

    一、下载安装

    如下地址查看https://www.cnblogs.com/LiuChengGang/p/13217839.html

     

    二、DDL、DML、DCL、TCL概述

    DDL(data definition language)数据定义语言:定义或改变表结构、数据类型、约束、表之间链接

    DML(data manipulation language)数据操作语言:操作数据表中的数据,如增删改查

    DCL(data control language)数据控制语言:设置或更改数据库用户和角色

    TCL(transaction control language)事务控制语言:控制事务

     

    三、DDL部分

    1、语法

    创建数据库:create database (if not exits) 数据库名;

    删除数据库:drop database (if exists) 数据库名;

    修改数据库:alter database 数据库名 character set utf8;    注:MySQL中“utf-8”都不需要“-”

    查看数据库:show databases ;

    使用或更换数据库:use 数据库名;

     

    创建表:create table 表名 (字段1 字段类型 约束,字段2 字段类型 约束...);

    删除表:drop table 表名;

    修改表:alter table 表旧名 rename to 表新名;

        alter table 表名 (add 字段 类型..);

        alter table 表名 drop 字段;

        alter table 表名 modify 字段 新类型;

        alter table 表名 change 列旧名 列新名 类型; 

    查询表:show tables;      desc/describe 表名;

    2、数据类型

    想细看,可查看该网址:https://www.cnblogs.com/-xlp/p/8617760.html

    数值型:int 、float、double

    字符串:char、varchar、text

    日期型:date、datatime、timestamp(时间戳)

    3、约束

    主键约束 primary key 非空且唯一    联合主键 primary key(字段1,字段2)

      添加主键 alter table user add primary key(id);

      删除主键 alter table user drop primary key;

      修改主键 alter table user modify id int primary key;

    自增约束 auto_increment

    外键约束 foreign key(class_id) references class(id)

    唯一约束 unique

    非空约束 not null

    默认约束 default

    操作约束的通用方法:

    a.建表时,字段类型后面添加约束

    b.建表时,约束字段

    c.建表后,alter修改表结构

    4、三范式

    第一范式:字段不可拆分

    第二范式:满足第一范式前提下,除主键外所有字段完全依赖于主键。如果不完全依赖(这 种情况只可能出现在联合主键),就需要拆表

    第三范式:满足第二范式前提下,不能出现传递依赖

     四、DML部分

    增:insert into 表名(字段...) values(值...);

    删:delete 表名 where 条件;

    改:update 表名 set 字段 = 值 where 条件;

    查:将查询需要用到的多数语法集合为一个语法(此句子不一定符合语法,只是方便记忆): 

      select distinct/聚合函数(avg、count、sum、max、min)/case when 条件 then 结果1 else 结果2 end       查询字段

      from 表1 a,表2 b

      left join/right join/inner join 表3 c on

      where and/or/between...and/in/like

      group by  分组字段  having 分组后条件 with rollup

      order by 排序字段1 desc/asc , 排序字段2 desc/asc

      limit 偏移量,行数;

     查询语句执行顺序:基本从上到下,select的句子除外,select句子仅仅在order by 之前。

    可去该网址练习一下:https://www.cnblogs.com/jike1219/p/9252582.html

    五、DCL部分

     一般开发人员使用较少,简单了解

    授权:grant 权限1,权限2... on 数据库名.* to 用户名@IP

    撤权:revoke 权限1,权限2... on 数据库名.* from 用户名@IP

    删除用户:drop user 用户名@IP

    查看权限:show grants for 用户名@IP

    六、TCL部分

    存储引擎innoDB支持事务

    事务四大特性(ACID):原子性、一致性、隔离性、持久性

    原子性:一个事务是一个整体,事务内操作要么全执行,要么全不执行

    一致性:一个事务整体的效果符合逻辑和客观事实

    隔离性:事务之前不能相互干扰

    持久性:一旦提交事务,数据库中数据永久改变

  • 相关阅读:
    idea设置全局ignore
    win 2012 安装mysql 5.7.20 及报错 This application requires Visual Studio 2013 Redistributable. Please ins
    win 2012 安装mysql 5.7.20 及报错 This application requires Visual Studio 2013 Redistr
    kafka 删除 topic
    java编译中出现了Exception in thread “main" java.lang.UnsupportedClassVersionError
    Centos中使用yum安装java时,没有jps的问题的解决
    Spring 整合Junit
    Spring纯注解配置
    Spring 基于注解的 IOC 配置
    打印java系统的信息
  • 原文地址:https://www.cnblogs.com/LiuChengGang/p/13218346.html
Copyright © 2011-2022 走看看