mysql基础sql语句_SQL基础语句汇总

引言

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

语法

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

基础

连接数据库

mysql -h10.20.66.32 -uroot -p1234561

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

查看数据库

show databases;1

e6af6ab072c5b3c5445b142406944a6f.png

使用数据库

use test;1

查看表

show tables;1

12709a721053525bac207025ce3afdc6.png

查看表结构

desc winton1

d3c4b115f6dfa3074847f44831ce5a78.png

建表

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

1676f3061ef91a98f2bce0e0138fdca6.png

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

语法: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

9e10c25636ab87c3ae4b7048c7d665b9.png

语法: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

af2496ba7b18317a099134131f58af97.png

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

嵌套查询

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

db7bacee4550019dcb6b62a9db516ab2.png

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

并查询

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

3675a13c78c3f77f3912e2339fea4306.png

交查询

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

3f9ca6eadefb70a61d55e7a1627e0644.png

删除

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;

383ca86b49d80e1f00ffa5583fdea72c.png

求最大值

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 表名;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值