###############    数据库    ##############

  1. 主要是通过这个学习到什么?
  2. 1,库的操作
  3. 2,表的操作,包括查询,多表查询,子查询
  4. 3,视图,事务,索引,锁,

###############    数据库操作    ##############

  1. """
  2. 数据库操作:
  3. 1.创建数据库
  4. #创建一个名字为 db_name 的数据库,并指定当前库的编码集为utf8
  5. CREATE DATABASE db_name charset utf8;
  6. 2.查看数据库
  7. #查询当前用户下所有数据库
  8. show databases;
  9. 3.选择数据库
  10. USE db_name;
  11. 4.删除数据库
  12. DROP DATABASE db_name;
  13. """

###############    表操作    ##############

  1. """
  2. 表操作:
  3. 1,创建表:
  4. 语法:
  5. CREATE TABLE 表名(
  6. 字段名1 类型[(宽度) 约束条件],
  7. 字段名2 类型[(宽度) 约束条件],
  8. 字段名3 类型[(宽度) 约束条件]
  9. )ENGINE=innodb DEFAULT CHARSET utf8;
  10.  
  11. 实例:
  12. create table student(
  13. id int not null auto_increment primary key,
  14. name varchar(250) not null,
  15. age int not null,
  16. sex enum('男','女') not null default '男',
  17. salary double(10,2) not null
  18. )engine=innodb default charset=utf8;
  19.  
  20. ps: not null :表示此列不能为空
  21. auto_increment :表示自增长,默认每次增长+1
  22. 注意:自增长只能添加在主键或者唯一索引字段上
  23.  
  24. primary key :表示主键(唯一且不为空)
  25. engine =innodb :表示指定当前表的存储引擎
  26. default charset utf8 :设置表的默认编码集
  27.  
  28. 2,查询表:
  29. select name,sex from student;
  30. 或者: select * from student;
  31. #查看表结构
  32. 例: desc student;
  33. #查看创建表信息
  34. show create table student; 
  35.  
  36. 3,修改表结构:
  37. #添加表字段
  38. alter table 表名 add 字段名 类型 约束;
  39. 例如: alter table student add age int not null default 0 after name;
  40. ps: after name 表示在name字段后添加字段 age.
  41. #修改表字段
  42. 方式一: alter table student modify 字段 varchar(100) null;
  43. 方式二: alter table student change 旧字段 新字段 int not null default 0;
  44. ps:二者区别:
  45. change 可以改变字段名字和属性
  46. modify只能改变字段的属性
  47. #删除表字段 :
  48. alter table student drop 字段名;
  49. #更新表名称:
  50. rename table 旧表名 to 新表名;
  51.  
  52. 4,删除表:
  53. #删除表
  54. drop table 表名;
  55. #清空表
  56. truncate table 表名; 
  57.  
  58. 5,复制表:
  59. #只复制表结构和表中数据
  60. CREATE TABLE tb2 SELECT * FROM tb1;
  61. ps:主键自增/索引/触发器/外键 不会 被复制
  62. #只复制表结构
  63. create table tb2 like tb1;
  64. ps: 数据/触发器/外键 不会被复制 
  65.  
  66. 6,数据类型:大致可以分为四类:数值、字符串类型、日期/时间和其他类型。
  67. 数值型
  68. 二进制类型: bit[(M)]
  69. 整数类型: tinyint[(m)] int[(m)] bigint[(m) 作用:存储年龄,等级,id,各种号码等
  70. 小数型: decimal[(m[,d])](特别的:对于精确数值计算时需要用此类型) FLOAT[(M,D)] DOUBLE[(M,D)] (数值越大,越不准确)
  71. 字符型: char (m) varchar(m) text text数据类型用于保存变长的大字符串,
  72. 枚举类型(了解): enum
  73. 集合类型(了解): set
  74. 日期/时间类型: DATE 日期值 TIME 时间值或持续时间 YEAR 年份值 DATETIME 混合日期和时间值 TIMESTAMP 时间戳
  75.  
  76. """

