show databases;
create database [if not exists] `数据库名` [字符编码];
注意:
drop database [if exists] 数据库名;
show create database 数据库名;
修改数据库的字符编码:
alter database 数据库名 charset=字符编码;
use 数据库名;
show tables;
create table [if not exists] 表名(
字段名 数据类型 [null|not null] [auto_increment] [primary key] [comment]
字段名 数据类型 [default]
)engine=存储引擎
| 单词 | 含义 | |
|---|---|---|
null |
not null |
空|非空 |
default |
默认值 | |
auto_increament |
自增 | |
primary key |
主键 | |
comment |
备注 | |
engine |
引擎(innodb, myisam, memory) |
创建简单的表:
create table stu(
id int,
name varchar(30)
);
创建复杂的表:
# 如果带有中文
set names gbk;
# 创建复杂表
create table if not exists teacher(
id int auto_increment primary key comment '主键',
name varchar(20) not null comment '姓名',
phone varchar(20) comment '电话号码',
address varchar(100) default '地址不详' comment '地址'
)engine=innodb;
提示: 使用数据库名.表名可以给其他数据库创建表
show create table 表名 [\G];
desc[ribe] 表名;
drop table [if exists] 表1,表2,...;
alter table 表名
alter table 表名 add [column] 字段名 数据类型 [位置];
位置:
first:在第一位添加after:在 xx 字段后添加alter table 表名 drop [column] 字段名;
alter table 表名 change 字段名 新字段名 数据类型...
alter table 表名 modify 字段名 字段属性...
alter table 表名 engine=引擎名称;
alter table 表名 rename to 新表名;
create table 表名 select 字段 from 旧表
特点:不能复制父表的主键,只能复制父表的数据
create table 新表 like 旧表
特点:只能复制表结构,不能复制表数据
插入单条数据
insert into 表名 (字段1,字段2,...) values (值1,值2,...);
插入多条数据
insert into 表名 values (值1,值2,...),(值1,值2,...),...;
update 表名 set 字段=值 [where 条件]
delete from 表名 [where 条件]
truncate table 表名
**delete from 表**和**truncate table 表**区别:
delete from 表:遍历表记录,一条一条的删除;
truncate table 表:将原表摧毁,再创建一个同结构的新表,就清空表而言,这种方法效率高;
语法:select [选项] 列名 [form 表名] [where 条件] [order by 排序] [group by 分组] [having 条件][limit 限制]
可以通过as来取别名(as 可以省略)
from 后跟数据源,数据源可以放多个,返回笛卡尔积
dual 表是一个伪表。在有些特定情况下,没有具体的表的参与,但是为了保证 select 语句的完整又必须要一个表名,这时候就使用伪表。
MySQL 支持的运算符:>, <, >=, <=, =, !=, and, or, not
Where 后面跟的是条件语句,返回条件为真的记录
多个条件查询可以通过 in 语句 实现:
select * from stu where stuadress in ('北京','上海');
查找某个范围的记录:
select * from stu where stuage between 18 and 20;
判断是否为空:
select * from stu where ch is null;
sum():求和avg():求平均数max():求最大值min():求最小值count():记录值在学生表中查询姓张的学生:
select * from stu where stuname like '张_'
# 年龄升序,成绩降序
select *,(ch+math) as '总分' from stu order by stuage asc,(ch+math) desc;
将查询的结果分组,分组查询目的在于统计数据
# 按照性别分组,显示每组的平均年龄
select avg(stuage) as '平均年龄',stusex from stu group by stusex;
注意:
group_concat()函数将同一组的值连接起来显示多列分组:
# 按照性别和地区分组,显示每组的平均年龄
select stuaddress,stusex,avg(stuage) as '平均年龄'
where 和 having 的区别:
where 是对原始数据进行筛选,having 是对结果集进行筛选
语法:limit 起始位置,显示长度
注意:
查询语句中的选项有两个:
all:显示所有数据【默认】distinct:去除结果集中重复的数据# 语法一:
select 列名 from 表1 inner join 表2 on 表1.公共字段=表2.公共字段;
# 语法二:
select 列名 from 表1,表2 where 表1.公共字段=表2.公共字段;
注意:如果要显示公共字段,必须要指定表名
以左边的表为标准,如果右边的表没有对应的记录,就以 null 填充结果
select 列名 from 表1 left join 表2 on 表1.公共字段=表2.公共字段;
以右边的表为标准,如果左边的表没有对应的记录,就以 null 填充结果
select 列名 from 表1 right join 表2 on 表1.公共字段=表2.公共字段;
自动的判断连接条件,它是通过同名字段来判断
自然连接分为:
natural joinnatural left joinnatural right join自然连接结论:
using()也会对连接字段进行整理,整理规则与自然连接相同语法:
select 语句 where 条件 (select ... from 表)
=只适用于子查询返回一个值的情况用于子查询返回多个值的情况
# 如果有学生笔试成绩大于80就显示所有学生
select * from stuinfo where exists(select * from stumarks where writtenexam>=80);
select stuname,stusex,ch from stu where (stusex,ch) in (select stusex,max(ch) from stu group by stusex);
作用:将多个 select 语句结果集纵向联合起来
select 语句 union [选项] select 语句 union [选项] select 语句
DDL(Data Definition Language)数据库定义语言 CREATE、ALTER、DROP
DML(Data Manipulation Language)数据操纵语言 SELECT、UPDATE、INSERT、DELETE
DCL(Data Control Language)数据库控制语言,是用来设置或更改数据库用户或角色权限的语句
字符集:字符在保存和传输时对应的二进制编码合集。
set names 字符集编码