表名建议用 反引号 `` 包裹(尤其是表名包含特殊字符或保留字时),但如果表名是普通字符串(如 user),可以省略。
注释( COMMENT '姓名' )
数据库
1.查看数据库:show databases;
2.如果存在就删除数据库:DROP DATABASE IF EXISTS 库名;
3.创建数据库:create datatbases( if not exists ) 数据库名;
4.(强烈建议)创建数据库时指定编码集和排序规则:(Mysql8.0固定写法)
create databases(if not exists) 数据库名 character set utf8mb4 collate utf8mb4_0900_ai_ci;
5.使用该库:use 库名;
表
6.搜索该库表:show tables;
7.如果存在该表就删除:drop table if exists `表名`;
8.创建表:create table `表名`(
字段1 类型1,
字段2 类型2,
...
);
9.查看表结构:desc 表名;
常用数据类型
10.常用数据类型:
Int 整数
decimal(m,d):浮点数类型 (最多一共只有M位数,整数部位M-d位数,小数部位d位数,四舍五入)
varchar(size):字符串类型
timestamp:日期类型
增、删、改、查
asc升序(从小到大)
desc降序(从大到小)
Order by排序
And/or/in可以在条件中使用
In:where math in(88,99,100);条件数学成绩为88,99,100;
Between[a,b]表范围[a,b]:math between 80 and 100;
1.增
-- 将一个表数据复制到另一个表
insert into 表1 select name, qq_mail from 表2;
值与表顺序相同:insert into 表名 values (值...);
一一对应:Insert into 表名(列名,列名....) values(值,值...);
多行插入:insert into 表名((列名,列名....)values[(值,值...),(值,值...)....];
2.删
删数据:Delete form 表名 where 条件 order by 列名 asc|desc limit n:按列名从小到大|从大到小 删除符合条件的n个数据
删表:Delete form 表名;
3.改(更新)
Update 表名 set 列名=值... where 条件
Update 表名 set 更新的东西 where id in(通过主键 id 进行筛选)
( select id from ( select id from 表名order
by (chinese + math + english) ASC -- 按总分升序排序 limit 3 ) AS temp
);
4.查
查询整个表数据:Select * from 表名;
指定数据查询:select 列名id、name.... from 表名;
查询字段为表达:select id,math+10... from 表名;
别名:select id,math+english 总分 from 表名;
去重:select distinct math from 表名;
排序:select name form 表名 order by id (asc);查询姓名,按id(升序)显示排列;
模糊:select name from 表名 where like '孙%';孙权、孙欣欣..
select name from 表名 where like '孙_';孙权、孙浩....
NULL查询:select name,qq from 表明 where qq if not NULL;查询QQ为NULL的同学姓名
分页查询:select name from 表名 limit n; 从0开始筛序n条结果
select name from 表名 limit s,n; 从s开始筛序n条结果
select name from 表名 order by id asc limit n offset s; 按照id从小到大,从s开始筛序n条结果