MySql(基础)

表名建议用 反引号 `` 包裹(尤其是表名包含特殊字符或保留字时),但如果表名是普通字符串(如 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条结果

相关推荐
Layux22 分钟前
flowable候选人及候选人组(Candidate Users 、Candidate Groups)的应用包含拾取、归还、交接
java·数据库
@Turbo@1 小时前
【QT】在QT6中读取文件的方法
开发语言·数据库·qt
ArabySide1 小时前
【EF Core】 EF Core 批量操作的进化之路——从传统变更跟踪到无跟踪更新
数据库·.net·efcore
废材是怎么养成的3 小时前
SpringBatch+Mysql+hanlp简版智能搜索
mysql
线条13 小时前
Hive SQL 中 BY 系列关键字全解析:从排序、分发到分组的核心用法
数据库·hive·sql
字节源流3 小时前
【MYSQL】索引篇(一)
数据库·mysql
n33(NK)4 小时前
MySQL中count(1)和count(*)的区别及细节
数据库·mysql
�FENG4 小时前
MYSQL备份与恢复
mysql·备份·xtrabackup
合作小小程序员小小店4 小时前
web安全开发,在线%机器学习异常流量检测系统%开发demo
人工智能·python·mysql·机器学习·sklearn
heart000_15 小时前
MySQL高级查询技巧:分组、聚合、子查询与分页【MySQL系列】
数据库·mysql