在MySQL的bin目录下cmd
输入命令
mysql -uroot -p
出现Enter password提示,输入密码即可登录
show databases;
注意:绝大部分SQL命令都以分号作为结束符!!!
create database boke;
use boke;
create table artical(id int(8),name varchar(20),score int(5));
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),必须使用单引号或双引号包裹!!!
查询全部
select * from artical;
查询id=5的score
select score from artical where id=5;
删除id=6的数据
delete from artical where id=6;
修改id=5的数据,将其score设置为30
update artical set score=30 where id=5;
(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,数据库就会报错。可以利用这个特点判断数据库查询的字段数!!!
基本格式为: limit M,N //表示从第M+1条数据开始,顺序往下查询N条数据 limit M //表示查询前M条数据
查询表中3,4条数据
select* from artical limit 2,2;
查询表中前3条数据
select * from artical limit 3;
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;
从以上结果可以总结,在已知字段名的情况下,攻击者只要将该字段置于任何能够显示的位置,就可以暴露该字段的值。
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';
两组语句执行结果相同!