3.0 数据库--基本操作

select * from 表名称 // **表示返回所有列
select distinct 列名称 from 表名称 // 去重
select 列名 from 表名 where 列 运算符 值 // between(范围),like(模式)
// 文本需加单引号,数值不需。
//and or 可以连接多重选择条件。
order by 对指定列进行排序:
select * from 表 order by 列名 // 以顺序排序
select * from 表 order by 列名 desc // 以逆序排序

insert into 表名 values(val1, val2, … val) // 插入新行

update 表名 set 列名=新值 where 列名=某值
update person set address = ‘Zsh’ where LastName = ‘Wilson’//two列加‘,’

delete from 表名 where 列名=value

top/limit/rownum:返回k个值
select top k * from 表名

like:模式
select * from 表名 where 列名 like ‘%xyz%’ // 列值包含xyz的数据行
// %:替代一个或多个字符;_:替代一个值;[charlist]: 其中的任意字符;!:不在

in:可在where规定多个值,实现多值查找。
select * from 表名 where 列名 in(‘val1’, ‘val2’, ‘val3’) // 列值包含xyz的数据行

between … and … : 选取介于两值间的数据
select * from 表名 where 列名 between ‘val1’ and ‘val2’
select * from 表名 where 列名 not between ‘val1’ and ‘val2’

as: 别名
select 列名1 as 别名1,列名2 as 别名2 where 表名

join:根据两个或多个表中的列间关系查询数据
select 表1.列1,表2.列2 from 表1,表2 where 表1.key= 表2.key
select 表1.列1,表2.列2 from 表1 inner join 表2 on 表1.key= 表2.key order by 表1.列1

union:合并两个或多个 select 语句的结果集
select 列1 from 表1 union select 列2 from 表2 // 自动去重
select 列1 from 表1 union all select 列2 from 表2 // 不去重

均值:select avg(列名) from 表名
select 列名 from 表名 where 列名>(select avg(列名) from 表名) #高于均值
计数:select count(列名) from 表名 //不算null
select count(*) from 表名 //算null
select count(distinct 列名) from 表名 //不同列值
第一个值:
select first(列名) from 表名 // 指定字段的第一个值,last最后一个
max(列名) // 最大值,min最小,sum总和,

inner join: 内连接
left join: 关键字会从左表 (Persons) 那里返回所有的行,即使在右表 (Orders) 中没有匹配的行。
right join: 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行。
只要其中某个表存在匹配,full join关键字就会返回行。

union 操作符用于合并两个或多个select 语句的结果集。
union 操作符选取不同的值。如果允许重复的值,请使用union all。

select * into 表2 from 表1 // 将表1的查找数据插入表2

create database 库名
create table 表名(列1 类型1,列2 类型2,列3 类型3)

约束:not null,unique主键,foreign key(外键),check限制列值,default列默认值

索引:create index 索引名 on 表名 (列名) // unique index 唯一索引,两行不能相同

drop index 删除表格中的索引:drop index 索引名 on 表名
drop table 表名:删除表(结构,属性,索引)
drop database 数据库名:删除数据库
truncata table 表名:仅仅删除数据,不动结构及表本身

alter table 表名 add 列名 数据类型:添加列
alter table 表名 drop column 列名:删除列
alter table 表名 alter 列名 新数据类型

select 列名1,sum(列2) from 表1 group by 列1 // group by 结合合计函数,根据一个或多个列对结果集进行分组。
ucase() 函数把字段的值转换为大写; lcase 函数把字段的值转换为小写; len() 字段长度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值