SQL

简介: 1、结构化查询语言SQL(Structed Query Language)建立在关系运算理论基础上,介于关系代数与关系演算之间,是一种通用的、功能强大的关系数据库语言。

1、结构化查询语言SQL(Structed Query Language)建立在关系运算理论基础上,介于关系代数与关系演算之间,是一种通用的、功能强大的关系数据库语言。

2、SQL语言提供两种主要工作方式:1)交互式命令方式 2)嵌入式程序方式

3、内模式—模式—外模式

4、表、主外键关系、候选键、约束、索引、视图、游标

5、查询、子查询、连接查询(内联接、外联接、交叉联接)

6、聚合函数、count、sum、max、min、avg

7、sql语句,CRUD(create、retrieve、update、delete)

8、T-SQL

declare @min tinyint
set @min=25
select cno,cname,csex,cage,crank from customers where cage between @min and 31
declare @min tinyint,@max tinyint
set @min=25
set @max=31
select cno,cname,csex,cage,crank from customers where cage between @min and @max
declare @male_num tinyint,@female_num tinyint
set @male_num=(select count(*) from customers where csex='男')
set @female_num=(select count(*) from customers where csex='女')
if(@male_num>@female_num)
  select cno,cname,csex,cage from customers where csex='男'
else
  select cno,cname,csex,cage from customers where csex='女'
update customers set crank=
  case
      when crank='普通会员' then '注册会员'
      when crank='注册会员' then '银卡会员'
      when crank='银卡会员' then '金卡会员'
      when crank='金卡会员' then 'VIP会员'
      when crank='VIP会员' then 'VIP会员'
while((select min(price) from products where price<=30)>10)
begin
  update products set price=price*0.9 where price<=30
end
select pno,pname,price from products where price<=30
waitfor delay '00:02:00'  --等待2分钟执行
select * from customers
declare @N int 
select @N=1
sect_1:
  print @N*@N
  select @N=@N+1
  while @N<7
     goto sect_1

9、函数

select avg(price) as avg_price,count(price) as count_price,sum(price) as sum_price,max(price) as max_price,min(price) as min_price from products
selecct var(price) from products --统计方差
select month(0),day(0),year(0)
select year('03/12/1998')
select month('03/12/1998')
select day('03/12/1998')
select getdate()
select getutcdate()
select datepart(year,getdate())
select round(150.75,0) --151.00
select ceiling($123.45),ceiling($-123.45)  --124.0000 -123.0000
select floor(123.45),floor(-123.45),floor($123.45) --123 -124  123.0000
declare @seed smallint
set @seed=1
while @seed<3
begin
  print rand(@seed)
  set @seed=@seed+1
end



 

目录
相关文章
|
4月前
|
SQL HIVE
每天一道大厂SQL题
每天一道大厂SQL题
54 1
每天一道大厂SQL题
|
2月前
|
SQL 存储 大数据
SQL技巧
【7月更文挑战第26天】SQL技巧
18 1
|
4月前
|
SQL 关系型数据库 MySQL
盘点6个SQL小技巧
这篇内容介绍了数据库查询中的各种JOIN操作,包括内联接(inner join)、左外联接(left outer join)、右外联接(right outer join)和全联接(full outer join)。其中,LEFT JOIN可以用于替换NOT EXISTS和NOT IN的查询。接着,文章展示了如何查询每个类别中的最高分记录,以及如何利用GROUP BY和LIMIT获取每个类别中的前N个记录。此外,还提到了MySQL 8引入的新语法LATERAL JOIN,用于更方便地处理这类问题。最后,文章提到了如何高效地统计不同时间范围内的数据量以及对比两个表之间的数据差异。
|
4月前
|
SQL 数据库 索引
SQL常用知识
SQL常用知识
|
4月前
|
SQL 关系型数据库 MySQL
|
SQL 程序员 数据库
sql 总结
一对多:在多的表中添加建立关系的字段(外键)指向另外一张表。如果需要查询一张表的全部和另外一张表的交集时,使用外连接,连表查询(左外连接)(显示左表的全部信息和右表相关联的信。连表查询(右外连接)(显示右表的全部信息和左表相关联的信。等值连接和内连接查询的是两个表的交集数据,推荐使用内连接。:选择插入必须选择需要插入的字段,选择对应字段的值,批量。查询所有部门的名称,地点和对应的员工姓名和工资。等值连接和内连接查询到的都是两张表的交集数据。外连接查询的是一张表的全部和另外一张表的交集。
118 0
|
SQL 数据挖掘 Python
sql8&10&11&12,3+1
sql8&10&11&12,3+1
106 0
sql8&10&11&12,3+1
|
SQL
sql last
sql last
59 0
|
SQL Oracle 关系型数据库
一个需求的三种实现(sql)
一个需求的三种实现(sql)
一个需求的三种实现(sql)
|
SQL 数据库
了解SQL
了解SQL
118 0