MySQL的开发必会的sql语句

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

创建数据库

create database db1;

删除数据库

drop database db1;

创建数据表

create table tb1用户表(
                    id int not null auto_increment primary key,
                    name char(10),
                    department_id int,
                    p_id int,
                )engine=innodb default charset=utf8;

主键(primary key)一个表只能有一个主键,主键可以由一列或者多列组成

外键的创建

CREATE TABLE t5 (
                      nid int(11) NOT NULL AUTO_INCREMENT,
                      pid int(11) not NULL,
                      num int(11),
                      primary key(nid,pid) --这里就是把两列设置成了主键
                    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

                    create table t6(
                        id int auto_increment primary key,
                        name char(10),
                        id1 int,
                        id2 int,
                        CONSTRAINT fk_t5_t6 foreign key (id1,id2) REFERENCES t1(nid,pid) --这里是设置外键
                    )engine=innodb default charset=utf8;

数据行的操作

数据的插入

insert into tb1(name,age) values('ax',8);
insert into tb12(name,age) select name,age from tb11;

表中的数据的删除

delete from t1;
truncate table t1;
drop table t1
delete from tb1 where id > 10
delete from tb12 where id >=2 or name='alex'

数据的更新

update tb1 set name='root' where id > 10

数据的查询

select * from tb;
select id,name from tb;

表结构的查看

show create table t1;
desc t1;

其他

  select * from tb12 where id != 1
    select * from tb12 where id in (1,5,12);
    select * from tb12 where id not in (1,5,12);
    select * from tb12 where id in (select id from tb11)
    select * from tb12 where id between 5 and 12;

通配符

select * from tb12 where name like "a%"
select * from tb12 where name like "a_"

分页

select * from tb12 limit 10;                    
select * from tb12 limit 0,10;
select * from tb12 limit 10,10;
select * from tb12 limit 20,10;                 
select * from tb12 limit 10 offset 20;

# page = input('请输入要查看的页码')
# page = int(page)
# (page-1) * 10
# select * from tb12 limit 0,10; 1 
# select * from tb12 limit 10,10;2

排序

select * from tb12 order by id desc; 大到小
select * from tb12 order by id asc;  小到大
select * from tb12 order by age desc,id desc;

取后10条数据
select * from tb12 order by id desc limit 10;

分组

select count(id),max(id),part_id from userinfo5 group by part_id;
聚合函数有下面几个:                  
                    count
                    max
                    min
                    sum
                    avg

**** 如果对于聚合函数结果进行二次筛选时?必须使用having ****
select count(id),part_id from userinfo5 group by part_id having count(id) > 1;

select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;

自增值设置

表自增值的设置

alter table t1 auto_increment=20;
-- 这个就表示从开始20开始算,用上面的show create table t1\G 就可以看到当前的值是多少。

基于会话级别

-- 查看当前的会话值
show session variables like 'auto_incre%'
-- 设置会话步长
set session auto_increment_increment=2;
-- 设置起始值
set session auto_increment_offset=10;

基于全局设置

-- 查看全局的设置值
show global variables like 'auto_inc%';
-- 设置全局步长值
set global auto_increment_increment=3;
-- 设置起始值
set global auto_increment_offset=11;

sql server 是在创建表的时候就可以自己设置,灵活度很高
REATE TABLE t5 (
nid int(11) NOT NULL AUTO_INCREMENT,
pid int(11) NOT NULL,
num int(11) DEFAULT NULL,
PRIMARY KEY (nid,pid)
) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=2 DEFAULT CHARSET=utf8

          CREATE TABLE `t6` (

nid int(11) NOT NULL AUTO_INCREMENT,
pid int(11) NOT NULL,
num int(11) DEFAULT NULL,
PRIMARY KEY (nid,pid)
) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=20 DEFAULT CHARSET=utf8

唯一索引

create table t1(
    id int,
    num int,
    xx int,
    unique qu1 (num ,xx) -- 意思就是这两列在一行上面数据不能相同,例如都是1,1,就不行
);

唯一索引:约束不能重复(可以为空)
主键索引:约束不能重复(不可以为空)
他们的特点都是加速查询

外键一对一

create table userinfo1(
                    id int auto_increment primary key,
                    name char(10),
                    gender char(10),
                    email varchar(64)
                )engine=innodb default charset=utf8;

                create table admin(
                    id int not null auto_increment primary key,
                    username varchar(64) not null,
                    password VARCHAR(64) not null,
                    user_id int not null,
                    unique uq_u1 (user_id),
                    CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id)
                )engine=innodb default charset=utf8;

