百度百科
关系数据库,是创建在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。现实世界中的各类实体以及实体之间的各类联系均用关系模型来表示。标准数据查询语言SQL就是一种基于关系数据库的语言,这种语言执行对关系数据库中数据的检索和操做。 关系模型由关系数据结构、关系操做集合、关系完整性约束三部分组成。
简单说,关系型数据库是由多张能互相联接的二维行列表格组成的数据库。sql
当前主流的关系型数据库有MySQL、Oracle、 SQL Server、Microsoft Access等。数据库
with tt as (select * from emp) select select * from from tt ;
select rownum, e.* from e where rownum < 11; -- 显示10条结果
select stuName as 姓名,stuAge as 年龄,stuID as 身份号 from stuInfo; select stuName 姓名,stuAge 年龄,stuID 身份号 from stuInfo;
alter table monthly_indicator rename to mi;
alter table mi rename column avgaqi to averageaqi; alter table mi drop column averageaqi;
select 1+1 from dual; #并非真的须要去表里查询,仅用于语法补位。 select initcap('hello world') from dual; #initcap 首字母变成大写。 select instr('hello world','w') from dual; #instr 查询字符串出现的位置。 select concat(lower('HELLO'), upper('world')) from dual; #concat 函数,链接字符串。 select replace('hello earth','earth','world') from dual; #replace 函数,替换字符串。 select substr('hello world',3,5) from dual; #substr 函数,提取字符串中起始位置(3),提取长度(5)。 select last_day(sysdate) from dual; #last_day 函数,当前月份最后一天的日期。 select to_date('2019-01-01','YYYY-MM-DD') from dual; #to_date 函数,将文本转换成正确的日期型日期。
Oracle数据库存储过程差别较大,过程详细转载:Oracle存储过程markdown
SQL Server使用T-SQL语言,是SQL程序设计语言的加强版数据结构
declare @myparam int --声明局部变量 select @myparam = 1 --为局部变量赋值 print @myparam --查看局部变量
select *, 备注 = case when f_price < 10 then '便宜' when f_price >= 10 and f_price <= 20 then '普通' when f_price > 20 then '贵' else '-' end from fruits
declare @x int set @x = 1 printout: -- 设定标记 print @x select @x = @x + 1 while @x < 5 goto printout -- 流程转向标记
declare @m int set @m = 10 while @m > 0 begin set @m = CAST(@m as varchar(4)) exec('alter table fruits drop column c'+ @m) set @m = CAST(@m as int) set @m = @m -1 end
create proc Proc_OutPutTest --建立 @numA int, --numA为存储过程的参数 @numB int, --numB为另外一参数 @numReturn int output --此为Output,也就是存储过程的返回值 as begin if(@numA>@numB) set @numReturn=@numA else set @numReturn=@numB --A>B的时候返回A,不然返回B end --begin...end肯定存储过程包含语句范围。 declare @numReceive int --先声明一个变量用来接收存储过程的返回值 exec Proc_OutPutTest 1,2, @numReceive output --调用存储过程并用@numReturn接收存储过程的返回值 select @numReceive --将会返回(1,2)中较大的数字:2