2019年到了,小编祝大家新的一年心想事成,万事如意。当然学习不能少,不能停止学习的脚步。今天介绍mysql的几个知识点:
一、数据库基础
二、mysql数据库
三、数据库常用命令
四、数据库表
五、数据类型
六、记录操作
七、练习
一数据库基础:
数据库:按照一定结构来存放数据的仓库。(DB)
数据库管理系统:(database management system)(DBMS)
用户:
数据库系统:用户、数据库管理系统 数据库。
1 为什么要用数据库:
2 数据库设计:
1》了解数据库设计步骤
2》掌握绘制数据库的e-r图
3》数据库的规范化---三大范式。
1》为什么需要数据库设计:
良好的数据库设计:
节省数据的存储空间
很好的保证数据的完整性
方便数据库应用系统的开发
2》web/软件项目开发的周期:
需求分析阶段:分析客户的业务和数据处理需求。
概要设计阶段:设计数据库e-r模型(实体关系图),需求必须明确。
详细设计阶段:把e-r转换成数据库表,且使用数据库三范式进行审核。
代码编写阶段:选择具体的数据库进行物理实现。且编写代码实现应用。
软件测试阶段:
安装部署
3》基本概念:
实体:现实世界中事物的抽象,
凡是可以区分开并被我们认识的事、物、概念等对象均可认为是实体。
属性:每个实体都有一组特征或性质,称为实体的属性。
事物用“有”来形容的。
一个属性就是表中的一列。
实体的属性值是数据库中主要的数据。
关系:两个或多个实体之间的联系。
关系类型:
一对一:
一对多:
多对一:
多对多:
建模:现实数据转换成信息数据的过程。e-r
数据模型:
e-r模型:实体关系模型
实体:矩形。
属性:椭圆。
关系:菱形。
4》数据库的规范化---三大范式。
三大范式:
1 确保每列的原子性:1nf
2 确保每列的原子性:除了主键以外的其他列,都依赖于该主键。2nf
主键:
3 确保每列的原子性:除了主键以外的其他列,都依赖于该主键。3nf
且除了主键以外的其他列都不传递依赖于该主键。
属性--》列----》字段。
数据库:
层次性:
网状型:
关系型:二维表格:列:(field)字段 行:(record)记录
db2 oracle mysql sql server
二、mysql数据库
mysql
mysql -h [主机地址] -u [用户名] -p 回车
输入密码:
exit; 退出mysql
show databases; 查看现有的数据库
查看已经创建的database
1 创建数据库: create database [if not exists] 数据库名称 [default character set '字符集']
2 删除数据库:drop database 数据库名称
3 使用数据库:use 数据库名称
四、数据库表
1 创建表:
create table [if not exists] 表名(
字段名 列类型 [not null|null][primary key][unique][auto_increment][default value],
字段名 列类型 [not null|null][primary key][unique][auto_increment][default value],
字段名 列类型 [not null|null][primary key][unique][auto_increment][default value],
字段名 列类型 [not null|null][primary key][unique][auto_increment][default value],
...........
)
2 desc 表名 :查看表结构。同理 show tables 查看库里面所有的表
查看创建的表
3 修改表:
添加字段:
alter table 表名 add 字段名 列类型 [not null|null][primary key][unique][auto_increment][default value]
ar_content
alter table 表名 add 字段定义 after ar_id;
删除字段:
alter table 表名 drop 字段名
修改字段:
alter table 表名 modify 字段名 字段新类型
完整修改字段:
alter table 表名 change 旧字段名称 新字段定义
修改表名称
alter table 表名 rename 新名字
删除表
drop table [if exists] 表名;
五、数据类型
字段名 字段类型
字段类型:
1 整形:
tinyint : -128-127 /0-255
smallint: -32768-32767 /0-65535
mediumint: -8088608--8088607/0---16777215
int 占4个字节。*****
bigint :占8个字节。
2 字符串型:
char: 1-255 定长字符;
varchar:1-255 变长字符。
text:65535个字符:
mediumtext:16777215字符。
enum(val1,val2,val3....)
3 日期型:
date : 2011-08-10
datetime: 2011-08-10 12:12:12
article
arId(primary key,auto_increment) int(8)
arTitle varchar(200)
arAuthor varchar(20)
arContent text
arDatedate
arUrl varchar(255)
arImgvarchar(255)
arStatus (0 表示不推荐,1 推荐)enum(0,1)
create table article(
arId int(8) not null primary key auto_increment,
arTitle varchar(200),
arAuthor varchar(20),
arContent text,
arDate date,
arUrl varchar(255),
arImg varchar(255),
arStatus enum('0','1')
);
六、记录操作
添加记录:
insert into 表名(字段名称1,字段名称2,字段名3.....) values(val1,val2,val3,...)
insert into article(arTitle,arAuthor,arContent,arDate,arUrl,arImg,arStatus)
values("三毛流浪","张三","免费模板网-提供免费网站模板下载的好平台","2018-08-10","http://www.freemoban.com","upload/1.jpg",0);
set names gbk/utf8; 设定字符集。(一般解决网站乱码问题)
解决中文乱码:
show variables like "%character%";
show variables like "%character%";
show variables like "%character%";
把查看结果中的所有“拉丁”都设置为utf8
set character_set_client=utf8 ;
set character_set_connection=utf8 ;
set character_set_database=utf8 ;
set character_set_results=utf8 ;
set character_set_server=utf8 ;
set names "utf8"
新建:test.sql
source /home/www/test.sql; 导入外部的sql文件。
知识点:
一 记录操作:
二 子查询
三 连接查询
一 记录操作:
1 插入记录:
insert into 表名(字段名称1,2,3...) values(val1,2,3,....);
2 修改操作:
update 表名 set 字段名1=val1,字段名2=val2,..... where arId=3;
update article set arAuthor="王五" where arId=3;
update article set arAuthor="张三",arContent="bbbbbbb" where arId=3;
多表更新:(了解)
update 表名1,表名2 set 字段名1=val1,字段名2=val2.....where 连接条件
3 删除操作:
delete from 表名 where 条件;
记录删除后,位置还在。
4 选择语句:
select 字段列表 from 表名 [where 条件][order by 字段名 asc|desc]
[limit 起始位置,长度][group by 字段名称];
强调:
字段列表:arTitle as at,arContent,arAuthor... [as 别名];
字段列表:* 查找所有字段。
limit:limit 起始位置,记录条数 limit 0,4
1 order by 字段名 asc|desc
2 select arTitle as at,arAuthor as aa from article order by arId desc;
3 select arTitle as at,arAuthor as aa from article limit 2,2;
4 倒序排列价格在30--60之间
select * from books where price between 30 and 60 order by price desc;
5 选择出的书籍价格(30,60,40,50)
select bName,publishing,price from books where price=30 or price=40 or price=50 or price=60;
select bName,publishing,price from books where price in(30,40,50,60) order by price desc;
6 模糊搜索:
select bName,price from books where bName like '%程序%';
补充:
in 效率很低:
like 效率很低:
全文索引:varchar text
7 条件:
范围运算:
[not]between ....and....
select * from books where price not between 30 and 60 order by price desc;
in
字段名 [not] in(val1,val2,val3.....)
模糊匹配:
字段名 [not]like '通配符' ----》% 任意多个字符; _任意一个字符。
逻辑运算:
and or not
等于=
不等号
二 子查询:
1 概念:在select 的where条件中又出现了select
2 选择 类型名为“网页设计”的图书:
select * from books where btypeid=(select btypeid from category where bTypeName='网页设计')
选择类型名称为“黑客图书”的图书;
select * from books where btypeid=(select btypeid from category where bTypeName='黑客图书')
3 引发子查询的情况:
1》由比较运算符引起:
=
选择bName ,price 条件:价格是电子工业出版社出版的书中最便宜还便宜。
select bName,price from books where publishing="电子工业出版社" order by price asc limit 0,1;
子查询:
select bName,price from books where price
select * from books where btypeid=(select btypeid from category where bTypeName='黑客图书')
select bName,price from books where price
三 连接查询:以一个共同的字段,求两张表当中符合条件的并集,且一共同字段把这两张表连接起来。
常用的连接:
内连接:根据表中的共同字段进行匹配,*********
外连接:左外连接、有外链接。************************
交叉连接:两张表记录的乘积。
1 语法:
select 语句 连接类型 数据表 on 连接条件。
2 内连接:根据表中的共同字段进行匹配
create table student(
sid int(4) primary key auto_increment,
name varchar(50)
);
create table grade(
id int(4) primary key auto_increment,
score varchar(20),
sid int(4)
);
查询有的成绩的人的信息.
select s.*,g.* from student as s,grade as g where s.sid=g.sid;
select s.*,g.* from student as s inner join grade as g on s.sid=g.sid;
3 外连接:现实某数据表的全部记录和另外数据表中符合连接条件的记录。
左连接: select 语句 a表 left[outer] join b表 on 连接条件
a表是主表,都显示。
b表从表
主表内容全都有,从表内没有的现实null。
select * from student as s left join grade as g on s.sid=g.sid;
右连接:
select 语句 a表 right[outer] join b表 on 条件
select * from student as s right join grade as g on s.sid=g.sid;
作业:
1 建立studeng grade表
把左右连接 语句5词
2类型表
shops_category
cgIdcgPIdcgNamecgLevel
10小说1
20文学1
30青春文学1
40传记1
51武侠小说2
61古典小说2
72诗歌2
83漫画2
shops_books
sboId
sboName
sboAuthor
sboISBN
sboPublish
sboDate
cgId(主类型) 1
cgSonId(子类型)
1> shops_books 插入10条记录
2> 题目 选出所有小说类型的书籍。
3> 选出小说中武侠小说的书籍。
总结一下,今天介绍的知识点很多,但是都很基础,在工作中经常遇到,有不懂的朋友欢迎留言。
领取专属 10元无门槛券
私享最新 技术干货