2018/12/6 星期四 19:34:07

  1. authot by dabaine

数据库注释;

  1. -- 这就是注释
  2. /*.....*/ 这也是注释

创建库;

  1. create databse [if not exists] dabaine [character set "utf8"];

查看所有数据库;

  1. show databses;

查看数据库结构:

  1. show create database dabaine;

查看当前数据库;

  1. select database();

修改数据库;

  1. alter database dabaine [character set "gbk"];

删除数据库;

  1. drop database [if exists] dabaine;

使用数据库;

  1. use database;

创建表;

  1. create table dabaine(
  2. id smallint(10) primary key not null auto_increment,
  3. name varchar(25) not null,
  4. gender boolean not null
  5. );

删除表;

  1. drop table dabaine;

查看表结构;

  1. eg1:show create table dabaine;
  2. eg2:show columns from dabaine;

查看表的全部信息;

  1. desc dabaine;

修改表结构;

  1. 增加字段:
  2. alter table dabaine add [column],add [column]......;
  3. 修改类型:
  4. alter table dabaine modify colum_name attribute [first|after column_name] colum_name;
  5. 修改列名:
  6. alter table dabaine change column_name new_column_name type [约束条件];
  7. 删除字段:
  8. alter table dabaine drop [column];
  9. 重命名:
  10. rename table table_name to new_table_name;

修改表内容;

  1. 插入:
  2. eg1:insert into dabaine (id, name) values(1,"dabaine");
  3. eg2:insert into dabaine set id = 2,name="dabaine";
  4. 更新:
  5. update dabaine set name="cody" where name="dabaine";
  6. 删除:
  7. eg1:delete from dabaine where name = "cody";
  8. eg2:truncate table dabaine; --把表摧毁,重新创建一张新表;

查询顺序;

  1. select [distinct] *|field ... from dabaine
  2. where (不分组筛选)
  3. group by field
  4. having (分组后筛选)
  5. order by field
  6. limit

查询别名;

  1. selct distinct id + 10 as id from dabaine;

执行顺序;

  1. from,where,select,group by,having, order by

聚合函数;

  1. select name, sum(grade) from dabaine group by name;
  2. ifnull(grade,0) --如果grade为空,则给它定为0;

外键约束;

  1. 创建主表:
  2. create table class(
  3. id int(10) primary key auto_increment,
  4. name varchar(20),
  5. age int(5)
  6. );
  7. 主表添加数据(多条):
  8. insert into class(name,age) values
  9. ("cody",18),
  10. ("solider",19),
  11. ("guan",21),
  12. ("lee",22),
  13. ("strong",28),
  14. ("pig",38);
  15. 创建子表:
  16. create table student(
  17. id int(10) primary key auto_increment,
  18. name varchar(20),
  19. age int(5),
  20. teacher_id int(10), --绑定外键的字段要和主表中的字段类型保持一致;
  21. constraint dabaine --给外键命名大白讷
  22. foreign key (teacher_id) --给子表的属性选择外键绑定
  23. references class(id) --映射主表的属性(追随主表的id字段)
  24. );
  25. 子表添加数据:
  26. insert into student(name,age,teacher_id) values
  27. ("cody",18,1),
  28. ("solider",19,2),
  29. ("guan",21,3),
  30. ("lee",22,4),
  31. ("strong",28,5),
  32. ("pig",38,6);
  33. 这时,主表和子表已经有关联了,不可以随便删除主表的记录;
  34. 增加外键:
  35. alter table son_table_name add constraint cody
  36. foreign key(son_table_field)
  37. references primary_table(field);
  38. 删除外键:
  39. alter table son_table_name drop foreign key cody;

