首页 > 数据库 >【数据库原理、编程与性能】Integrity, View, Security

【数据库原理、编程与性能】Integrity, View, Security

时间:2023-06-20 11:38:40浏览次数:41  
标签:name cid Create colname constr table Security Integrity View



文章目录

  • 1. Integrity Constrains
  • 1.1 Definition
  • 1.2 实施机制
  • 1.3 Integrity Constraints in Create Table Statement
  • 1.3.1 Create Table
  • 1.3.2 Column Constraints
  • 1.3.3 Table Constraints.
  • 1.4 Alter Table Statement
  • 1.5 Trigger
  • 1.5.1 Create Trigger Statement
  • 1.5.2 Drop Trigger statement
  • 1.5.3 Trigger executed
  • 1.5.4 Two ways to realize the Referential Integrity
  • 2. Creating Views
  • 2.1 Create Views
  • 2.2 Drop Views
  • 3. Security & Privileges
  • 3.1 Grant (授予权限)
  • 3.2 Revoke (撤销权限)


1. Integrity Constrains

1.1 Definition

  • 表创建时定义完整性约束
  • 按作用对象分为:表级完整性、列级完整性
  • 按类型分为:实体完整性、参照完整性、用户自定义完整性

1.2 实施机制

  • 实体完整性:主键、唯一索引、非空+唯一
  • 参照完整性:外键、触发器、存储过程
  • 用户自定义完整性:非空、缺省值、取值约束等

1.3 Integrity Constraints in Create Table Statement

1.3.1 Create Table

CREATE TABLE [schema.]tablename
( colname datatype [DEFAULT {default_constant | NULL}]
[col_constr {col_constr. . .}] | table_constr
{ , {colname datatype [DEFAULT {default_constant | NULL}]
[col_constr {col_constr. . .}] | table_constr} . . . } );

eg 1 :Create Table statement for “products”.

create table products (pid char(3) not null,
pname varchar(13), city varchar(20),
quantity integer default 0 ,/*缺省填充0*/
price double precision default 0.0 ,
primary key (pid) );

eg 2 :Create Table statement for “customers”.

create table customers (cid char(4) not null unique ,
cname varchar(13), city varchar(20),
discnt real constraint discnt_max check (discnt<=15.0) );/*约束名为discnt_max,约束内容为discnt<=15.0*/

1.3.2 Column Constraints

{NOT NULL |
[CONSTRAINT constraint_name]
UNIQUE | PRIMARY KEY | CHECK (search_cond)
| REFERENCES tablename [(colname) ] [ON DELETE CASCADE]}

  • CONSTRAINT constraintname name for each constraint
  • UNIQUE constrained all the non-null column values in table are unique
  • PRIMARY KEY specifies a column to be a primary key
  • A column with PK constraint is implicitly in: NOT NULL + UNIQUE
  • PRIMARY KEY and NOT NULL can be used together, but the
  • UNIQUE and PRIMARY KEY cannot both be used for a column.

1.3.3 Table Constraints.

[CONSTRAINT constraint_name] {UNIQUE (colname{, colname…})
| PRIMARY KEY (colname {, colname……})
| CHECK (search_condition)
| FOREIGN KEY (colname {, colname……})
REFERENCES tablename [(colname {, colname……})]
[ON DELETE CASCADE]}

eg: Example7.1.2 Create tables of CAP DataBase.

create table customer ( cid char(4) not null,
cname varchar(13), city char(20),
discnt real constraint discnt_max check (discnt <= 15.0),
primary key ( cid ) ) ;
create table orders ( ordno integer not null, month char(3),
cid char(4) not null , aid char(3) not null,
pid char(3) not null , qty integer not null check(qty >= 0) ,
dollars float default 0.0 check(dollars >= 0.0) ,
primary key (ordno) ,
foreign key (cid) references customers,
foreign key (aid) references agents,
foreign key (pid) references products ) ;

1.4 Alter Table Statement

//change the structure of an existing table.

