database-note-4

mac2024-04-18  31

SQL-续

not null 与 unique

not null

unique

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

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

check

类似where,后面接条件。

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

外码约束

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

级联

1234567891011121314# 主表ddlcreate table main_table ( A char(5) primary key, other_id varchar(60) references f_table on delete cascade # 删除设定为级联操作 on update cascade, # 更新设定为级联操作 ...);# 外表ddlcreate table other_name ( other_id int(11) primary key, other_other char(1) not null);

check

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

作用

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

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

index

1234567891011create table main_table ( A char(5) primary key, other_id varchar(60) references f_table on delete cascade # 删除设定为级联操作 on update cascade, # 更新设定为级联操作 ...);# indexcreate index A_index on main_table(A);

作用

可以快速检索,详见11章

create type

12# 包装类型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

1grant <priviege list> on <relation name or view name> to <user list>;

<priviege list>:

insertupdatedeleteselectall privieges

<user list>:

a user-idpublic, allow all user valid users the priviledge granteda role

revoke

回收权限

1revoke <priviledge list>;

all 代表全体用户。

public 代表非指明的用户。

特殊情况

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

role

12craete role instructor;grand instructor to Amit;

作用

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

最新回复(0)