SQL最常用的语句

SQL最常用的语句语法:一步步由浅到深,这里用的都是mysql做的。基础:连接数据库:mysql-h10.20.66.32-uroot-p1234561-h后面是mysqlServer所在地址,-u后面是用户名,-p后面是密码。查看数据库showdatabases;1使用数据库usetest;1查看表showtables;1查看表结构descwinton1建表createtablet1(…

大家好,又见面了,我是你们的朋友全栈君。

语法:

一步步由浅到深,这里用的都是mysql做的。

基础:

连接数据库:

mysql -h10.20.66.32 -uroot -p123456
  • 1

-h后面是mysqlServer所在地址,-u后面是用户名,-p后面是密码。

查看数据库

show databases;
  • 1

show databases

使用数据库

use test;
  • 1
  • 查看表
show tables;
  • 1

tables

查看表结构

desc winton
  • 1

desc

建表

create table t1( id int not null primary key, name char(20) not null );
  • 1
  • 2
  • 3
  • 4

语法 create table 表名称( 字段名 字段名类型 字段描述符,字段名 字段类型 字段描述符);

删除表

drop table test;
  • 1

语法:drop table 表名称;

修改表

添加字段

alter table t1 add(score int not null);
  • 1

语法:alter table 表明称 add(字段名 类型 描述符);

移除字段

alter table t1 drop column score;
  • 1

语法:alter table 表名 drop colunm 字段名,drop colunm 字段名;

变更字段

alter table t1 change name score int not null;
  • 1

语法:alter table 表名 change 旧字段名 新字段名 新字段描述符

插入

全字段插入

insert into winton values(001,'zww'),(002,'rs');
  • 1

语法:insert into 表名 values(字段1值,字段2值,……),(字段1值,字段2值,……);

个别字段插入

insert into winton(id) values(004);
  • 1

insert 
查看插如后的结果,如上图所示。 

语法:insert inton 表名(字段名) values(值一),(值二);

普通查询

单表全字段查询

select * from t1;
  • 1

语法:select * from 表名;

单表个别字段查询

select id from t1;
  • 1

语法:select 字段一,字段二 from 表名;

多表查询

select t1.id,t1.score,winton.name from t1,winton;
  • 1

多表查询 

语法:select 表一字段,表二字段,表三字段,…… from 表一,表二,表三,……

条件查询

单表条件查询

select * from t1 where socre>90;
  • 1

语法:select 字段1,字段2 from 表名 where 条件;

多表条件查询

select t1.id,t1.score,winton.name from t1,winton where t1.id=winton.id;
  • 1

winton 

语法:select 表一字段,表二字段 from 表一,表二 where 条件;

嵌套查询

select name from winton where id=(select id from t1 where score=90);
  • 1

这里写图片描述 

语法:select 字段一,字段二…… from 表名 where 条件(查询);

并查询

(select id from t1 )union(select id from winton);
  • 1

并查询

交查询

select id from t1 where id in (select id from winton);
  • 1

这里写图片描述

删除

delete from winton where id=4;
  • 1

语法:delete from 表名 where 条件;

更新

update t1 set score=69 where id=2;
  • 1

语法:update 表名 set 更改的字段名=值 where 条件;

常用函数

求和

select sum(score) from t1;
  • 1

注:sum(字段) 对字符串和时间无效

求平均值

select avg(score) from t1; 
  • 1

注:avg(字段)对字符串和时间无效

计数

select count(*) from t1;
  • 1

注:count(字段名)不包含NULL; 

这里写图片描述

求最大值

select max(name) from winton;
  • 1

注:max(colunm)返回字母序最大的,返回数值最大的

求最小值

select min(name) from winton;
  • 1

注:min(colunm)返回字母序最小值,返回数值最小值

常用的修饰符

distinct 字段中值唯一

select distinct name from winton;
  • 1

limit查询结果数限制

select * from winton limit 2;
  • 1

order by 排序

select * from winton order by name;
  • 1

注:默认是升序

desc 降序

slelect * from winton order by name desc;
  • 1

asc 升序

select * from winton order by name asc;
  • 1

group by 分组

select name from winton group by name;
  • 1

索引

创建普通索引

create index wintonIndex on winton (name);
  • 1

语法:create index 索引名称 on 表名 (字段一,字段二,……);

创建唯一索引

create unique index wintonIndex on winton (id);
  • 1

语法:create unique index 索引名 on 表名 (字段一,字段二,……); 
ps:unique index 要求列中数据唯一,不能出现重复。

移除索引

drop index wintonIndex on winton;
  • 1

语法: drop index 索引名 on 表名;

结尾

恩,基本能想起来的就值么多了,都是最基础,最常用的一些。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152099.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月22日 上午10:46
下一篇 2022年6月22日 上午10:46


相关推荐

  • 百度通用翻译api使用

    百度通用翻译api使用百度通用翻译 api 使用官方 api 文档 http api fanyi baidu com api trans product apidoc 第一步 注册百度账号 自行注册 第二步申请百度翻译 api 获得 appid 以及 securityKey 申请教程 https jingyan baidu com article 3f16e00305bb

    2026年3月17日
    2
  • shell循环生成连续日期

    shell循环生成连续日期1,给定长度,循环生成日期begin_day=-5end_day=0for((i=${begin_day};i<=${end_day};i++));do day=`date-d”${i}days”+”%Y%m%d”` echo${day}done输出为:2020082520200826202008272020082820200829202008302,给定开始日期和结束日期,循环生成日期。begin_day=”20200801″end_day=”2020

    2022年7月24日
    9
  • 数仓建模—元数据管理

    数仓建模—元数据管理元数据管理元数据在数据仓库中远比操作型环境中更加重要 因为数据仓库是一个不断迭代升级并且数据量也是远远大于操作型数据库的 所以我们需要元数据进行更好的监控和管理 元数据通常定义为 关于数据的数据 在数据仓库中是定义和描述 DW BI 系统的结构 操作和内容的所有信息 元数据贯穿了数据仓库的整个生命周期 使用元数据驱动数据仓库的开发 使数据仓库自动化 可视化 管理这些附加 MetaData 信息的目的 一方面是为了让用户能够更高效的挖掘和使用数据 另一方面是为了让平台管理人员能更加有效的做好系统的维护管理工

    2026年3月16日
    2
  • JavaScript中数组定义的几种方法,以及关于数组长度的讨论

    JavaScript中数组定义的几种方法,以及关于数组长度的讨论大家在使用 JavaScript 编写脚本语言的时候 尤其是习惯使用 Java 语言的朋友 定义了一个固定长度的数组 但是使用了一个超出定义数组的元素 程序并没有报 数组越界 错误 这到底是什么原因呢 今天我们来分享一下 1 数组定义的四种方法 vararr 1 newArray vararr 2 newArray 10 vararr 3 newArray 8

    2026年3月17日
    2
  • Linux安装Tomcat完整步骤

    Linux安装Tomcat完整步骤Linux 安装 Tomcat 完整步骤

    2026年2月24日
    0
  • vue-router 基本使用「建议收藏」

    vue-router 基本使用「建议收藏」vue-router基本使用  路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about按钮,页面中就要显示about的内容。Home按钮=>home内容,about按钮=>about内容,也可以说是一种映射.所以在页面上有两个部分,一个是点击部分,一个是点击之后,显示内容的部分。  点击之后,…

    2022年7月11日
    20

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号