级联删除(cascade);

  1. create table studentNew(
  2. id int(10) primary key auto_increment,
  3. name varchar(20),
  4. age int(5),
  5. teacher_id int(10),
  6. constraint cody foreign key (teacher_id)
  7. references class(id)
  8. on delete cascade --级联删除
  9. );
  10. constraint cody foreign key (teacher_id)
  11. references class(id)
  12. on delete set null --主表删除后,子表记录设置为空值,且子表的字段属性不能设置为not null;
  13. on delete restrict --拒绝对主表进行更新删除操作;
  14. on delete no action --类似于restrict

多表查询;

  1. 笛卡尔积连接:
  2. A表中的全部数据m * B表中的全部数据n条;
  3. 连接查询~内连接:
  4. inner join
  5. eg1:select tableA.id,tableA.name,tableB.name from
  6. tableA,tableB where tableA.id = tableB.tableA_id
  7. eg2:select tableA.id,tableA.name,tableB.name from tableA
  8. inner join tableB on tableA.id = tableB.tableA_id
  9. +---------+----+---------+
  10. | name | id | name |
  11. +---------+----+---------+
  12. | cody | 1 | cody |
  13. | solider | 2 | solider |
  14. | guan | 3 | guan |
  15. | cody | 4 | lee |
  16. | strong | 5 | strong |
  17. | lee | 6 | pig |
  18. +---------+----+---------+
  19. 连接查询~左外连接(左连接):
  20. left join
  21. select tableA.id,tableA.name,tableB.name from tableA
  22. left join tableB on tableA.id = tableB.tableA_id
  23. --左连接以左表为主,select所选择的字段,左表中的记录会全部显示,而右表会去匹配左表里的记录,没有的则显示空值;
  24. +----+---------+---------+
  25. | id | name | name |
  26. +----+---------+---------+
  27. | 1 | cody | cody |
  28. | 2 | solider | solider |
  29. | 3 | guan | guan |
  30. | 4 | lee | cody |
  31. | 5 | strong | strong |
  32. | 6 | pig | lee |
  33. +----+---------+---------+
  34. 连接查询~右外连接(右连接):
  35. right join
  36. 类似左连接,以右表为主;
  37. +------+---------+---------+
  38. | id | name | name |
  39. +------+---------+---------+
  40. | 1 | cody | cody |
  41. | 4 | lee | cody |
  42. | 2 | solider | solider |
  43. | 3 | guan | guan |
  44. | 6 | pig | lee |
  45. | 5 | strong | strong |
  46. | NULL | NULL | pig |
  47. +------+---------+---------+

嵌套;

  1. 查询嵌套:
  2. select * from table_name where field in (select field from table_name);
  3. 复制表:
  4. create table new_table(select * from old_table); --原表中的约束不会复制过来,需要重新添加
  5. selcet * from table_name where exists
  6. (selcet field from table_name where....)
  7. --exists 后面的语句会返回一个布尔值,true则执行前面的select语句,
  8. flase 则返回空值;

索引;

  1. unique(唯一索引),fulltext(全局索引),spatial(空间索引),index|key(普通索引)
  2. 添加索引:
  3. eg1:create
  4. [unique|fulltext|spatial] index|key
  5. index_name on table_name (字段名[(长度)] [asc|desc]);
  6. eg2:alter table table_name
  7. add [unique|fulltext|spatial] index|key index_name (字段名[(长度)] [asc|desc]);
  8. 删除索引:
  9. drop index index_name on table_name;
  10. unique:唯一索引的字段不能重复;
  11. 多列索引:给多个字段添加索引 (field1,field2...)

事务;

  1. start transaction; --开启事务
  2. Rollback; --回滚事务(撤销)
  3. Commit; --提交事务;
  4. savepoint; 保留点,事务处理中的临时占位符;
  5. savepoint name;
  6. rollback to svaepoint_name;

存储过程;

