前言
因为最近要考MySQL了,打算把数据库的所有基础知识复习一遍。也就不分篇写了,文章可能会比较长。
数据库的基本操作
创建数据库
create database database_name;
可以使用show database 查看当前所有数据库。
删除数数据库
drop database database_name;
数据库引擎
引擎包含InnoDB、MyISAM、MEMORY。
## 查看默认数据库存储引擎
show variables like 'storage_engine';
数据表的基本操作
创建数据表
数据表的语法形式
- 在使用数据表之前应该先使用
use <数据库名>指定在哪个数据库操作。
create table <表名>
(
字段名1,数据类型,
字段名2,数据类型
);
## 创建好后下面的语句查看数据表
show tables;
使用主键约束
主键约束,要求主键列的数据唯一。并且不允许为空。主键分为两种类型:单字段主键和多字段联合主键。
create table tb_1
(
id int(11) primary key,
name vatchar(11)
);
## 或者
create table tb_1
(
id int(11),
name vatchar(11),
primary key(id)
);
## 多字段联合主键
create table tb_1
(
id int(11),
name vatchar(11),
primary key(id),(name)
);
使用外键约束
外键的作用是保持数据的一致性,完整性。外键用来再两个表的数据之间建立连接,它可以是一列或者多列。一个表可以有一个或多个外键。一个表的外键可以为空值。外键名为定义的外键约束的名称。
create table tb_1
(
id int(11),
deptid int (11),
constraint <外键名称> foreign key(deptid) references tb_1(id)
);
使用非空约束
create table tb_3
(
id int(11) not null,
name vatchar(11)
);
使用唯一约束
create table tb_4
(
id int(11) unique,
name vatchar(11)
);
## 或者
create table tb_4
(
id int(11),
name vatchar(11),
constraint sth unique(name)
);
使用默认约束
create table tb_5
(
id int(11) default 1,
name vatchar(11)
);
使用表的属性值自动增加
create table tb_6
(
id int(11) auto_increment,
name vatchar(11)
);
查看数据表结构
查看表基本结构语句describe
describe 表名;
## 或者
desc 表名;
查看表详细结构语句
show create table <表名>
修改数据表
修改表名
alter table <旧表名> rename <新表名>;
修改字段数据类型
alter table <表名> MODIFY <字段名> <数据类型>;
修改字段名
alter table <表名> change <旧字段名> <新字段名> <新数据类型>;
添加字段
alter table <表名> add <新字段名> <数据类型> [约束条件] [first | after 已存在字段名];
删除字段
alter table <表名> drop <字段名>;
修改字段的排列位置
alter table <表名> modiey <字段 1> < 数据类型> first|after <字段 2>;
更改表的存储引擎
alter table <表名> engine=<更改后的数据存储引擎>;
删除表的外键约束
alter table <表名> drop foreign key <外键约束名>;
删除数据表
drop table [if exists] 表1,表2···表n;
查询数据
基本查询语句
select * from <表名>;
单表查询
查询指定记录
select 字段名1,字段名2····
from 表名
where 查询条件
查询条件有= > < != betwen
带IN关键字的查询
select <字段名>
from 表名
where 字段名 IN ();
带范围查询
select 字段名 from 表名
where 字段名 between 1 and 2;
字符匹配查询
## 所有以“b”字母开头的水果。
select 字段名
from 表名
where 字段名 like 'b%';
## 两个字符
like '__'
## b开头y结尾
like 'b%y'
查询空值
slelect 字段名 from 表名 where 字段名 is (not) null;
带and的多条件查询
slelect 字段名 from 表名 where 查询条件 and 查询条件;
带or的多条件查询
slelect 字段名 from 表名 where 查询条件 or 查询条件;
and 和 or 可以一起使用,但是and 的优先级高于 or ,会先处理and。
查询结果不重复
select distinct 字段名 from 表名;
对查询结果排序
slelect 字段名 from 表名 order by 字段名;
## 指定排序方向
slelect 字段名 from 表名 order by 字段名 desc;
## desc降序排列,asc升序排列(默认的)。
分组查询
select 字段名 as 名称 from 表名 group by 字段名;
## 使用having过滤分组
select 字段名 as 名称
from 表名
group by 字段名 having 条件;
## 在group by 中使用with rollupo
## 作用是在最后进行一次统计
select 字段名 as 名称
from 表名
group by 字段名 with rollup;
使用limit限制查询结果数量
limit [位置偏移量],行数
## 需要注意的是
## 没有指定位置偏移量返回的是第一行开始。指定了之后会从指定的数字作为开始行。
集合函数查询
count()
sum()
avg()
max()
min()
子查询
## 返回tb12表的所有num2列,将tb11中的num1的值进行比较,大于就返回。
select num1 from tb11 where num1 > any(select num2 from tb12);
## 返回tb11表中比tb12表num2列所有值都大的值。
select num1 from tb11 where num1 > all(select num2 from tb12);
## 带exists关键字查询
## exists后面的结果至少返回一行,此时外层查询语句执行,如果没有任何行,外层语句将不再进行查询。
合并子查询
select column,... from table1
union [all]
select column,... from table2
插入、更新、删除数据
插入数据
## 为表的所有字段插入数据
insert into table_name(column_list) values(value_list);
## 为表的指定字段插入数据
insert into person(name,age,info)
values('willam',20,'sports man');
更新数据
update table_name
set colimn_namel = value1,column_name2=value2,.....,column_namen-valuen
where(condition);
删除数据
delete from table_name [where<condition>];
索引
索引的分类
普通索引、唯一索引、单列索引、组合索引、全文索引、空间索引
创建索引
## 创建表的时候创建索引
### 普通索引
create table table_name(
id int(11),
index(id)
);
### 唯一索引
unique index uniqidx(id)
### 单列索引
index singleidx(id)
### 组合索引
index multiidx(id,name)
### 全文索引
fulltext index fulltxtidx(id)
### 空间索引
## 在已存在的表建立索引
alter table table_name add index bknameidx(id);
删除索引
alter table table_name drop index index_name;
存储过程和函数
创建存储过程和函数
## 普通的定义
delimiter //
create procedure func_name()
begin
select * from 表名
end //
delimiter ;
## 定义名为countProc1的存储过程,然后调用这个过程。
delimiter //
create procedure countProc1(in sid int,out num int)
begin
select count(*)into num from fruits where s_id=sid;
end //
delimiter ;
call countProc1(101,@num);
select @num;
视图
创建视图
create view view_t as select quantity,price,quantity*price from t;
查看视图
show table status like '视图名';
更新视图
update view_t set quantity=5;
触发器
创建触发器
create trigger trigger_name trigger_time trigger_event
on tb1_name for each row trigger_stmt
查看触发器
show triggers;
就先到这里吧。我把基础的从头到尾全部打了一个遍。如果拿这个当做一个很基础的复习资料应该可以了吧。有问题的话还望在博客下方留言指正一下。要是转载什么的还请保留我博客下方的声明哈。打这个是真的占时间。。。时间也不早了。早点睡觉了。晚安哈!