ALTER TABLE tablename
[ADD ({colname datatype [DEFAULT {default_constant| NULL}]
[col_constr {col_constr…}] | table_constr} {, …})]
[DROP {COLUMN columnname | (columnname {, columnname…})}]
[MODIFY (columnname data-type
[DEFAULT {default_const|NULL}] [[NOT] NULL] {, …})]
[DROP CONSTRAINT constr_name]
[DROP PRIMARY KEY]
[ENABLE and DISABLE clauses for constraints];

eg: 修改orders 表中的month 为日期类型、将ordno 字段改为实型.

思路:

(1) 创建日期型字段order_date, 再取出原month 值拼上“年、日”回填;

(2) 修改ordno 字段的类型

ALTER TABLE orders DROP PRIMARY KEY (ordno),

ADD order_date char(10) default “2018/01/01”,

MODIFY ordno real;

UPDATE orders set order_date= ……(month) ;

ALTER TABLE orders DROP month ;

1.5 Trigger

1.5.1 Create Trigger Statement

CREATE TRIGGER trigger_name { BEFORE | AFTER }
{ INSERT | DELETE | UPDATE [OF columnname {,columnname…}]}
ON tablename [REFERENCING corr_name_def {,corr_name-def…}]
[FOR EACH ROW | FOR EACH STATEMENT]
[WHEN (search-condition)]
statement – (single action)
| BEGIN statement; {statement;…} END – (multiple actions)

“corr_name_def” defines a correlation name follows:
{OLD [ROW] [AS] oldrow_corr_name
| NEW [ROW] [AS] newrow_corr_name
| OLD TABLE [AS] oldtable_corr_name
| NEW TABLE [AS] newtable_corr_name}

eg:Use an ORACLE trigger to check the DISCNT value of a new customers does not exceed 15.0 .

CREATE TRIGGER discnt_max AFTER INSERT ON customers
REFERENCING new as x FOR EACH ROW
WHEN ( x.discnt > 15.0)
begin
raise_error(-20003, “ invalid discount on insert ” );
end;

1.5.2 Drop Trigger statement

DROP TRIGGER trigger_name;

1.5.3 Trigger executed

{INSERT | DELETE | UPDATE [of colname {, colname . . .}]}

1.5.4 Two ways to realize the Referential Integrity

(1) Using a Foreign Key

(2) Using a Trigger

2. Creating Views

View is a result from a Subquery, it is a virtual table that no data store in it.

(视图是子表的查询结果,它是一个没有数据存储的虚表)

View has its own name and can be treated as if it were a base table.

2.1 Create Views

CREATE VIEW view_name [(colname {,colname…})] AS subquery [WITH CHECK OPTION];

eg 1 :Create a view “agentorders” that extends the rows of“orders” table to include all information about the agent taking the order.

Create view agentorders (ordno, month, cid, aid, pid, qty, charge,
aname, acity, percent) /* must be named */
as select o.ordno, o.month, o.cid, o.aid, o.pid, o.qty, o.dollars,
a.aname, a.city, a.percent from orders o, agents a
where o.aid = a.aid;

eg 2 :Create a view “acorders” that gives all order information and names of the agent and customers involved in order.

Defined new view from “agentorders” in Example 7.2.1. ( 在视图之上定义视图)

create view acorders (ordno, month, cid, aid, pid, qty, dollars, aname, cname)
as select ordno, month, ao.cid as cid, aid, pid, qty, charge, aname, cname
from agentorders ao, customers c where ao.cid = c.cid;

2.2 Drop Views

DROP {TABLE tablename | VIEW viewname} {CASCADE | RESTRICT};

In ORACLE with [CASCADE], means constraints referring to the table (as Foreign-Key) are dropped. If there is [RESTRICT], then Drop fails.

3. Security & Privileges

3.1 Grant (授予权限)

GRANT { ALL PRIVILEGES | privilege {, privilege…}}
ON [TABLE] tablename | viewname
TO {PUBLIC | user-name {,user-name…}} [WITH GRANT OPTION]

eg:Grant select, update, insert , but not to delete to “tom” on table orders. Then give “tom” authorization for all operations on products.

GRANT select, update, insert ON orders to tom;
GRANT all privileges ON products to tom;

3.2 Revoke (撤销权限)

REVOKE {ALL PRIVILEGES | priv {, priv…}} on tablename | viewname
FROM { PUBLIC | user {, user…} } [CASCADE |RESTRICT];


