主要数据库
- 面向关系的数据库
Oracle
–企业级MySql
–主要用于开发网站SQLServer
–开发企业内部PostgreSql
–同MySql
- 非关系型数据库
MongoDB
Redis
PostgreSql
的基础数据类型
数值型:
integer(int)
real
serial
文字型:
char
varchar
text
日期型:
date
time
timestamp
特色类型:
Array
- 网络地址型(
inet
) JSON
型XML
型
约束条件
1.not null
:不能为空
2. unique
:在所有数据中值必需唯一
3. check
:字段设置条件
4. default
:字段默认值
5. primary key(not null, unique)
:主键,不能为空,且不能重复
INSERT
语句
insert into [tablename] (field,...) values (value,...)
SELECT
语句
横向显示
select * from users
纵向显示
/x
select * from users
WHERE
语句
- 使用
where
语句来设定select
,update
,delete
语句抽象出的条件 - 例:
select * from users where score > 20
数据抽出选项
-
order by
:排序 -
limit
:取前三名 -
offset
:从第几条开始取 -
例:升序排序:
select * from users order by score asc
-
例:降序排序:
select * from users order by score desc
-
例:降序排序取前三:
select * from users order by score desc limit 3
-
例:降序排序从第二个开始取前三:
select * from users order by score desc limit 3 offset 1
统计抽出的数据
-
distinct
–过滤重复的数据 -
sum
–求和 -
max/min
–取出指定字段的最大值或最小值 -
group by/having
–对集合分组再计算 -
例:选取字段的唯一值:
select distinct team from users
-
例:求出字段总和:
select sum(score) from users
-
例:求出字段最小得分:
select min(score) from users
-
例:求出字段最大得分:
select max(score) from users
-
例:取出最高得分球员信息:
select * from users where score = (select max(score) from users)
-
例:按队伍分组取出最小得分:
select team,min(score) from users group by team
-
例:按队伍分组取出大于25的数据:
select team,max(score) from users group by team having max(score) >=25
-
例:按队伍分组取出大于25的数据按照升序排序:
select team,max(score) from users group by team having max(score) >=25 order by max(score)
方便的函数
-
length
–长度 -
concat
–连接两个字符串 -
alias
–对查询字段起一个别名 -
substring
–切割字符串 -
random
–随机函数 -
例:打印除字段的名称和长度:
select player.length(player) from users
-
例:连接两个字段并起一个别名:
select player.concat(player,'/',team) as"球员信息" from users
-
例:取出字段的首字:
select substring(team,1,1) as "球队首文字" from users
-
例:随机函数(从0到1内的值):
select random()
-
例:乱序抽出全部数据:
select * from users order by random()
-
例:随机抽出一条数据:
select * from users order by random() limit 1
更新和删除
update [table] set [field=newvalue,...] where ...
物理删除–删除信息不可恢复 注意!!!!!delete from [table] where ...
- 例:更新
users
表设置分数+100
在队伍勇士和骑士队中:update users set score = score + 100 where team IN ('勇士','骑士')
变更表结构
alter table [tablename] ...
建立删除索引
create index ...
drop index ...
- 例:添加字段:
alter table users add fullname varchar(255)
- 例:更改字段名:
alter table users rename players to nnn players
操作多个表
- 例:表结合查询:
select u.player,t.content from users as u,twitters as t where u.id = t.ueser_id and u.id = 1
使用视图
- 视图就是一个
SELECT
语句,吧业务系统中常用的SELECT
语句简化成一个类似于表的对象,便于简单读取和开发。 - 例:表结合查询:
select u.player,t.content from users as u,twitters as t where u.id = t.ueser_id and u.id = 1
- 创建一个视图:
create view curry_twitters as select u.[layer,t.content from users as u,twitters as t where u.id = t.user_id and u.id = 1
- 使用视图:
select * from curry_twitters
使用事务
- 事务的使用
begin
–开始事务commit
–提交事务rollback
–回滚
提交事务更新数据
begin
update users set score = 50 where player = '库里'
update users set score = 60 where player = '哈里'
commit
执行事务回滚恢复之前状态
begin
update users set score = 0 where player = '库里'
update users set score = 0 where player = '哈里'
rollback