数据库
数据存储的仓库
数据库管理系统
操纵和管理数据库的大型软件
Sql
操作关系型数据库的编程语言
关系型数据库
建立在关系模型基础上,由多张相互连接的二维表组成的数据库,格式统一,标准统一
SQL
Sql的通用语法:
1. 单行或多行书写,分号结尾
2. 使用空格或缩进增强可读性
3. 不分大小写,关键字大写
4. --单行注释或#注释
/多行注释/
Sql的分类
DDL:数据定义语言,定义数据库对象(数据库,表,字段)
DML:数据操作语言,增删改
DQL:数据查询语言
DCL:数据控制语言,创建数据库用户、控制数据库访问权限
DDL
查询所有数据库:show databases;
查询当前数据库:select database();
创建:Create database [if exists] 数据库名 [defalut charset 字符集][collate排序规则]
//创建:
Create database [if exists] 数据库名 [defalut charset 字符集][collate排序规则]
删除: Drop database[if exists]数据库名
使用: Use 数据库
表操作
DDL:
创建: Create table 表名(
字段1 字段1类型[comment 字段1 注释],
字段2 字段2类型[comment 字段2注释]
)[comment 表注释]
create table user(
id int comment"编号";
name varchar(50) comment"年龄",
gender varchar(1) comment "性别",
name varchar(50) comment"年龄"
)comment "yonghu";
create table student1(
id int,
name varchar(16),
password varchar(20),
age int,
sex varchar(5),
birthday timestamp,
score decimal(3,1), -- decimal存小数,(3,1)表示有效数字有3位,小数点后1位
introduction text -- text 存长文本数据
);
查询当前数据库所有表:show tables;
查询表结构:desc表名;
查询指定表的建表语句:show create table 表名;
//数据类型
添加
Alter table 表名 add 字段名 类型(长度)[comment 注释];
修改
修改数据类型: alter table 表名 modify 字段名 新数据类型(长度);
修改字段名和字段类型:alter table 表名 change 旧字段名 新字段名 类型(长度)[comment 注释];
删除
Alter table 表名 drop 字段名;
修改表名:alter table 表名 rename to 新表名;
删除表
删除表:drop table [if exists]表名;
删除表并重新创建:truncate table 表名;
DML
添加
1. 给指定字段添加数据:insert into 表名(字段1,字段2,...)values(值 1,值2,..);
-
给全部字段添加数据:insert into 表名 values(值1,值2,...);
-
批量添加数据:insert into 表名(字段1,字段2,...)values(值1,值2,..),(值1,值2,..),(值1,值2,..);
insert into 表名 values(值1,值2,...),(值1,值2,...);
修改
Update 表名 set 字段1=值1,字段2=值2,...[where 条件];
没有条件会修改整张表
删除
Delete 表名 [where 条件];
没有条件会删除整张表
不能单独删除某一个字段(用update,设为none)
DQL
基本查询
1. 查询多个字段:select 字段1,字段2 from 表名;
SELECT *from 表名;
2. 设置别名:select 字段1as 别名,字段2as 别名 from 表名;
3. 去除重复记录:select distinct 字段列表 from 表名;
条件查询
Select 字段列表 from 表名 where 条件列表;
//逻辑、字符
聚合函数
将一列数据纵向计算
Select count()FROM 表名;
Select avg()FROM 表名;
Select max/min()FROM 表名;
Select sum()FROM 表名;
分组查询
Select 字段列表 from 表名 [where条件] group by分组字段名 [having分组后过滤条件];
Where 分组前过滤,不满足条件不参与分组,having对结果进行过滤;
Where不能使用聚合函数,having可以。
执行顺序
Where>聚合函数>having
排序查询
Select 字段列表 from 表名 order by 字段1 排序方式1,字段2 排序方式2;
Asc升序(默认)
Desc降序
分页查询
Select 字段列表 from 表名 limit 起始索引,查询记录数;
起始索引从0开始,起始索引=(查询页码-1)*每页显示记录数;
第一页数据索引可省略,简写为limit10
Select * from emp where age in (20,21,22,23);
Select * from emp where gender=’女’ and age in (20,21,22,23);
Select *from emp where gender=’男’ and age between20and40 and name like”---” ;
Select count(gender) , * from emp where age<60;
Select gender,count( *) from emp where age<60 group by gender;
Select count(*) from emp where age<35 oder by name asc,time desc;
Select name,age from emp where age<35 oder by name asc,time desc;
Select gender,count() from emp where gender=’男and age>20&&age<=40 ,oder by name asc,time desc;
Select * from emp where gender=’男and age>20&&age<=40 ,oder by name asc,time desc limit 5;
//编写顺序
//执行顺序
From where group by having select oderby limit
DCL
管理用户
查询用户
Use mysql
Select * from user;
创建用户
Create user ‘用户名’@’主机名’ identified by ‘密码’;
修改用户密码
Alter user ‘用户名’@’主机名’ identified with mysql_native_password by ‘新密码’;
删除用户
Drop user ‘用户名’@’主机名’;
权限控制
查询权限
Show grants for ‘用户名’@’主机名’;
授予权限
Grant 权限列表 on 数据库.表名 to ‘用户名’@’主机名’;
撤销权限
Revoke 权限列表 on 数据库.表名 from ‘用户名’@’主机名’;