标签:name,cid,Create,colname,constr,table,Security,Integrity,View
From: https://blog.51cto.com/u_16165815/6521616

相关文章

  • LabVIEW开发的测试设备软件代码和PLC程序 前
    LabVIEW开发的测试设备软件代码和PLC程序前几年给一台检测设备做的上位机软件,三条测试支路共用同一个状态机vi,每个支路可独立运行,按编号区分每路的控制,下位机为西门子200smart,上下位机通过ModBUSTCP/IP通信。可以给LabVIEW学习者带来一些开发思路和启发。LabVIEW开发的测......
  • labview和西门子plc走以太网通信 这段话涉及到的知识点是LabVIEW和西
    labview和西门子plc走以太网通信这段话涉及到的知识点是LabVIEW和西门子PLC的以太网通信。LabVIEW是一种图形化编程环境,用于控制、测量和监视各种设备和系统。它可以与各种硬件设备进行通信,并提供了丰富的功能和工具来处理数据和执行自动化任务。西门子PLC(可编程逻辑控制器)是一种......
  • uniapp企业微信web-view父子通信问题
    项目背景:开发工具为HBuilderX,框架为uniapp,开发移动端的Web应用,在企业微信中使用(自建应用),Web开发的应用,不是小程序。需求:页面中用到<web-view>组件,加载其他系统的页面(有跨域),需要在父子页面之间相互通信。这里通信的东西其实就是获取定位,通过uniapp获取用户定位信息,传递给<web-view......
  • 「JOISC 2023 Day4」 Security Guard
    subtask1因为\(1\les_i\le2\),所以每艘船上都至少有一个保安。令\(cnt_i\)表示第\(i\)艘船上的保安数,可以先将所有\(cnt_i+=1\),所有\(s_i-=1\)。经过这一次操作后,如果两艘船之间的小岛的\(s_i\)全为\(0\),表示这两艘船可以相互到达,即可将这两艘船合并成一艘,然后再做一......
  • 什么是 SAP Commerce Cloud SmartEdit 的 preview API
    PreviewAPI使得SmartEdit能够将商户网站加载到请求的体验环境上的iframe中。体验环境是指特定站点、目录和目录版本的商户网站,并且还可以是指定的语言、日期和时间。为了以指定的体验环境呈现商户网站,SmartEdit将请求的商户网站加载到请求的体验环境的iframe中。为了在请求的体......
  • MVCC并发版本控制之重点ReadView
    MVCC并发版本控制本文大部分来自《MySQL是怎样运行的》,这里只是简单总结,用于各位回忆和复习。版本链对于使用InnoDB存储引擎的表来说,它的聚簇索引记录中都包含两个必要的隐藏列(不知道的快去看《MySQL是怎样运行的》)trx_id:每次一个事务对某条聚簇索引记录进行改动时,都会把......
  • uview提示:设置rules,model必须设置
    问题:setRules时,uview提示:设置rules,model必须设置原因:<u-formref="form1"v-model="model1">眼瞎把v-model当成:model,可能全网只有我遇到。解决:<u-formref="form1":model="model1">正确绑定model这个prop即可拓展(仅作白话解释,详情查阅vue官网):v-model双向绑定,......
  • 利用react-json-view最JSON数据进行渲染
    1.安装npminstall--savereact-json-view2.使用importReactJsonfrom"react-json-view";constA=()=>{letsrc={"content-length":"675","x-b3-parentspanid":"06c634eea567252a",&quo......
  • 安科瑞APView电能质量监测设备功能介绍
    安科瑞虞佳豪1.1概述随着电气环境中自动化程度的提高,以电力电子技术为代表的各种整流、逆变、变频等非线性负载比重不断变大,加之调控手段不完善及外来干扰等原因,使得电能质量下降。基于计算机、微处理器控制的电子仪器在国民经济企业中大量使用,对供电质量的敏感程度越来越高......
  • c# listview
     1、属性CheckBoxes ture表头显示checkHideSelection失去焦点后选中的依然有区别2、           listView1.View=View.Details; //这样才能显示           this.listView1.Columns.Add("测试项",100,HorizontalAlignment.Left);     ......