数据库原理与技术(实例) - 张祖平 1 大型数据库完整性典型实现方式 在大型数据库中的完整性控制 触发器.

Slides:



Advertisements
Similar presentations
MySQL. To start go to Login details: login: labuser password:macimd15 – There.
Advertisements

SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Triggers. Triggers: Motivation Assertions are powerful, but the DBMS often can’t tell when they need to be checked. Attribute- and tuple-based checks.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Constraints We have discussed three types of integrity constraints: primary keys, not null constraints, and unique constraints. CREATE TABLE Movies ( title.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Integrity 7. 1 CSE2316/3316 Database Management Systems Database Integrity.
Harris Spring 2005 讲座一 “ERP 与企业流程再造 ” 东南大学 自控系 赵霁教授 电话: 一 ERP 是什么? 二企业为什么要引入 ERP 系统? 三企业管理软件的发展与 ERP 的创新之处 四 ERP 与流程再造的关系。 五 企业流程再造规划分析。
数据库与智能网络研究室. © htttp://dbin.jlu.edu.cn 数据库系统原理复习大纲.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Triggers.
Database Management System LICT 3011 Eyad H. Elshami.
Oracle Data Definition Language (DDL)
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
1 The Relational Model Instructor: Mohamed Eltabakh
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL: DDL John Ortiz Cs.utsa.edu.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Intro to SQL| MIS 2502  Spacing not relevant › BUT… no spaces in an attribute name or table name  Oracle commands keywords, table names, and attribute.
CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
1 ISYS Triggers. 2 Agenda Triggers Review Correlation identifiers (pseudo records) Restrictions on triggers Trigger usage Mutating tables Enabling.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
Visual Programing SQL Overview Section 1.
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
1 SQL Insert Update Delete Create table Alter table.
More on Primary and Foreign Keys Please see speaker notes for additional information!
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
SQL1-ch9 使用 DDL 建立與管理表格 1. 題號  80 題: 63 、 76  140 題: 6 、 24 、 44 、 71 、 77 、 92 2.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Including Constraints
SQL: Schema Definition and Constraints Chapter 6 week 6
Constraints and Triggers
Active Database Concepts
Introduction to Database Systems, CS420
Instructor: Mohamed Eltabakh
الأزنده أ.موضي المحرج.
PL/SQL Programing : Triggers
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
CS122 Using Relational Databases and SQL
Oracle Data Definition Language (DDL)
Instructor: Mohamed Eltabakh
Triggers.
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Including Constraints
Presentation transcript:

数据库原理与技术(实例) - 张祖平 1 大型数据库完整性典型实现方式 在大型数据库中的完整性控制 触发器

数据库原理与技术(实例) - 张祖平 2 在大型数据库中的完整性控制 RDBMS 中的完整性 实体完整性 参照完整性 用户自定义完整性 约束 Constraint NULL/NOT NULL Unique Primary Key Foreign Key References check

数据库原理与技术(实例) - 张祖平 3 例1例1 create table customers (cid char(4) not null, cname varchar(13), city varchar(20), discnt real check(discnt <= 15.0), primary key(cid)); Cid not null, primary key, unique Discnt <=15.0

数据库原理与技术(实例) - 张祖平 4 例题 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);

数据库原理与技术(实例) - 张祖平 5 关于 FK … REFs Now with this FOREIGN KEY... REFERENCES clause for cid in orders table, if try to insert an orders row with cid value that isn't in customers, insert will fail and give an error condition. Can also have larger tuples matching: Create table employees (.. …. foreign key (cityst, staddr, zip) references ziptst(cityst, staddr, zip);

数据库原理与技术(实例) - 张祖平 6 完整的表定义格式 CREATE TABLE 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}...}); col_constr table_constr

数据库原理与技术(实例) - 张祖平 7 列 Constraints. The col_constr form that constrains a single column value follows: {NOT NULL | [CONSTRAINT constraint-name] UNIQUE | PRIMARY KEY | CHECK (search_cond) | REFERENCES table-name [(col-name) ] [ON DELETE CASCADE]}