###############    表操作    ##############

  1. """
  2. 数据操作:
  3. 1,插入数据:
  4. #语法一: 按字段进行插入
  5. insert into 表(字段1,字段2 ...) values (值1,值2 ...);
  6. #语法二:按字段顺序插入
  7. insert into 表 values (值1,值2 ...);
  8. #语法三: 插入多条记录
  9. insert into 表 values (值1,值2 ...) ,(值1,值2 ...) ,(值1,值2 ...);
  10. #语法四:插入查询结果
  11. insert into 表(字段1,字段2 ...) select 字段1,字段2 ... from 表;
  12.  
  13. 2,更新数据:
  14. #语法一: 更新整表数据
  15. update 表 set 字段1= '值1', 字段2='值2' ... ;
  16. #语法二:更新符合条件字段3的数据
  17. update 表 set 字段1= '值1', 字段2='值2' ... where 字段3 = 值3;
  18.  
  19. 3,删除数据:
  20. #语法一:整表数据删除
  21. delete from 表 ;
  22. #语法二:删除符合 where后条件的数据
  23. delete from 表 where 字段1=值1;
  24.  
  25. """

###############    单表查询    ##############

  1. """
  2. 单表查询:
  3. 一.简单查询
  4. #查询所有字段信息
  5. select * from person;
  6. #查询指定字段信息
  7. select id,name,age,sex,salary from person;
  8. #别名查询,使用的as关键字,as可以省略的
  9. select name,age as'年龄',salary '工资' from person;
  10. #直接对列进行运算,查询出所有人工资,并每人增加100块.
  11. select (5/2);
  12. select name, salary+100 from person;
  13. #剔除重复查询
  14. select distinct age from person;
  15.  
  16. 二 条件查询
  17. #比较运算符: > < >= <= = <>(!=) is null 是否为null
  18. select * from person where age = 23;
  19. select * from person where age <> 23;
  20. select * from person where age is null;
  21. select * from person where age is not null;
  22. #逻辑运算符: 与 and 或 or
  23. select * from person where age = 23 and salary =29000;
  24. select * from person where age = 23 or salary =29000;
  25.  
  26. 三 区间查询
  27. # 使用 between...and 进行区间 查询
  28. select * from person where salary between 4000 and 8000;
  29. ps: between...and 前后包含所指定的值
  30. 等价于 select * from person where salary >= 4000 and salary <= 8000;
  31.  
  32. 四 集合查询
  33. #使用 in 集合(多个字段)查询
  34. select * from person where age in(23,32,18);
  35. 等价于: select * from person where age =23 or age = 32 or age =18;
  36. #使用 in 集合 排除指定值查询
  37. select * from person where age not in(23,32,18);
  38.  
  39. 五 模糊查询
  40. #模糊查询 like %:任意多个字符, _:单个字符
  41. #查询姓名以""字开头的
  42. select * from person where name like '张%';
  43. #查询姓名以""字结尾的
  44. select * from person where name like '%张';
  45. #查询姓名中含有""字的
  46. select * from person where name like '%张%';
  47. #查询 name 名称 是四个字符的人
  48. select * from person where name like '____';
  49. #查询 name 名称 的第二个字符是 'l'的人
  50. select * from person where name like '_l%';
  51. #排除名字带 a的学生
  52. select * from student where name not like 'a%'
  53.  
  54. 六 排序查询
  55. 升序:ASC 默认为升序
  56. 降序:DESC
  57. PS:排序order by 要写在select语句末尾
  58. #按人员工资正序排列,注意:此处可以省略 ASC关键字
  59. select * from person order by salary ASC;
  60. select * from person order by salary;
  61. #工资大于5000的人,按工资倒序排列
  62. select * from person where salary >5000 order by salary DESC;
  63. #按中文排序
  64. select * from person order by name;
  65. #强制中文排序
  66. select * from person order by CONVERT(name USING gbk);
  67. ps:UTF8 默认校对集是 utf8_general_ci , 它不是按照中文来的。你需要强制让MySQL按中文来排序
  68.  
  69. 七 聚合函数
  70. 聚合函数: 对列进行操作,返回的结果是一个单一的值,除了 COUNT 以外,都会忽略空值
  71. COUNT:统计指定列不为NULL的记录行数;
  72. SUM:计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果为0;
  73. MAX:计算指定列的最大值,如果指定列是字符串类型,那么使用字符串排序运算;
  74. MIN:计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运算;
  75. AVG:计算指定列的平均值,如果指定列类型不是数值类型,那么计算结果为0;
  76. #格式:
  77. select 聚合函数(字段) from 表名;
  78. #统计人员中最大年龄、最小年龄,平均年龄分别是多少
  79. select max(age),min(age),avg(age) from person;
  80.  
  81. 八 分组查询
  82. #分组查询格式:
  83. select 被分组的字段 from 表名 group by 分组字段 [having 条件字段]
  84. ps: 分组查询可以与 聚合函数 组合使用.
  85. #查询每个部门的平均薪资
  86. select avg(salary),dept from person GROUP BY dept;
  87. #查询每个部门的平均薪资 并且看看这个部门的员工都有谁?
  88. select avg(salary),dept,GROUP_CONCAT(name) from person GROUP BY dept;
  89. #GROUP_CONCAT(expr):按照分组,将expr字符串按逗号分隔,组合起来
  90. #查询平均薪资大于10000的部门, 并且看看这个部门的员工都有谁?
  91. select avg(salary),dept,GROUP_CONCAT(name) from person GROUP BY dept; having avg(salary)>10000;
  92.  
  93. 九 分页查询
  94. 好处:限制查询数据条数,提高查询效率
  95. #查询前5条数据
  96. select * from person limit 5;
  97. #查询第5条到第10条数据
  98. select * from person limit 5,5;
  99. #查询第10条到第15条数据
  100. select * from person limit 10,5;
  101.  
  102. 十 正则表达式  
  103.  MySQL中使用 REGEXP 操作符来进行正则表达式匹配。
  104. # ^ 匹配 name 名称 以 "e" 开头的数据
  105. select * from person where name REGEXP '^e';
  106. # $ 匹配 name 名称 以 "n" 结尾的数据
  107. select * from person where name REGEXP 'n$';
  108. # . 匹配 name 名称 第二位后包含"x"的人员 "."表示任意单个字符
  109. select * from person where name REGEXP '.x';
  110. # [abci] 匹配 name 名称中含有指定集合内容的人员
  111. select * from person where name REGEXP '[abci]';
  112. # [^alex] 匹配 不符合集合中条件的内容 , ^表示取反
  113. select * from person where name REGEXP '[^alex]';
  114. #注意1:^只有在[]内才是取反的意思,在别的地方都是表示开始处匹配
  115. #注意2 : 简单理解 name REGEXP '[^alex]' 等价于 name != 'alex'
  116. # 'a|x' 匹配 条件中的任意值
  117. select * from person where name REGEXP 'a|x';  
  118. #查询以w开头以i结尾的数据
  119. select * from person where name regexp '^w.*i$';
  120. #注意:^w 表示w开头, .*表示中间可以有任意多个字符, i$表示以 i结尾
  121.  
  122. 十一 SQL 语句关键字的执行顺序
  123. select name, max(salary)
  124. from person
  125. where name is not null
  126. group by name
  127. having max(salary) > 5000
  128. order by max(salary)
  129. limit 0,5
  130.  
  131. """

