数据库语句

1. 数据定义

// 定义模式
create schema 模式名 authorization 用户名;

//删除模式
drop schema 模式名 cascade/restrict;

//定义基本表
create table 表名 (
列名 数据类型 列级完整性约束,
列名 数据类型 列级完整性约束,
列名 数据类型 列级完整性约束
);

//修改基本表
alter table 表名 add 新列名 数据类型 完整性约束条件;
alter table 表名 add 表级完整性约束条件;
alter table 表名 drop 列名 cascade/restrict;
alter table 表名 drop constraint  完整性约束名 cascade/restrict;
alter table 表名 alter 列名 数据类型; 

//删除基本表
drop table 表名 cascade/restrict;

//建立索引
create unique/cluster index 索引名 on 表名(列名);

//删除索引
drop index 索引名;

2.数据查询

// select查询
select */all/distinct 目标列名,目标列名,目标列名
from 表名/视图名,表名/视图名,表名/视图名
where 条件表达式
group by 列名1 having 条件表达式
order by 列名2 asc/desc;

//查询条件
1.  between xxx and xxx;
2.  not between xxx and xxx;
3.  =,>,<,<=,>=,!=,!>,!<,not in 上述比较运算符
4.  in,not in
5.  like,not like
	a%b  //以a开头,b结尾,任意长度
	a_b  //以a开头,b结尾,长度为3
	escape'\'  //转换字码
	
	例子:以DB_开头,倒数第三个字符为i
	where name like 'DB\_%i_ _'escape'\';
	
6.  is null,is not null
7.  and,or,not

//聚集函数
函数	说明
AVG(distinct/all 列名)	     返回某列的平均值
COUNT(distinct/all 列名)	 返回某列的行数
MAX(distinct/all 列名)	     返回某列的最大值
MIN(distinct/all 列名)	     返回某列的最小值
SUM(distinct/all 列名)	     返回某列之和
只能用于group by 中的having子句
和select子句

//group by :
1.select sno from sc group by sno having count(*)>3;
2.select sno,avg(grade) from sc group by sno having avg(grade)>=90;

//插入
insert 
into 表名
value();

//修改数据
update 表名
set 列名=表达式
where 条件;

//删除数据
delete 
from 表名
where 条件;

3.数据安全性

//授权
grant 权限,权限
on 对象类型 对象名,对象类型 对象名
to 用户,用户
with grant option;

//收回
revoke 权限,权限
on 对象类型 对象名,对象类型 对象名
from 用户,用户
cascade/restrict;

//创建数据库模式的权限
create user 用户名 with DBA/resource/connect;

//创建角色
1.
create role 角色名;

2.
grant 权限
on 对象类型 对象名
to 角色;

3.
grant 角色
to 角色1,用户1
with admin option;//传播

4.
revoke 权限
on 对象类型 对象名
from 角色;

//视图机制
create view 视图名
as
select xxx
from   xxx;

4.数据完整性

实体完整性:primary key

参照完整性:foreign key
例:foreign key(sno) references student(sno)
on delete cascade 
on update cascade;

用户定义完整性:
not null
unique
check

完整性约束条件子句:
constraint c1 check (sex in('男','女'));

alter table student
drop constraint c1;

//创建断言
create assertion 断言名  check子句;
//删除断言
drop assertion 断言名;

//创建触发器
create trigger 触发器名
before/after 触发事件 on 表名
referencing new/old row as 变量
for each row/statement
when 触发条件 触发动作体;:
·
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值