SQL基础语句汇总[通俗易懂]

SQL基础语句汇总[通俗易懂]引言语法基础连接数据库查看数据库使用数据库查看表查看表结构建表修改表添加字段移除字段变更字段插入全字段插入个别字段插入普通查询单表全字段查询单表个别字段查询多表查询条件查询单表条件查询多表条件查询嵌套查询并查询交查询删除更新常用函数求和求平均值计数求最大值求最小值常用的修饰符distinct字段中值唯一limit查询结果数限制

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

引言

是时候复习一波SQL语句的语法了,无需太深,但总得会用啊。

语法

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

基础

连接数据库

mysql -h10.20.66.32 -uroot -p123456

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

查看数据库

show databases;

show databases

使用数据库

use test;

查看表

show tables;

tables

查看表结构

desc winton

desc

建表

create table t1(
	id int not null primary key, 
	name char(20) not null
	);

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

删除表

drop table test;

语法:drop table 表名称;

修改表

添加字段

alter table t1 add(score int not null);

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

移除字段

alter table t1 drop column score;

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

变更字段

alter table t1 change name score int not null;

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

插入

全字段插入

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

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

个别字段插入

insert into winton(id) values(004);

insert
查看插如后的结果,如上图所示。
语法:insert inton 表名(字段名) values(值一),(值二);

普通查询

单表全字段查询

select * from t1;

语法:select * from 表名;

单表个别字段查询

select id from t1;

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

多表查询

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

多表查询
语法:select 表一字段,表二字段,表三字段,…… from 表一,表二,表三,……;

条件查询

单表条件查询

select * from t1 where socre>90;

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

多表条件查询

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

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

嵌套查询

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

这里写图片描述
语法:select 字段一,字段二…… from 表名 where 条件(查询);

并查询

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

并查询
##交查询

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

这里写图片描述

删除

delete from winton where id=4;

语法:delete from 表名 where 条件;

更新

update t1 set score=69 where id=2;

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

常用函数

求和

select sum(score) from t1;

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

求平均值

select avg(score) from t1; 

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

计数

select count(*) from t1;

注:count(字段名)不包含NULL;
这里写图片描述

求最大值

select max(name) from winton;

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

求最小值

select min(name) from winton;

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

常用的修饰符

distinct 字段中值唯一

select distinct name from winton;

limit查询结果数限制

select * from winton limit 2;

order by 排序

select * from winton order by name;

注:默认是升序

desc 降序

slelect * from winton order by name desc;

asc 升序

select * from winton order by name asc;

group by 分组

select name from winton group by name;

索引

创建普通索引

create index wintonIndex on winton (name);

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

创建唯一索引

create unique index wintonIndex on winton (id);

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

移除索引

drop index wintonIndex on winton;

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

结尾

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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 完整javaEE学生信息管理系统[通俗易懂]

    完整javaEE学生信息管理系统[通俗易懂]基于javaweb的ssm学校教务管理系统(管理员,教师,学生)文章结构一、开发框架及业务方向1.开发环境2.开发框架3.整体业务二、项目结构及页面展示1.项目整体结构2.用户页面3.管理员页面***需要源码的加企鹅:671033846;备注CSDN即可******文章结构一、开发框架及业务方向1.开发环境操作系统不限:java特性,一套代码,导出运行jdk版本不限:推荐jdk1.8tomcat版本不限:推荐Tomcat8.0数据库mysql:版本不限,推荐mysql8.0以下开发工具:e

    2022年10月16日
    1
  • Matlab求分段函数的积分[通俗易懂]

    Matlab求分段函数的积分[通俗易懂](一)前言本文介绍一个使用Matlab进行求分段函数积分值的方法。首先介绍如何使用int()对连续函数进行积分的求解,然后介绍一个对分段函数进行求积分的例子。(二)使用Matlab求定积分Matlab中求积分的函数为int(),调用形式为int(func,’x’,a,b),其中func为被积函数,x为积分变量,[a,b]为被积区间。如int(x^2,’x’,1,2…

    2022年6月9日
    70
  • 您也使用托管C++吗?

    您也使用托管C++吗?

    2021年8月9日
    46
  • 288÷4×7解决什么问题_java has not been found

    288÷4×7解决什么问题_java has not been found前言:今天连接zookeepter的时候出现问题(上午连接的时候还没问题的,下午就出现了问题,很是无奈)报错信息如下:java.net.ConnectException:Connectiontimedout:nofurtherinformation atsun.nio.ch.SocketChannelImpl.checkConnect(NativeMethod) ats…

    2022年9月18日
    0
  • xshell的安装和使用_Xshell命令

    xshell的安装和使用_Xshell命令Xshell安装Xshell安装完后提示更新到最新版本

    2022年9月8日
    0
  • vue删除对象的某个属性(js怎么删除对象中的某个元素)

    微信小程序开发交流qq群173683895承接微信小程序开发。扫码加微信。实现代码:vardata={a:1,b:2,c:3}for(varitemindata){if(item==’b’){deletedata[item];}}console.log(‘data:…

    2022年4月11日
    46

发表回复

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

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