database-note-4

SQL-续

not null 与 unique

not null

unique

1
unique (A_1, A_2, ... , A_n)

后接属性集,可作为候选码。

check

类似where,后面接条件。

1
2
3
4
5
create table s_table (
...,
...,
check (a_1 in ('ABC', 'WIN', 'DES')) # a_1 属性只能为'ABC', 'WIN', 'DES', 类似枚举
)

外码约束

1
2
foreign key S references table_name
# S: 主表中做外吗,table_name: 外表

级联

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 主表ddl
create table main_table (
A char(5) primary key,
other_id varchar(60) references f_table
on delete cascade # 删除设定为级联操作
on update cascade, # 更新设定为级联操作
...
);

# 外表ddl
create table other_name (
other_id int(11) primary key,
other_other char(1) not null
);

check

1
2
# 嵌套子查询表达某个属性取值的范围
check (time_slot_id in (select time_slot_id from time_slot));

作用

当某个属性只是参照完整性约束下的码而不是外码,可以用check clause对这种情况进行描述。

在创建表的时候描述为对某种参照情况的描述。

index

1
2
3
4
5
6
7
8
9
10
11
create table main_table (
A char(5) primary key,
other_id varchar(60) references f_table
on delete cascade # 删除设定为级联操作
on update cascade, # 更新设定为级联操作
...
);

# index

create index A_index on main_table(A);

作用

可以快速检索,详见11章

create type

1
2
# 包装类型
create type Dollars as numeric(12, 2) final;

大数存储

  • blob 数字型大数
  • clob 字符型大数

授权

Authorization

  • 数据授权
    • Read: allow reading.
    • Insert: allow insert, not modification.
    • Update: allow modification.
    • Delete: allow deletion.
  • 结构授权
    • Index: allow create and deletion of Indices.
    • Resourse: allow creation of relation.
    • Alteration: allow addition or deletion of attribute in a relation.
    • Drop: allow deletion of relations.

grant

1
grant <priviege list> on <relation name or view name> to <user list>;
<priviege list>:
  • insert
  • update
  • delete
  • select
  • all privieges
<user list>:
  • a user-id
  • public, allow all user valid users the priviledge granted
  • a role

revoke

回收权限

1
revoke <priviledge list>;

all 代表全体用户。

public 代表非指明的用户。

特殊情况

当某个用户在同一个权限上被授予两次,每次调用revoke会回收其中一条权限。

role

1
2
craete role instructor;
grand instructor to Amit;
作用

当某个用户从表中删除,其关联的角色所授予的权限仍然有效。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值