###############    多表查询    ##############

  1. """
  2. 多表查询:
  3. 一.多表联合查询
  4. #多表查询语法
  5. select 字段1,字段2... from 表1,表2... [where 条件]
  6. 注意: 如果不加条件直接进行查询,则会出现以下效果,这种结果我们称之为 笛卡尔乘积
  7. #查询人员和部门所有信息
  8. select * from person,dept where person.did = dept.did;
  9. #注意: 多表查询时,一定要找到两个表中相互关联的字段,并且作为条件使用
  10.  
  11. 二 多表连接查询
  12. #多表连接查询语法(重点)
  13. SELECT 字段列表
  14. FROM 表1 INNER|LEFT|RIGHT JOIN 表2
  15. ON 表1.字段 = 表2.字段;
  16. 1 内连接查询 (只显示符合条件的数据)
  17. #查询人员和部门所有信息
  18. select * from person
  19. inner join dept
  20. on person.did =dept.did;
  21. 效果: 大家可能会发现, 内连接查询与多表联合查询的效果是一样的.
  22. 2 左外连接查询 (左边表中的数据优先全部显示)
  23. #查询人员和部门所有信息
  24. select * from person
  25. left join dept
  26. on person.did =dept.did;
  27. 效果:人员表中的数据全部都显示,而 部门表中的数据符合条件的才会显示,不符合条件的会以 null 进行填充.
  28. 3 右外连接查询 (右边表中的数据优先全部显示)
  29. #查询人员和部门所有信息
  30. select * from person
  31. right join dept
  32. on person.did =dept.did;
  33. 效果:正好与[左外连接相反]
  34. 4 全连接查询(显示左右表中全部数据)
  35. 全连接查询:是在内连接的基础上增加 左右两边没有显示的数据
  36. 注意: mysql并不支持全连接 full JOIN 关键字
  37. 注意: 但是mysql 提供了 UNION 关键字.使用 UNION 可以间接实现 full JOIN 功能
  38. #查询人员和部门的所有数据
  39. SELECT * FROM person LEFT JOIN dept ON person.did = dept.did
  40. UNION
  41. SELECT * FROM person RIGHT JOIN dept ON person.did = dept.did;
  42.  
  43. 三 复杂条件多表查询
  44. 1. 查询出 教学部 年龄大于20岁,并且工资小于40000的员工,按工资倒序排列.(要求:分别使用多表联合查询和内连接查询)
  45. #1.多表联合查询方式:
  46. select * from person p1,dept d2 where p1.did = d2.did
  47. and d2.dname='python'
  48. and age>20
  49. and salary <40000
  50. ORDER BY salary DESC;
  51. #2.内连接查询方式:
  52. SELECT * FROM person p1 INNER JOIN dept d2 ON p1.did= d2.did
  53. and d2.dname='python'
  54. and age>20
  55. and salary <40000
  56. ORDER BY salary DESC;
  57. 2.查询每个部门中最高工资和最低工资是多少,显示部门名称
  58. select MAX(salary),MIN(salary),dept.dname from
  59. person LEFT JOIN dept
  60. ON person.did = dept.did
  61. GROUP BY person.did;
  62.  
  63. 3,三张表查询,sql如下:,
  64. select a.uid,a.uname,a.upsw,a.urealname,a.utel,a.uremark, b.rid,b.rname,b.rremark,c.deptid,c.deptname,c.deptremark
  65. from table1 a,table2 b,table3 c
  66. where a.sems_role_rid=b.rid and a.udeptid=c.deptid
  67. 或者:
  68. select a.uid,a.uname,a.upsw,a.urealname,a.utel,a.uremark, b.rid,b.rname,b.rremark,c.deptid,c.deptname,c.deptremark
  69. from table1 a
  70. left join table2 b on a.sems_role_rid=b.rid
  71. left join table3 c on a.udeptid=c.deptid
  72. LEFT JOIN 可以实现统一数据库多表联合查询符合条件的数据。
  73.  
  74. 四 子语句查询
  75. 1.作为表名使用
  76. select * from (select * from person) as 表名;
  77. 2,作为字段的值
  78. select name,salary from person where salary=(select max(salary) from person);
  79.  
  80. 五 SQL逻辑查询语句执行顺序(重点***)
  81. SELECT DISTINCT <select_list>
  82. FROM <left_table>
  83. <join_type> JOIN <right_table>
  84. ON <join_condition>
  85. WHERE <where_condition>
  86. GROUP BY <group_by_list>
  87. HAVING <having_condition>
  88. ORDER BY <order_by_condition>
  89. LIMIT <limit_number>
  90.  
  91. """

