mysql数据库开发重点知识总结

一、存储引擎

1.什么是存储引擎?

库就是文件夹,表就是文件,文件都会有文件的格式,存储引擎就是文件格式.

存储引擎就是文件的类型(格式),文本文件有TXT类型、图片有JPG类型、视频有MP4格式.

mysql中最常用的是InnoDB存储引擎(格式)

2.使用存储引擎

1 # 创建表是指定存储引擎
2 create table t1(
3     id int,
4     name char
5 )engine=innodb;
6 
7 # 查看表达存储引擎
8 show create table t1;

二、数据类型

1.数字

1.1整型:tinyinit, int, bigint

存储年龄,等级,ID,各种号码等

1.2小数(浮点型):float, double, decimal

存储薪资,身高,体重,体质参数等

2.字符串

2.1char(10)

简单粗暴,浪费空间,存取速度快

2.2varchare

精准,节省空间,存取速度慢

2.3sql对字符串的优化

创建表时,定长(比如性别)的类型往前放,变长(比如地址)的往后放

超过255个字符,就把文件路径存放到数据库中,比如图片,视频等找一个文件服务器,数据库中只存路径或URL.

3.时间类型:datetime

date, time, datetime, temestamp, year

now()

4.枚举类型与集合类型

enum单选

set多选

三、库

 1 # 1. 新增库
 2 create database db10 charset utf8;
 3 
 4 # 2. 删除库
 5 drop database db10;
 6 
 7 # 3. 修改数据库
 8 alter database db10 charset utf8;
 9 
10 # 4. 查看数据库
11 show databases;
12 show create database db10;
13 select database();  # 查看当前已经选择的数据库
14 
15 # 5. 选择数据库
16 use db10;

 

四、表

1.增删改查

 1 # 1. 创建表
 2 create table t1(
 3     id int,
 4     name varchar(50),
 5     sex enum('male', 'female'),
 6     age int(3)
 7 );
 8 
 9 # 2. 删除表
10 drop table t1;
11 
12 # 3. 修改表
13 # 3.1 修改存储引擎
14 alter table t1
15     engine = innodb;
16 # 3.2 添加字段
17 alter table t1
18     add name varchar(20) not null,
19     add age int(3) not null default 22;
20 alter table student10
21     add stu_num varchar(10) not null after name;  //添加name字段之后
22 alter table student10
23     add sex enum('male', 'female') default 'male' first;  //添加到最前面
24 # 3.3 删除字段
25 alter table student10
26     drop sex;
27 # 3.4 修改字段类型
28 alter table student10
29     modify age int(3);
30 alter table student10
31     modify id int(11) not null primary key auto_increment;  //修改为主键
32 # 3.5 增加约束(针对已有的主键增加auto_increment)
33 alter table student10 
34     modify id int(11) not null primary key auto_increment;
35 alter table student10
36     modify id int(11) not null auto_increment;
37 # 3.6 对已经存在的表增加复合主键
38 alter table service2
39     add primary key (host_ip, port);
40 # 3.7 增加主键
41 alter table student1
42     modify name varchar(10) not null primary key ;
43 # 3.8 增加主键和自动增长
44 alter table student1
45     modify id int not null primary key auto_increment;
46 # 3.9 删除主键
47 alter table student10 
48     modify id int(10) not null ;  //删除自增约束
49 alter table student10
50     drop primary key ;  //删除主键
51 
52 # 4. 查看表结构
53 describe t1;
54 desc t1;  //简写形式
55 show create table t1\G;  //查看表详细结构,可加\G
56 
57 # 5. 复制表
58 create table new_service 
59     select * from service;  //复制表结构+记录(key不会复制:主键,外键,和索引)
60 create table new_service
61     select * from service where 1=2;  //只复制表结构,因为条件为假,查不到任何记录.

2.完整性约束条件

约束性条件和数据类型的宽度一样,都是可选参数

作用:用于保证数据的完整性和一致性

主要分为:

  primary key (pk)  标识该字段为该表的主键,可以唯一的标识记录

  foreign key (fk) 标识该字段为该表的外键

  not null  标识该字段不能为空

  unique key (uk)  标识该字段的值是唯一的

  auto_increment  标识该字段的值自动增长(数据类型,而且为主键)

  default  为该字段设置默认值

 

  unsigned 无符号

  zerofill  使用0填充

说明:

  1.是否允许为空,默认null,可设置为not null,字段不允许为空,必须赋值

  2.字段是否有默认值,缺省的默认值是null,如果插入记录时不给字段赋值,此字段使用默认值

    sex enmu('male', 'female') not null default 'male'

    age int usigned not null default 20 必须为正值(无符号),不允许为空,默认20

  3.是否是KEY

    主键 primary key

    外键 foreign key

    索引 (index, unique...)

3.完整性约束条件操作实例

3.1unique设置唯一约束

 1 # 方法一:
 2 create table t2(
 3     id int,
 4     name varchar(20) unique,
 5     comment varchar(100)
 6 );
 7 # 方法二:
 8 create table t3(
 9     id int,
10     name varchar(20),
11     comment varchar(100),
12     constraint uk_name unique (name)
13 );
1 # 联合唯一
2 create table service(
3     id int primary key auto_increment,
4     name varchar(20),
5     host varchar(15) not null,
6     port int not null ,
7     unique (host, port)
8 );

3.2primary key字段的值不为空且唯一

一个表中可以:

  单列做主键,not null + unique 或 primary key

  多列做主键(符合主键),

但一个表内只能有一个主键primary key

1 # 多列主键
2 create table t4(
3     ip varchar(15),
4     port char(5),
5     service_name varchar(10) not null,
6     primary key (ip, port)
7 );

