mysql基础学习之增删改查

5 篇文章 0 订阅
3 篇文章 0 订阅

文章主要为mysql的一些基础的增删改查语法,现分享给大家,希望在加深自我记忆的同时也帮助到更多的人。
首先,我们创建一个数据库,数据库名称为test:
#create database test;
进到test库里面:
#use test;
在test库内创建test表:

create table test(
-> name char(20),
-> age int(10),
-> gender char(50)
-> );

以下所有语句示例均以上面创建的表test为例;
一、增(insert)
1、语法:insert into table (field1,field2,…) values (1,2,…);
例:insert into test (name,age)
values
(‘tom’,18);
此时test表内数据如上所示
2、若不指定字段名,则按照表内字段完全一致的顺序进行新增
语法:insert into test
values
(‘jack’,19,‘men’);
此时test表内数据如上所示
3、insert语句特殊用法
insert into test set name=‘haha’,age=20,gender=‘woman’;
此时test表内数据如上所示
二、删
1、删除表内所有数据共有以下两种方式来删除
delete
语法:delete from table;
truncte
语法:truncte table table;
注:后面的table为表名,前面的table为语法要求。

2、删除部分数据
delete
语法:delete from table where condition;
删除的数据为符合where条件的数据;

三、改
1、更改表内数据
update
语法:update table set field1=’’ field2=’’ … where condition;
注:不添加where条件默认更改表内所有;

四、查
关于sql查询可有多种逻辑关系匹配,比如逻辑与和逻辑互,具体理解到何种程度因人而异,此处只讲述了一些基本的查询语法;
下面为基础语法与完整语法
基础语法:select (Field list) from (data source);
完整语法:select distinct/all (Field list) [as field alias] from (data source) [where condition] [group by field] [having field] [order by field] [limit number];
注:
distinct/all 去重/不去重,默认为不去重
as alias 字段别名
order by 升序asc,降序为desc,默认为升序;
groud by 根据指定数据列对查询结果进行分组统计,最后得到一个分组汇总表,select语句中的列名必须为分组列或函数,列函数对group by字句定义的每个组各返回一个函数。
limit 自定义输出多少条记录;

1、基础之单表查询
1.1查看表内所有数据
语法:select * from table;
例:select * from test;
查看表内全部数据
1.2condition查询
语法:select * from table where condition;
例:select * from test where name = “tom”;
查询name字段是tom的数据
首先,我们先往test表内insert一条新纪录:
insert into test values (‘tom’,20,‘men’ );
查看当前test表内数据
select * from test;
当前表内全部数据
关于where的and和or
语法:select * from test where condition and/or condition;
例:select * from test where name = ‘tom’ and age=20;
and为条件同时匹配
例:select * from test where name = ‘tom’ or age=30;
or为任一条件匹配
1.3字段查询
语法:select field1,field2,… from table;
例:select name,age from test;
查询name和age字段
1.4字段别名
语法:select field as [field alias] from table ;
例:select name as ‘姓名’ from test ;
将name字段定义别名姓名
2.多表组合查询
在做以下实验前我们先创建一个新的表,并更改字段名称:
首先创建test1;
create table test1(select * from test);
更改test1表内字段age为age_1;
alter table test1 change age age_1 int ;
更改数据表test1内数据为与test不相同的数据
update test1 set name=‘hehe’ where name=‘haha’;
update test1 set age_1=22 where gender=‘men’;
查看test1表内数据
select * from test1 ;
图为test1表内最新数据
2.1外连接
外连接可分为左连接、右链接、完全外连接。
通过外连接就可以将不同的表连接在一起,变成一张大表,查询操作就变得简单而快捷了,大大避免了一张数据表内数据量过大查询慢的问题。
2.1.1左外连接
左外连接包括left join左表所在行,假设左表内某行在右表没有匹配。则结果中相应行右表的部分所有为空(NULL)。
left join 或 left outer join
语法:select * from table left join table on field=field ;
例:select * from test left join test1 on age=age_1;
图为使用左外连接查询的结果2.1.2右外连接
右外连接包括right join 右表的所在行,假设左表内某行在右表没有匹配,则结果中相应左表的部分所有为空(NULL)。
right join 或 right outer join
语法:select * from table right join table on field=field ;
例:select * from test right join test1 on age=age_1;
图为使用右外连接查询的结果
2.1.3完全外连接
完全外连接包括full join左右两个数据表中所有的行,假设右表中某行在左表中没有匹配,则结果中相应行右表的部分所有为空(NULL),假设左表中某行在右表中没有匹配,则结果中相应行左表的部分所有为空(NULL)。
full jpoin 或 full outer join
语法:select * from table full join table on field=field ;
例:select * from test full join test1 on age=age_1;
图为使用完全外连接查询的结果
2.2内连接
join 或 inner join
语法:select * from table,table where condition;
例:select * from test,test1 where age=age_1;
图为使用内连接查询的结果
2.3交叉连接
cross jopin
语法:select * from table cross join table;
例:select * from test cross join test1;
图为使用交叉连接查询的结果
2.4子查询
查询需要的条件是另外一张数据表查询的结果
2.4.1 IN关键字子查询
查勋前一张数据表字段内数据在第二张数据表字段内的数据
语法:select * from table where field IN(select field from table1);
例:select * from test where name in(select name from test1);
图为使用IN关键字进行子查询的结果2.4.2 EXISTS关键字子查询
exists内查询返回flase,若返回True是,外查询进行查询,否则外查询不进行查询
语法:select * from table where exists(select * from table1 where condition);
例:select * from test where exists(select * from test1 where name=‘tom’);
图为使用EXISTS关键字进行子查询的结果2.4.3 ANY关键字子查询
ANY关键字只要有一个满足,就进行该条件来执行外查询
语法:select field1,field2,… as ‘alias’ from table where field judge ANY(select field1,field2,… from table1 where condition);
例:select name,age as “年龄” from test where age>ANY(select age_1 from test1 where age>10);
图为使用ANY关键字进行子查询的结果2.4.4ALL关键字子查询
ALL关键字必须满足所有的内层查询语句返回的所有结果,才执行外查询
语法:select field1,field2,… as ‘alias’ from table where field judge ALL(select field1,field2,… from table1 where condition);
例:select name,age as’年龄’ from test where age<ALL(select age_1 from test1 where name=‘tom’);
![图为使用ALL关键字进行子查询的结果](https://img-blog.csdnimg.cn/20200515154611331.png](https://img-blog.csdnimg.cn/20200515155417559.png)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值