###############   约束    ##############

  1. """
  2. 约束
  3.  
  4. 外键约束
  5. 1,创建表时,同时创建外键约束
  6. CREATE TABLE IF NOT EXISTS dept (
  7. did int not null auto_increment PRIMARY KEY,
  8. dname VARCHAR(50) not null COMMENT '部门名称'
  9. )ENGINE=INNODB DEFAULT charset utf8;
  10.  
  11. CREATE TABLE IF NOT EXISTS person(
  12. id int not null auto_increment PRIMARY KEY,
  13. name VARCHAR(50) not null,
  14. age TINYINT(4) null DEFAULT 0,
  15. sex enum('男','女','人妖') NOT NULL DEFAULT '人妖',
  16. salary decimal(10,2) NULL DEFAULT '250.00',
  17. hire_date date NOT NULL,
  18. dept_id int(11) DEFAULT NULL,
  19.   CONSTRAINT fk_did FOREIGN KEY(dept_id) REFERENCES dept(did) -- 添加外键约束
  20. )ENGINE = INNODB DEFAULT charset utf8;
  21. 2,已经创建表后,追加外键约束
  22. #添加外键约束
  23. ALTER table person add constraint fk_did FOREIGN key(dept_id) REFERENCES dept(did);,
  24. #删除外键约束
  25. ALTER TABLE person drop FOREIGN key fk_did;
  26. 注:插入数据时,先插入主表中的数据,再插入从表中的数据。
  27. 删除数据时,先删除从表中的数据,再删除主表中的数据。
  28.  
  29. ###########################################
  30. 其他约束类型:
  31.  
  32. 1.非空约束
  33. 关键字: NOT NULL ,表示 不可空. 用来约束表中的字段列
  34. create table t1(
  35. id int(10) not null primary key,
  36. name varchar(100) null
  37. );    
  38. 2.主键约束
  39. 用于约束表中的一行,作为这一行的标识符,在一张表中通过主键就能准确定位到一行,因此主键十分重要。
  40. create table t2(
  41. id int(10) not null primary key
  42. );
  43. 注意: 主键这一行的数据不能重复且不能为空。
  44.  
  45. 3.唯一约束
  46. 关键字: UNIQUE, 比较简单,它规定一张表中指定的一列的值必须不能有重复值,即这一列每个值都是唯一的。
  47. create table t4(
  48. id int(10) not null,
  49. name varchar(255) ,
  50. unique id_name(id,name)
  51. );
  52. //添加唯一约束
  53. alter table t4 add unique id_name(id,name);
  54. //删除唯一约束
  55. alter table t4 drop index id_name;
  56.  
  57. 4.默认值约束
  58. 关键字: DEFAULT
  59. create table t5(
  60. id int(10) not null primary key,
  61. name varchar(255) default '张三'
  62. );
  63. #插入数据
  64. INSERT into t5(id) VALUES(1),(2);
  65. 注意: INSERT语句执行时.,如果被DEFAULT约束的位置没有值,那么这个位置将会被DEFAULT的值填充
  66.  
  67. """

