本篇为基础的sql语句,方便sql注入时忘记语法来进行查询
CREATE/DROP/ALTER语句 模式(schema) mysql中schema = database
1 2 3 4 5 6 7 create schema 模式名 authorization 用户名;create schema authorization 用户名; drop schema 模式名 cascade; drop schema 模式名 restrict;
基本表(table) 定义基本表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #定义基本表create table 表名 ( 列名1 数据类型 列级完整性约束条件, 列名n 数据类型 列级完整性约束条件, 表级完整性约束条件1 , 表级完整性约束条件n );char (n) varchar (n) number(n) int smallint bigint float (n) date time primary keynot null unique check primary key(列名1 , 列名n)foreign key(列名1 ) references 被参照表(列名1 ) #在模式中定义表create table 模式名.表名 ( 列定义语句, 表完整性约束 );create schema 模式名 authorization 用户名 create table 表名 ( 列定义语句, 表完整性约束 );ALTER TABLE 模式名.表名 SET SCHEMA 新模式名;
修改基本表 1 2 3 4 5 6 7 8 9 10 11 alter table 表名 add column 新列名 数据类型 完整性约束;alter table 表名 add 列级完整性约束; alter table 表名 add 表级完整性约束;alter table 表名 drop 列名 cascade; alter table 表名 drop 列名 restrict;alter table 表名 drop constraing 完整性约束 cascade/ restrict;alter table 表名 alter column 列名 数据类型;
删除基本表 1 drop table 表名 cascade/ restrict;
索引 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #唯一索引create unique index 索引名 on 表名(列名1 次序, 列名n 次序); #聚簇索引create cluster index 索引名 on 表名(列名1 次序, 列名n 次序);ASC DESC #修改索引alter index 旧索引名 rename to 新索引名; #删除索引drop index 索引名;
SELECT语句 1 2 3 4 5 select distinct / all 属性from 表/ 视图where 条件表达式group by 列名 having 条件表达式order by 列名 次序/ 默认ASC ;
基本查询 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 select * 表达式 列名 别名distinct 列名 #聚集函数count (* )count (distinct / all 列名)avg (distinct / all 列名)sum (distinct / all 列名) max/ min (distinct / all 列名)where 列名= > < >= <= != (<> )between a and bin ('分量1' , '分量n' )like '字符串' escape '\' not like '' #通配符 # % 0 ~ 任意长度 # _ 单个字符, 一个汉字(两个字符)用两个_ # escape 定义转义字符is null is not null group by 列名1 group by 列名 having 条件order by 列名1 , 列名2
where与having区别
where用来从基本表与视图中选择条件,不接聚集函数
having用于从组中选择条件
连接查询 1 2 #两表连接查询where 表名1. 列名1 比较运算符 表名2. 列名2 ;