mysql基础语法
前言
数据库是用于存储和管理数据库的仓库持久化的存储数据,数据库就是一个文件系统
便于存储和管理数据
数据库语句分类
数据库语句是指操作数据库的命令语句分为:
数据定义语言(DDL)
数据操作语言(DML)
数据查询语言(DQL)
数据控制语言(DCL)
数据类型
int 整数类型
bigint 大整数
double 小数
decimal 精确小数
date 日期 只包含年月日 yyyy-MM-dd
datetime 日期 包含年月日时分秒 yyyy-MM-dd HH:mm:ss
timestamp 时间戳 包含年月日时分秒 (赋值时为null,会默认使用系统时间赋值)
varchar (20) 可变字符串
char(1) 定长字符串,值为几字符串长度必须为几
一、数据库定义语言DDL(Data Definition Language)
数据库定义语言的结构,包括创建、修改和删除数据库、表、视图、索引等。
常见的DDL语句有create、alter和drop。
1、creat
1.创建数据库 库名
create database dan;
判断数据库是否存在再创建
create database if not exists 数据库名;
创建数据库,指定字符集
create database 数据库名 character set 字符集名;
2.查询全部数据库,名为dan的数据库创建成功
show databases;
查询数据库的字符集
show create database 数据库名;
查询数据库中的表
show tables;
3.切换数据库
use dan;
4.查看当前使用的数据库
select database();
5.删除数据库
drop database dan;
判断数据库是否存在再删除
drop database if exists dan;
2、alter
为表中添加字段
1.创建数据表 ——其中StuId为主键,自增
create table student(
StuId int(11) primary key auto_increment,
StuName varchar(20),
StuAge int,
StuSex varchar(20));
2.查看表结构
desc student;
3.添加列
alter table student add score int not null;
4.修改列的数据类型
alter table student modify score double(3.1);3表示数据的长度,1表示小数点后保留几位,例59.5
5.修改列名
alter table student change score grade int not null;
6。修改表名
alter table student to stu;
7.修改表的字符集
alter table stu character set UFT8;
3、drop
8.删除列
alter table stu drop grade;
二、数据库操作语言DML(Data Manipulation Language)
insert:向表中插入数据
update:修改表中的数据
delete:删除表中的数据
1、insert
1.为表中添加数据
insert into stu(stuid,stuname,stuage,stusex) value (1,"王路飞",18,"男");
为表中指定字段添加数据,其stuid为自增,值可以为null
insert into stu(stuid,stuname) value (null,"刘索隆");
2.
为所有字段添加数据
insert into stu values(null,"山治",18,"男");
2、update
3.修改数据
全部人的stuage为20
update stu set stuage=20;
根据stuid修改数据
update stu set stuage=20 where stuid=1;
3、delete
删除全部表记录
delete from stu;
删除指定的表记录
delete from stu where stuid=3;
三、数据库查询语言DQL(Data Query Language)
基本查询
1.select用于查询表中的数据(* 代表查询所有)
select * from stu;
2.查询指定字段
select stuid from stu;
3.DISTINCT 关键字的主要作用就是对数据表中一个或多个字段重复的数据进行过滤
select distinct stuname from stu;
条件查询(where)
4.where用于筛选符合条件的数据
select stuname from stu where stuid=1;
5.like 模糊查询
_表示单个字符
%表示多个字符
select * from stu where like '王_'; 表示两个字
select * from stu where like '王__'; 表示三个字
select * from stu where like '王%'; 表示姓王的
select * from stu where like '%王%'; 表示名字中带王的
6.between...and范围查询
select *from stu where stuage between 18 and 20;
7.and表示查询条件同时满足
or表示查询条件满足其中一个即可
select *from stu where stuage>18 and stusex='男';
select *from stu where stuage>18 or stusex='男';
8.in查询括号中的值
not in查询非括号中得知
select *from stu where stuid in(1,2,3);
select *from stu where stuid not in(1,2,3);
聚合函数
9.聚合函数
count 统计数量
max 最大值
min 最小值
sum 求和
avg 平均值
select count (stuid) from stu; 查询有多少个stuid
select sum (字段) from 表;
select max(字段) from 表;
select min(字段) from 表;
select avg(字段) from 表;
分组查询(ground by)
10.分组查询gruop by having(where后面可以跟gruop by having,但having后面不能跟where)
where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤。
select stusex from group by stusex;
selete * from group by stusex having stusex='男';
排序查询(order by)
11.排序查询(order by)
asc升序查询
desc降序查询
select * from stu order by stuage asc;
select * from stu order by stuage desc;
分页查询(limit)
12.分页查询(limit)
表记录下标从0开始
select * from stu limit 2,10;
从下标2开始显示10条数据
多表查询
CREATE TABLE emp(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20) UNIQUE,
sex char(1),
join_date DATE,
salary DOUBLE,
dept_id int
);
添加数据
INSERT INTO emp VALUES
(1,'张三','男','2022-1-1',3000,1),
(NULL,'李四','男','2020-12-13',6900,2),
(NULL,'王五','女','2012-11-14',8000,3),
(NULL,'赵六','男','2020-10-13',5700,1),
(NULL,'田七','女','2019-4-3',4600,2),
(NULL,'孙八','男','2018-5-19',3000,NULL);
CREATE TABLE dept(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20),
location VARCHAR(20),
FOREIGN KEY(dept_id) REFERENCES dept(id)
);
添加数据
INSERT INTO dept VALUES
(1,'销售部','上海'),
(NULL,'财务部','北京'),
(NULL,'人事部','天津');
-
内连接查询
隐式内连接:使用where关键字消除冗余数据(常用) 显示内连接:inner join关键字
如图我们将两个表的共有部分作为条件,满足条件才被获取,emp表中孙八没有部门他不会被获取到
1、查询员工及员工部门所在地(显示、隐式)
select * from emp e,dept d where e.dept_id=d.id;
select * from emp e inner join dept d on e.dept_id=d.id;
-
外连接查询
左外连接:查询左表所有的与右表共有的 右外连接:查询右表所有的与左表共有的
可以查到孙八
select * from emp e left join dept d on e.dept_id=d.id;
孙八没有部门,所以查不到孙八,将left改为right即可
select * from dept d left join emp e on e.dept_id=d.id;
- 子查询:查询中嵌套查询
1、查询工资最高的员工信息
SELECT * FROM emp WHERE
salary=(SELECT MAX(salary) FROM emp);
2、查询财务部和销售部所有的员工信息
SELECT * FROM emp WHERE
dept_id IN (SELECT id FROM dept WHERE NAME='财务部' OR NAME='销售部');
3、查询2020-1-1号之后入职的员工信息和部门信息
SELECT s.name,d.location FROM
(SELECT * FROM emp WHERE join_date<'2022-1-1') s LEFT JOIN dept d
ON s.dept_id=d.id;