外键多对多

示例1:
                用户表
                相亲表

            示例2:
                用户表
                主机表
                用户主机关系表
            ===》多对多

                create table userinfo2(
                    id int auto_increment primary key,
                    name char(10),
                    gender char(10),
                    email varchar(64)
                )engine=innodb default charset=utf8;

                create table host(
                    id int auto_increment primary key,
                    hostname char(64)
                )engine=innodb default charset=utf8;

                create table user2host(
                    id int auto_increment primary key,
                    userid int not null,
                    hostid int not null,
                    unique uq_user_host (userid,hostid),
                    CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id),
                    CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id)
                )engine=innodb default charset=utf8;

连表操作

select * from userinfo5,department5

                    select * from userinfo5,department5 where userinfo5.part_id = department5.id

                    select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
                    select * from department5 left join userinfo5 on userinfo5.part_id = department5.id
                    # userinfo5左边全部显示

                    # select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
                    # department5右边全部显示

                    select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
                    将出现null时一行隐藏

select * from 
                        department5 
left join userinfo5 on userinfo5.part_id = department5.id
left join userinfo6 on userinfo5.part_id = department5.id

select 
                        score.sid,
                        student.sid 
from 
                    score

left join student on score.student_id = student.sid

left join course on score.course_id = course.cid

left join class on student.class_id = class.cid

left join teacher on course.teacher_id=teacher.ti           

select count(id) from userinfo5;



本文转自 kesungang 51CTO博客,原文链接:http://blog.51cto.com/sgk2011/2051641,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
22天前
|
SQL 关系型数据库 MySQL
mysql一条sql查询出多个统计结果
mysql一条sql查询出多个统计结果
14 0
|
1天前
|
SQL 存储 关系型数据库
数据库开发之mysql前言以及详细解析
数据库开发之mysql前言以及详细解析
9 0
|
5天前
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
|
8天前
|
PHP
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
web简易开发——通过php与HTML+css+mysql实现用户的登录,注册
|
8天前
|
SQL 关系型数据库 数据库
【后端面经】【数据库与MySQL】SQL优化:如何发现SQL中的问题?
【4月更文挑战第12天】数据库优化涉及硬件升级、操作系统调整、服务器/引擎优化和SQL优化。SQL优化目标是减少磁盘IO和内存/CPU消耗。`EXPLAIN`命令用于检查SQL执行计划,关注`type`、`possible_keys`、`key`、`rows`和`filtered`字段。设计索引时考虑外键、频繁出现在`where`、`order by`和关联查询中的列,以及区分度高的列。大数据表改结构需谨慎,可能需要停机、低峰期变更或新建表。面试中应准备SQL优化案例,如覆盖索引、优化`order by`、`count`和索引提示。优化分页查询时避免大偏移量,可利用上一批的最大ID进行限制。
33 3
|
15天前
|
SQL 存储 关系型数据库
【MySQL实战笔记】02.一条SQL更新语句是如何执行的-2
【4月更文挑战第5天】两阶段提交是为确保`redo log`和`binlog`逻辑一致,避免数据不一致。若先写`redo log`, crash后数据可能丢失,导致恢复后状态错误;若先写`binlog`,crash则可能导致重复事务,影响数据库一致性。一天一备相较于一周一备,能缩短“最长恢复时间”,但需权衡额外的存储成本。
16 1
|
22天前
|
SQL 关系型数据库 MySQL
【MySQL】慢SQL分析流程
【4月更文挑战第1天】【MySQL】慢SQL分析流程
|
25天前
|
SQL 关系型数据库 MySQL
【MySQL技术之旅】(7)总结和盘点优化方案系列之常用SQL的优化
【MySQL技术之旅】(7)总结和盘点优化方案系列之常用SQL的优化
39 1
|
15天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)
|
1月前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
84 1