###############   约束    ##############

mysql数据库-基础--长期维护的更多相关文章

  1. mysql数据库-进阶-长期维护

    ###############    视图    ############## """ 1.视图 视图:是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有 ...

  2. mysql数据库-索引-长期维护

    ###############    索引介绍    ############## """ 1. 索引介绍 需求: 一般的应用系统,读写比例在10:1左右,而且插入操作和 ...

  3. php面试专题---15、MySQL数据库基础考察点

    php面试专题---15.MySQL数据库基础考察点 一.总结 一句话总结: 注意:只写精品 1.mysql定义int(3),那么我存1234就错了么? 不是:无影响:只会影响显示字符的个数:可以为整 ...

  4. MySQL数据库基础知识及优化

    MySQL数据库基础知识及优化必会的知识点,你掌握了多少? 推荐阅读: 这些必会的计算机网络知识点你都掌握了吗 关于数据库事务和锁的必会知识点,你掌握了多少? 关于数据库索引,必须掌握的知识点 目录 ...

  5. 26.MySQL数据库基础

    MySQL数据库基础 目录 MySQL数据库基础 数据库的概念 数据 表 数据库 数据库的管理系(DBMS) 数据库系统 访问数据库的流程 数据库系统发展史 当今主流数据库介绍 关系数据库 关系数据库 ...

  6. mysql数据库基础的简单操作指南

    最近在学习mysql,本文是做的关于mysql学习的笔记,跟大家分享一下,希望对大家学习mysql知识有所助益.mysql现在几乎已经成了网站建设的主流数据库,很多php网站系统都采用了mysql数据 ...

  7. MySQL数据库基础

    MySQL数据库基础 本文的所有操作是基于CMD环境,MySQL通过在命令行中输入SQL语句对数据库进行操作.配置问题可参考<打通MySQL的操作权限>中的内容,该文算是针对前期的环境配置 ...

  8. Mysql数据库基础学习笔记

    Mysql数据库基础学习笔记 1.mysql查看当前登录的账户名以及数据库 一.单表查询 1.创建数据库yuzly,创建表fruits 创建表 ) ) ,) NOT NULL,PRIMARY KEY( ...

  9. Mysql数据库基础操作

    Mysql数据库基础操作 在mysql数据库中开启使用tab键补全功能 1)修改主配置文件/etc/mysql/my.cnf(mysql和mariadb目录有些不同) vim /etc/mysql/m ...