数据库原理与技术(实例) - 张祖平 8 例3例3 Create table dept(DNO CHAR(3) PRIMARY KEY, DN VARCHAR2(32), DEAN CHAR(8), TEL CHAR(8), /*, have or no */ CHECK(SUBSTR(tel,1,3)='887 ‘ ) ); Col-cons and tab_cons CREATE TABLE STUD(sno CHAR(8) constraint stud_pk PRIMARY KEY,/* DEMO */ sn CHAR(8),bd DATE, DNO CHAR(3) constraint stud_fk REFERENCES DEPT(DNO) ON DELETE CASCADE );

数据库原理与技术(实例) - 张祖平 9 例 4 ORACLE 标准修改表的语法 ALTER TABLE tablename [ADD ({colname datatype [DEFAULT {default_const|NULL}] [col_constr {col_constr...}] | table_constr} {,...})] [DROP {COLUMN col-name | (col-name {, col-name … })}] [MODIFY (col-name data-type [DEFAULT {default_const|NULL}] [[NOT] NULL]{,...})] [DROP CONSTRAINT constr_name] [DROP PRIMARY KEY] [disk storage and other clauses (not covered, or deferred)] [any clause above can be repeated, in any order] [ENABLE and DISABLE clauses for constraints];

数据库原理与技术(实例) - 张祖平 10 例5例5 Alter table orders add constraint order_check_dollars check(dollars>=10); Alter table orders modify primary key disable; Alter table stud drop column bd; Alter table orders modify order_check_dollars disable;

数据库原理与技术(实例) - 张祖平 11 2 触发器 TRIGGER CREATE TRIGGER trigger_name BEFORE | AFTER {INSERT | DELETE | UPDATE [OF colname {, colname...}]} ON tablename [REFERENCING corr_name_def {, corr_name_def...}] [FOR EACH ROW | FOR EACH STATEMENT] [WHEN (search_condition)] {statement--action (single statement) | BEGIN ATOMIC statement; { statement;...} END}

数据库原理与技术(实例) - 张祖平 12 例6例6 create trigger discnt_max after insert on customers referencing new x for each row when (x.discnt > 15.0) begin raise_application_error(-20003, 'invalid discount on insert'); end; /

数据库原理与技术(实例) - 张祖平 13 例7例7 create trigger foreigncid after delete on customers referencing old ocust for each row -- no WHEN clause -- PL/SQL form starts here begin update orders set cid = null where cid = :ocust.cid; end;

数据库原理与技术(实例) - 张祖平 14 例 8 ( 问题 1) Emp( EMPNO,ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO ) 1 SAL+COMM<= SAL+COMM ’ PRESIDENT ’ 3 SAL GRADE (TABLE salgrade ) GRADE LOSAL HISAL

数据库原理与技术(实例) - 张祖平 15 例 8 ( 问题 1) 1 Alter table emp add constraint emp_check_sal check(sal+comm<=5000); 2 Alter table emp add constraint emp_check_sal check(sal+comm<=5000) when job<> ’ PRESIDENT ’ ; 有语法错误!!

数据库原理与技术(实例) - 张祖平 16 例 8 ( 解决 3) Create trigger trg_emp before insert or update on emp for each row when (new.job<> ’ PRESIDENT ’ ) BEGIN if nvl(:new.sal,0)+nvl(:new.comm,0)>=5000 then raise_application_error(-20003, 'invalid sal or comm'); End if; End;

数据库原理与技术(实例) - 张祖平 17 例 8 ( 解决 3) Create or replace trigger trg_emp before insert or update on emp for each row declare hi number(8,2); lo number(8,2); BEGIN Select losal,hisal into lo,hi from salgrade where job=:new.job; if :new.sal>hi or :new.sal<lo then raise_application_error(-20003, 'invalid sal'); End if; End;