SQL数据库操作基础
2022/2/13 19:44:59
本文主要是介绍SQL数据库操作基础,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、MySQL基础操作
1.连接数据库
在MySQL的bin目录下cmd
输入命令
mysql -uroot -p
出现Enter password提示,输入密码即可登录
2.显示系统中所有数据库的名称
show databases;
注意:绝大部分SQL命令都以分号作为结束符!!!
2.新建数据库boke
create database boke;
4.使用数据库boke
use boke;
5.在数据库boke中创建表artical
create table artical(id int(8),name varchar(20),score int(5));
6.在表boke中增加数据
insert into artical(id,name,score) values(3,"aa",10); insert into artical(id,name,score) values(6,"cc",56); insert into artical(id,name,score) values(1,"ee",63); insert into artical(id,name,score) values(4,"jj",98); insert into artical(id,name,score) values(2,"vv",75); insert into artical(id,name,score) values(5,"qq",69);
注意:如果数据是字符型(varchar),必须使用单引号或双引号包裹!!!
7.查询表中数据
查询全部
select * from artical;
查询id=5的score
select score from artical where id=5;
8.删除一条数据
删除id=6的数据
delete from artical where id=6;
9.修改一条数据
修改id=5的数据,将其score设置为30
update artical set score=30 where id=5;
二、MySQL进阶操作
1.order by 的用法
(1)将result表中的数据按照score从低到高进行排序:
select * from artical order by score asc;
其中,asc表示升序(递增);如果从高到底(降序)进行排列,则可以将asc换成desc;如果不加此参数,默认情况下按升序方式排列。
(2)分别尝试以下命令:
select id,name,score from artical order by 1;
正常显示以id升序排列的结果
select id, name,score from artical order by 2;
正常显示以name升序排列的结果
select id, name,score from artical order by 3;
正常显示以score升序排列的结果
select id, name,score from artical order by 4;
注意:order by后面的数字(M)必须小于或等于n(数据库查询的字段数),才能正常显示。如果M>n,数据库就会报错。可以利用这个特点判断数据库查询的字段数!!!
2.limit的用法
基本格式为: limit M,N //表示从第M+1条数据开始,顺序往下查询N条数据 limit M //表示查询前M条数据
查询表中3,4条数据
select* from artical limit 2,2;
查询表中前3条数据
select * from artical limit 3;
3.union select的用法
select * from artical union select 1,2,3;
此语句的查询结果,即是select * from artical和 select 1,2,3查询结果的拼接
尝试执行以下2条语句:
select id,name from artical union select 1,2;
正常显示!
select id,name from artical union select 1,2,3;
报错!
注意:后半句union select查询的字段数(m)必须与前半句select查询的字段数(n)相等,数据库才能正常显示结果。与order by相似,可以利用这个特点判断数据库查询的字段数!!!
select id,name from artical where id=1 and 1=2 union select name,score from artical;
从以上结果可以总结,在已知字段名的情况下,攻击者只要将该字段置于任何能够显示的位置,就可以暴露该字段的值。
4.union select结合information_schema数据库
MySQL5.0以上版本存在一个叫information_schema的数据库,它存储着数据库的所有信息,其中保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型和访问权限等。而5.0以下没有。可以把 information_schema数据库看作 MySQL的目录。
show databases; select schema_name from information_schema.schemata;
两条语句执行结果相同!
use boke; show tables;
select table_name from information_schema.tables where table_schema='boke';
两组语句执行结果相同!
这篇关于SQL数据库操作基础的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求