Article directory
- MySQL data type
- database operation
- DDL statement (table operation)
- DML statements (data manipulation)
- DQL statement (data query statement)
- DCL statement (data control)
MySQL data type
value
type | size | scope | use |
---|---|---|---|
tinyint | 1 byte | signed (-128, 127) unsigned (0, 255) | small integer |
smallint | 2 bytes | signed (-32768, 32767) unsigned (0, 65535) | big integer |
mediumint | 3 bytes | signed (-8388608, 8388607) unsigned (0, 16777215) | big integer |
int or integer | 4 bytes | signed ((-2 147 483 648, 2 147 483 647) unsigned (0, 4 294 967 295) | small integer |
bigint | 8 bytes | Signed (-9,223,372,036,854,775,808, 9 223 372 036 854 775 807) Unsigned (0, 18 446 744 073 709 551 615) | small integer |
float | 4 bytes | Signed (-3.402 823 466 E+38, -1.175 494 351 E-38), 0, (1.175 494 351 E-38, 3.402 823 466 351 E+38) unsigned 0, (1.175 494 351 E-38, 3.402 823 466 E+38) | small integer |
double | 8 bytes | Signed (-1.797 693 134 862 315 7 E+308, -2.225 073 858 507 201 4 E-308), 0, (2.225 073 858 507 201 4 E-308, 1.797 693 134 862 315 7 E+ 308) nothing Sign 0, (2.225 073 858 507 201 4 E-308, 1.797 693 134 862 315 7 E+308) | small integer |
date
type | size (bytes) | scope | Format | use |
---|---|---|---|---|
DATE | 3 | 1000-01-01/9999-12-31 | YYYY-MM-DD | date value |
TIME | 3 | ‘-838:59:59’/‘838:59:59’ | HH:MM:SS | time value or duration |
YEAR | 1 | 1901/2155 | YYYY | year value |
DATETIME | 8 | '1000-01-01 00:00:00' to '9999-12-31 23:59:59' | YYYY-MM-DD hh:mm:ss | Mixed date and time values |
string
The n in the square brackets of char(n) and varchar(n) represents the number of characters, not the number of bytes. For example, CHAR(30) can store 30 characters.
type | size | use |
---|---|---|
CHAR | 0-255 | bytes |
VARCHAR | 0-65535 bytes | variable length string |
TINYBLOB | 0-255 bytes | Binary string of no more than 255 characters |
TINYTEXT | 0-255 bytes | short text string |
BLOB | 0-65 535 bytes | Long text data in binary form |
TEXT | 0-65 535 bytes | long text data |
MEDIUMBLOB | 0-16 777 215 bytes | Medium-length text data in binary form |
MEDIUMTEXT | 0-16 777 215 bytes | medium length text data |
LUNG BLOB | 0-4 294 967 295 bytes | Very large text data in binary form |
LONGTEXT | 0-4 294 967 295 bytes | extremely large text data |
database operation
create database
create + database name
create database;
create database if not exists testdb;
//创建数据库并指定编码形式
create database if not exists testdb default charset utf8mb4;
##Display all databases
show + database name
show databases
use database
use+database name
use student
delete database
drop database if exists student
DDL statement (table operation)
Data definition language, mainly to define/change table structure, data type, link between tables, etc.
display table
show + table name
show tables
create table
create table 表名
(
列名1 列的类型(长度)<约束> comment ‘列名注释’,
列名2 列的类型(长度)<约束>
列名3 列的类型(长度)<约束>
列名4 列的类型(长度)<约束>
…
)
// 创建表
create table tb_user(
id int comment '用户id',
name varchar(50) comment '用户名称',
age int,
sex varchar(1)
);
查看表结构
desc ta_user;
查看表创建语句
show create table tb_user;
添加字段
alert table tableName add username varchar(18);
删除字段
alert table tableName drop username;
修改字段数据类型(可加约束)
alert table tableName modify username varchar(16);
alert table tableName modify username varchar(16) not null;
修改字段名
alert table tableName change useraName newName varchar(16);
修改表名
alert table tableName rename to newTableName;
删除表
drop table if exists tableName;//删除整个表结构与所有数据
删除表数据
truncate table tableName; // 清除所有数据,保留表结构
DML语句(数据操纵)
增加数据
全值插入 选择插入 批量插入
语法:insert into 表名称(字段1,字段2,字段3,…)values(值1,值2,值3,…);
//选择插入(未选择的字段必须是可为空)
intsert into tb_temp(tempID,empNUM,empName,empAge)values(100,'NUM10010','珠珠',45);
//全值插入(表中每个字段都有对应数据)
intsert into tb_temp(tempID,empNUM,empName,empAge,empPhone,empBirdate)values(100,'NUM10010','珠珠',45,'13895071111','2000-2-12');
//批量插入
intsert into tb_temp(tempID,empNUM,empName,empAge,empPhone,empBirdate)values(101,'NUM10011','珠珠',45,'13895071111','2000-2-12'),(102,'NUM10012','美俄米',20,'13895071111','2020-8-9')(103,'NUM10013','嘟嘟',10,'13895071111','2030-12-1');
修改数据
语法:update 表名 set 字段1=字段值1,字段2=字段值2,字段3=字段值3 where 修改条件
update tb_temp set empAge=23,empPhone='13888871111'where tempID=102;
删除表中数据
语法delete from 表名 where 删除条件
delete from tb_temp where tempID=102
//不加条件则删除数据表的所有数据
delete from tb_temp
DQL语句(数据查询语句)
语法 SELECT 列名 FROM 表名 WHERE 限定条件;
查询表所有数据查询
select * from tb_user;
查询选择字段
select name,age from tb_user;
查询的时候为字段重新设置别名
select name as '姓名' from tb_user;
去重查询
select distinct name from tb_user;
条件查询
比较运算符
运算符 | 作用 |
---|---|
= | 等于 |
<=> | 安全的等于 |
<> 或者 != | 不等于 |
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
IS NULL 或者 ISNULL | 判断一个值是否为空 |
IS NOT NULL | 判断一个值是否不为空 |
BETWEEN AND | 判断一个值是否落在两个值之间 |
IN(…) | 在in之后的列表中的值,多选一 |
LIKE 占位符 | 模糊匹配(匹配单个字符,%匹配任意个字符) |
逻辑运算符
运算符 | 作用 |
---|---|
NOT 或者 ! | 逻辑非 |
AND 或者 && | 逻辑与 |
XOR | 逻辑异或 |
OR 和 || 逻辑或
逻辑与查询
//and同时满足
select * from tb_user where name='昭明' and age=18;
逻辑非查询
//or满足一个就可以
select * from tb_user where name='昭明' or age=18;
比较运算查询
select * from tb_user where age!=18;
select * from tb_user where name!='昭明';
select * from tb_user where age<>18;
select * from tb_user where name <>'昭明';
范围查询
select * from tb_user where age>=20 and age<=30;
select * from tb_user where age between 20 and 30;
模糊查询
//查询表中所有姓张的人员的信息
//%匹配所有长度的字符串
//查询姓张的数据
select * from tb_user where name like '张%';
//查询姓名中包含'张'字的姓名
select * from tb_user where name like '%张%';
//查询姓张,只有两个字的姓名人员信息
//'_'匹配单个位置
select * from tb_user where name like '张_';
//匹配名字中间有'张'的名字
select * from tb_user where name like '_张_';
聚合查询
函数 | 作用 |
---|---|
count | 统计数量 |
max | 最大值 |
min | 最小值 |
avg | 平均值 |
sum | 求和 |
统计
select COUNT(*) from tb_user;
select COUNT(nickname) from tb_user;
最大值 最小值 平均值 求和
select max(age) from tb_user;
select sum(age) from tb_user where gender = '女';
select max(age)最大年龄,min(age)最小年龄,avg(age)平均年龄,sum(age)年龄总和 from tb_user ;
分组查询
限制查询的条数
select * from tb_user limit 3;
分页查询
limit offset=(当前页-1)*pagesize(pagesize是限制查询条数)
select* from tb_user limit 0,3;//第一页数据
select* from tb_user limit 3,3;//第二页数据
select* from tb_user limit 6,3;//第三页数据
条件查询
// 按照年龄升序查询、年龄相等的时候按照name降序排序。
select * from tb_user order by age asc , name desc ;
分组查询
select 能够跟的内容必须是聚合函数或者group by 后面指定字段
where与having区别
执行时机不同:where是分组之前进行过滤,不满足where条件不参与分组;而having是分组之后对结果进行过滤。
判断条件不同:where不能对聚合函数进行过滤,二having可以;
select gender,max(age)最大年龄,min(age)最小年龄,avg(age)平均年龄,sum(age)年龄总和 from tb_user group by gender;
// 根据男女分组、查询nickName = 11 且分组数量大于2的。
select gender, count(*) as number from tb_user where nickname = '11' group by gender having number > 2;
执行顺序:
from>where>group by>having>select>order by>limit
DCL语句(数据控制)
用于管理数据库用户、控制数据库的访问权限
创建用户
create user 'yanggerry'@'localhost' identified by '12456';
修改用户密码
alter user 'yanggerry'@'localhost' identified with mysql_native_password by '1234';
删除用户
drop user 'yanggerry'@'localhost';
显示用户权限
show grants for 'yanggerry'@'localhost';
给用户 emp 所有的表的所有权限
grant all on emp.* to 'yanggerry'@'localhost';
移除用户 emp 所有表的所有权限
revoke all on emp.* from 'yanggerry'@'localhost';