MySql的基操勿六的更多相关文章

  1. Mysql的基操

    创建一个数据库   (myschool是数据库名) create database myschool; 删除数据库 drop database myschool 创建一个表:(Student是 表名) ...

  2. 我的MYSQL学习心得(十六) 优化

    我的MYSQL学习心得(十六) 优化 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...

  3. mysql基操

    创建数据表: create table tt1( id int, name varchar(20), age int,sex boolean ); insert into tt1 values(1,& ...

  4. 替小白整理的 linux基操命令 切勿扣6 不用感谢

    Linux --------小白必会的基本命令 命令行提示字符[root@localhost ~]#[当前登录系统的用户@主机名称 当前所在的目录]## 表示为管理员登录$ 表示为普通用户登录   切 ...

  5. MySQL数据库学习笔记(六)----MySQL多表查询之外键、表连接、子查询、索引

    本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...

  6. MySql:SELECT 语句(六) CONCAT() 函数的使用

    一.计算字段 为什么要用计算字段? 1)想要在一个字段中既显示公司地址,又显示公司名称,但是往往这两个都不在一个字段中 2)列数据是大小写混合的,但是报表程序需要把他们全部按大写形式展示出来 3)需要 ...

  7. MySQL高级知识(十六)——小表驱动大表

    前言:本来小表驱动大表的知识应该在前面就讲解的,但是由于之前并没有学习数据批量插入,因此将其放在这里.在查询的优化中永远小表驱动大表. 1.为什么要小表驱动大表呢 类似循环嵌套 for(int i=5 ...

  8. Redis基操

    Redis key-value类型的缓存数据库 指定IP和端口连接redis: ./redis-cli -h ip -p port Redis基本操作命令 命令 返回值 简介 ping PONG 测试 ...

  9. MongoDB基操

    基本概念 database 数据库 包含多个collection collection 集合 包含多个文档document(类JSON对象) document 文档 一个文档对象中包含多个key-va ...

随机推荐

  1. mysql之SQL入门与提升(四)——终结篇,函数

    一.SQL Aggregate (聚合)函数 SQL Aggregate 函数计算从列中取得的值,返回一个单一的值. AVG() - 返回平均值 COUNT() - 返回行数 FIRST() - 返回 ...

  2. 慕课笔记-Java入门第三季

    1.自定义异常 自定义异常必须继承Exception类或者其子类. 2.字符串 String对象创建后则不能被修改,是不可变的,所谓的修改其实是创建了新的对象. 多次创建的字符常量,Java编译程序只 ...

  3. Mybatis思

    站在巨人的肩膀上,感谢! mybatis源码分析之Mapper代理实现分析 2017年11月21日 23:39:04 huangshanchun 阅读数:277    版权声明:欢迎转载,如有不足之处 ...

  4. Maven - settings.xml里的offline节点的作用

    场景 某天我在本地修改了某个子项目的代码,并进行了打包:mvn clean install -DskipTests,接着我运行父项目却发现自己刚刚的改动并没有生效,或者说,我刚刚打包好的子项目变回了打 ...

  5. JTextArea设置滚动条

    应将JTextArea置于JScrollPanel中 若要使只有垂直滚动条而没有水平滚动条,使用JTextArea.setLineWrap(true),自动换行.   以下摘自[url]http:// ...

  6. JSP && Servlet | 错误统一处理

    对404错误和500错误处理: 在WebContent文件下新建404.jsp 和 500.jsp 显示错误时弹出的信息 <%@ page language="java" c ...

  7. 可视化-grafana_使用influxDB数据

    1 添加数据源 给数据源取个名字,然后选择数据类型为influxDB. HTTP:8086是influxDB的HTTP查询API,grafana是通过这个接口获取数据. Details:选择从infl ...

  8. C#基础之析构函数

  9. I/O————File对象

    File文件对象 文件和文件夹都是用File代表 创建一个文件对象,(并不会有真正的文件或文件夹被创建) File f1 = new File("d:/lolfilder"); S ...

  10. Emacs Org-mode中英文字体设置

    Emacs Org-mode中英文字体设置 Table of Contents 1. 缺省字体存在的问题 2. 解决方法 2.1. 环境说明 2.2. 思路和方法 2.3. emacs设置代码 2.4 ...