随机推荐

  1. NiFi_Demo_调度示例

    1.背景 要求:每天凌晨1:00后开始每2min执行一次sql查询 2.作业编排 3.各模块配置 3.1 GenerateFlowFile 作用:用于产生flowfile,该flowfile内容为空. ...

  2. POJ 1850:Code 组合数学

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8710   Accepted: 4141 Description ...

  3. 动态类型识别&动态创建

    以下大部分内容摘自<windows程序设计 第2版> 王艳平 张铮 编著 动态类型识别:在程序运行过程中,辨别对象是否属于特定类的技术. 应用举例:函数辨别参数类型.需要针对对象的类编写特 ...

  4. POJ 3660 Cow Contest【Floyd 传递闭包】

    传送门:http://poj.org/problem?id=3660 题意:有n头牛, 给你m对关系.(a, b)表示牛a能打败牛b, 求在给出的这些关系下, 能确定多少头牛的排名. 传递闭包: 关系 ...

  5. RTMP、RTSP

    一.参考网址 1.RTMP.RTSP.HTTP视频协议详解(附:直播流地址.播放软件) 2.海康RTSP流转RTMP并推送至WEB端展示 3.使用FFmpeg将rtsp流摄像头视频转码为rtmp播放 ...

  6. 05 SpringMVC:02.参数绑定及自定义类型转换&&04.SpringMVC返回值类型及响应数据类型&&05.文件上传&&06.异常处理及拦截器

    springMVC共三天 第一天: 01.SpringMVC概述及入门案例 02.参数绑定及自定义类型转换 03.SpringMVC常用注解 第二天: 04.SpringMVC返回值类型及响应数据类型 ...

  7. PAT Advanced 1085 Perfect Sequence (25) [⼆分,two pointers]

    题目 Given a sequence of positive integers and another positive integer p. The sequence is said to be ...

  8. 分布式场景下Kafka消息顺序性的思考

    如果业务中,对于kafka发送消息异步消费的场景,在业务上需要实现在消费时实现顺序消费, 利用kafka在partition内消息有序的特点,消息消费时的有序性. 1.在发送消息时,通过指定parti ...

  9. win32框架

    win32的框架 1.入口函数 2.窗口注册类信息 3.窗口创建 4.显示窗口 5.更新窗口 6.消息循环 7.入口函数结束 WNDCLASSEX wcex;窗口类结构 wcex.cbSize = s ...

  10. BBS数据库设计

    BBS数据库设计 一.BBS数据库设计 # models.py from django.db import models # Create your models here. from django. ...