3.3auto_increment自增长字段

1 create table student(
2     id int primary key auto_increment,
3     name varchar(20),
4     sex enum('male', 'female') default 'male'
5 );

3.4foreign key关联其它表

先建父表,再建关联子表,删除父表记录,子表中对应的记录跟着删.

表之间的关系:一对一,多对一,多对多

 

 1 # 先建立主表department部门表,然后让employee员工表关联该表.
 2 # 表类型必须是innodb存储引擎,且被关联的字段,即references指定的另外一个表的字段,必须保证唯一.
 3 create table department(
 4     id int primary key ,
 5     name varchar(20) not null 
 6 )engine = innodb;
 7 
 8 # 建立多对一的关联表
 9 # dept_id外键,关联父表(department主键id),同步更新,同步删除
10 create table employee(
11     id int primary key,
12     name varchar(20) not null,
13     dpt_id int,
14     constraint fk_name foreign key (dpt_id)
15                      references department(id)
16         on delete cascade 
17         on update cascade 
18 )engine = innodb;
 1 # 多对多关系,需要新建第三张表来描述两个表之间的关系.
 2 create table author2book(
 3     id int not null unique auto_increment,
 4     author_id not null ,
 5     book_id int not null,
 6     foreign key (author_id) references author(id)  # 作者ID与作者表的id字段对应
 7                         on delete cascade 
 8                         on update cascade,
 9     foreign key (book_id) references book(id)  # 书籍ID与图书表的id字段对应
10                         on delete cascade 
11                         on update cascade ,
12     primary key (author_id, book_id)
13 );

 

 

五、记录

在mysql管理软件中,可通过SQL语句中的DML语言来实现数据的操作,包括:

  1.使用insert实现数据的插入

  2.update实现数据的更新

  3.使用delete实现数据的删除

  4.使用select查询数据

1.增

 1 # 1.插入完整书籍(顺序插入)
 2 # 语法一:
 3 insert into 表名(字段1,字段2,字段3,...字段n) values 
 4     (值1,值2,值3,...值n);
 5 # 语法二:
 6 insert into 表名 values 
 7     (值1,值2,值3,...值n);  # 值一定要按照表的字段顺序一个不漏
 8 
 9 # 2.指定字段插入数据
10 insert into 表名(字段1,字段2,字段3,...) values 
11     (值1,值2,值3,...);
12 
13 # 3.插入多条记录
14 insert into 表名 values 
15     (值1,值2,值3...值n),
16     (值1,值2,值3...值n),
17     (值1,值2,值3...值n);
18 
19 # 4.插入查询结果
20 insert into 表名(字段1,字段2,字段3...字段n)
21     select (字段1,字段2,字段3...字段n) from 表2
22     where ...;

 

2.删

1 # 语法
2 delete from 表名
3     where conition;
4 # 实例
5 delete from mysql.user
6     where password='';

 

3.改

1 # 语法
2 update 表名 set
3     字段1=值1,
4     字段2=值2
5     where condition;
6 # 实例
7 update from mysql.user set
8     password=password('123')  # password('123')意思是采用密文方式存储
9     where user='root' and host='localhost';

 

4.查

1 # 单表查询
2 select distinct 字段1,字段2,字段3 from 库.表
3     where 条件
4     group by 分组条件
5     having 过滤
6     order by 排序字段
7     limit n;
1 #  查询的执行顺序
2 # 1. from 表
3 # 2. where 条件
4 # 3. group by 分组条件
5 # 4. having 过滤
6 # 5. deistinct 去重
7 # 6. order by 排序
8 # 7. limit n 取数据n条
9 # 8. select 输出结果

 

六、多表数据的查询操作

1.连表操作

连表后两个表组合成了一个表,可以按照一个表的查询来操作

 1 # 内连接:只取两张表的共同部分
 2 select * from employee inner join department on employee.dpt_id = department.id;
 3 
 4 # 左连接:在内连接的基础上保留左表的记录(左表中没有对应关系的记录保留下来)
 5 select * from employee left join department on employee.dpt_id = department.id;
 6 
 7 # 右连接:在内连接的基础上保留右表的记录(右表中没有对应关系的记录保留下来)
 8 select * from employee right join department on employee.dpt_id = department.id;
 9 
10 # 全外连接:在内连接的基础上保留两表没有对应关系的记录
11 select * from employee left join department on employee.dpt_id = department.id
12 union
13 select * from employee right join department on employee.dpt_id = department.id;

2.子查询

 1 # 1. 带IN关键字的子查询
 2 # 查询平均年龄在25岁以上的部门名
 3 select name from department 
 4     where id in (
 5         select dep_id from employee
 6             group by dep_id
 7             having avg(age) >25
 8         );
 9 # 查看技术部员工姓名
10 select name from employee
11     where dpt_id = (
12         select id from department
13             where name='技术'
14         );
15 # 查看不足1人的部门
16 select name from department
17     where id not in (
18         select distinct dep_id from employee
19         );
20 
21 # 2. 带比较运算符的子查询
22 # 查询大于所有人平均年龄的员工名与年龄
23 select name, age from employee
24     where age >  (
25         select avg(age) from employee
26         );

3.连表与子查询组合使用

1 # 查询每个部门最新入职的那名员工
2 select * from employee as t1
3     inner join (
4         select post,max(hire_date) as max_hire_date from employee
5             group by post) as t2  # 子表也出自员工信息表
6         on t1.post = t2.post
7     where t1.hire_date=t2.max_hire_date;

七、索引

 

posted @ 2020-10-04 12:26  蓝蓝的白云天!  阅读(248)  评论(0编辑  收藏  举报