다양한예제로쉽게배우는 오라클 SQL 과 PL/SQL 서진수저

Similar documents
Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

A <column constraint> is a constraint that applies to a single column.

CHAPTER4 CONSTRAINTS

DATA CONSTRAINT. Prepared By: Dr. Vipul Vekariya

q Ø v v v v v v v v IBM - 2

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

Difficult I.Q on Databases, asked to SCTPL level 2 students 2015

Database Programming - Section 10. Instructor Guide

5 Integrity Constraints and Triggers

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Data Definition Language (DDL)

Sample Question Paper

Lab # 3 Hands-On. DML Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

CS6312 DATABASE MANAGEMENT SYSTEMS LABORATORY L T P C

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

EXAMINATIONS 2009 END-YEAR. COMP302 / SWEN304 Database Systems / Database System Engineering. Appendix

Downloaded from

Structure Query Language (SQL)

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Group your Data. PPDM Data Management Symposium March 2012

BEGINNING T-SQL. Jen McCown MidnightSQL Consulting, LLC MinionWare, LLC

Indexes (continued) Customer table with record numbers. Source: Concepts of Database Management

Oracle Tables TECHGOEASY.COM

Appendix A Practices and Solutions

RDBMS Topic 4 Adv. SQL, MSBTE Questions and Answers ( 12 Marks)

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Table Joins and Indexes in SQL

MySQL Introduction. By Prof. B.A.Khivsara

Chapter 17: Table & Integrity Contraints. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh

To understand the concept of candidate and primary keys and their application in table creation.

SQL Server. Lecture3 Cascading referential integrity constraint

Trigger is a stored procedure which is called implicitly by oracle engine whenever a insert, update or delete statement is fired.

PL/SQL. Exception. When the PL/SQL engine cannot execute the PLSQL block it raise an error. Every Oracle error has an error number

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

MTAT Introduction to Databases

What are temporary tables? When are they useful?

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Database Programming with SQL

Lab # 4 Hands-On. DDL and DML Advance SQL Statements Institute of Computer Science, University of Tartu, Estonia

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

ACCESS isn t only a great development tool it s

Ciljevi. Poslije kompletiranja ove lekcije trebalo bi se biti u mogućnosti: Opisati ograničenja Generisati i održavati ograničenja u bazi

Objectives. After completing this lesson, you should be able to do the following:

Active Databases Part 1: Introduction CS561

Application Containers an Introduction

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

Oracle 1Z Oracle Database 12c: Advanced PL/SQL.

Application Containers an Introduction

Programming Languages

Application Containers an Introduction

CS510 Laboratory Exercise

The Structured Query Language Get Started

Full file at

IBM DB2 UDB V7.1 Family Fundamentals.

Oracle MOOC: SQL Fundamentals

Database Modelling. Lecture 4 (b): Database Integrity, Keys & Constraints. Akhtar Ali 10/14/2014 1

4 Schema Definition with SQL / DDL (II)

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Oracle User Administration

Lecture 07. Spring 2018 Borough of Manhattan Community College

ORACLE 12C NEW FEATURE. A Resource Guide NOV 1, 2016 TECHGOEASY.COM

Recovering from User Errors

Data Base Lab. The Microsoft SQL Server Management Studio Part-3- By :Eng.Alaa I.Haniy.

Exam : 1Z Title : Introduction to Oracle9i: SQL

ES I INFORMATICS PRACTICES Set I Class XII Sec- A,B,C Date: April 2017 Time: 1 hr 10 min M.M.: 30

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

Exam: 1Z Title : Introduction to Oracle9i: SQL. Ver :

Chapter 14 Data Dictionary and Scripting

King Fahd University of Petroleum and Minerals

Exam Actual. Higher Quality. Better Service! QUESTION & ANSWER

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

Oracle Create Table Foreign Key On Delete No

Enhancements and New Features in Oracle 12c Summarized

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

3.1. Keys: Super Key, Candidate Key, Primary Key, Alternate Key, Foreign Key

Unit 1 - Chapter 4,5

Creating and Managing Tables Schedule: Timing Topic

Oracle Alter Table Add Unique Constraint Using Index Tablespace

Microsoft MOS- Using Microsoft Office Access Download Full Version :

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 15 EXAMINATION Model Answer

Index. Boolean expression, , Business rules enforcement. see Declarative constraints table with Oracle constraints and,

N : 1 양방향관계, 저장, 검색, 삭제실습

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

TECHNOLOGY: Ask Tom. As Published In. May/June 2011

Chapter 11 How to create databases, tables, and indexes

Oracle Database Gateway for Teradata

Creating SQL Tables and using Data Types

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

Oracle 1Z Oracle9i: Program with PL/SQL. Download Full Version :

@vmahawar. Agenda Topics Quiz Useful Links

SYSTEM CODE COURSE NAME DESCRIPTION SEM

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

Oracle Login Max Length Of Column Name 11g Database

Database Design. 1-3 History of the Database. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

CHAPTER: 4 ADVANCE SQL: SQL PERFORMANCE TUNING (12 Marks)

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Database Management. This transition was facilitated by the induction of database technology into the organizations. Approaches to Data Management

Transcription:

다양한예제로쉽게배우는 오라클 SQL 과 PL/SQL 서진수저

8 장제약조건을배웁니다 1

1. 제약조건의종류 조건이름 의 미 NOT NULL 이조건이설정된컬럼에는 NULL 값이입력되지못합니다. UNIQUE 이조건이설정된컬럼에는중복된값이입력되지못합니다. PRIMARY KEY 이조건은 NOT NULL + UNIQUE 의의미를가지며테이블내에서데이터들끼리의유일성을보장하는컬럼에설정할수있으며테이블당 1 개만설정할수있습니다. FOREIGN KEY 이조건은다른테이블의컬럼을참조해서무결성검사를하게됩니다. CHECK 이조건으로설정된값만입력을허용하고나머지는거부됩니다. 2

- FOREIGN KEY 제약조건 3

2. 각제약조건의설정방법 1) 테이블생성시에동시에설정하기 SCOTT> CREATE TABLE emp3 2 ( no NUMBER(4) 3 CONSTRAINT emp3_no_pk PRIMARY KEY, 4 name VARCHAR2(10) 5 CONSTRAINT emp3_name_nn NOT NULL, 6 jumin VARCHAR2(13) 7 CONSTRAINT emp3_jumin_nn NOT NULL 8 CONSTRAINT emp3_jumin_uk UNIQUE, 9 area NUMBER(1) 10 CONSTRAINT emp3_area_ck CHECK ( area < 5 ), 11 deptno VARCHAR2(6) 12 CONSTRAINT emp3_deptno_fk REFERENCES dept2(dcode) 13 ) ; 4

- 약식으로생성하기 SCOTT> CREATE TABLE emp4 2 ( no NUMBER(4) PRIMARY KEY, 3 name VARCHAR2(10) NOT NULL, 4 jumin VARCHAR2(13) NOT NULL UNIQUE, 5 area NUMBER(1) CHECK ( area < 5 ), 6 deptno VARCHAR2(6) REFERENCES dept2(dcode) 7 ) ; 5

2) 테이블생성후추가하기 SCOTT>ALTER TABLE emp4 2 ADD CONSTRAINT emp4_name_uk UNIQUE(name) ; SCOTT>ALTER TABLE emp4 2 ADD CONSTRAINT emp4_area_nn NOT NULL(area) ; ADD CONSTRAINT emp4_area_nn NOT NULL(area) * ERROR at line 2: ORA-00904: : invalid identifier NULL <-> NOT NULL 변경방법 SCOTT>ALTER TABLE emp4 2 MODIFY (area constraint emp4_area_nn NOT NULL) ; Table altered. 6

- FOREIGN KEY 추가하기 -emp4 테이블의 no 컬럼이 emp2 테이블의 empno 컬럼의값을참조하도록참조키제약조건을설정하세요.(emp4 테이블이자식테이블입니다 ) SCOTT> ALTER TABLE emp4 2 ADD CONSTRAINT emp4_no_fk FOREIGN KEY(no) 3 REFERENCES emp2(empno) ; 7

- FOREIGN KEY 추가시주의사항 SCOTT>ALTER TABLE emp4 2 ADD CONSTRAINT emp4_name_fk FOREIGN KEY(name) 3 REFERENCES emp2(name) ; REFERENCES emp2(name) * ERROR at line 3: ORA-02270: no matching unique or primary key for this column-list SCOTT>ALTER TABLE emp2 2 ADD CONSTRAINT emp2_name_uk UNIQUE(name); Table altered. 부모테이블칼럼에 Unique 나 Pk 칼럼이어야함 SCOTT>ALTER TABLE emp4 2 ADD CONSTRAINT emp4_name_fk FOREIGN KEY(name) 3 REFERENCES emp2(name) ; Table altered. 8

- ON DELETE CASCADE FOREIGN KEY 를설정후부모테이블의데이터를지우고싶은데만약자식테이블에서부모테이블의해당데이터를참조하고있을경우지울수가없습니다. 이럴경우를대비해서 FOREIGN KEY 를생성할때설정함. 이옵션을주면부모테이블의데이터가지워지면자식테이블의데이터도함께지우라는의미임. -ON DELETE SET NULL 이옵션은부모테이블의데이터가지워질경우자식테이블의값을 NULL 로설정하라는의미임. 9

3. 제약조건관리하기 1) 제약조건 DISABLE 하기 ( NOVALIDATE ) SCOTT>INSERT INTO test_novalidate VALUES(1,'DDD'); INSERT INTO test_novalidate VALUES(1,'DDD') * ERROR at line 1: ORA-00001: unique constraint (SCOTT.SYS_C0014418) violated DISABLE NOVALIDATE Primary Key 가설정되어있는컬럼이므로중복된데이터가입력이안됩니다. SCOTT>ALTER TABLE test_novalidate 2 DISABLE NOVALIDATE CONSTRAINT SYS_C0014418 ; Table altered. SCOTT>INSERT INTO test_novalidate VALUES(1,'DDD'); 1 row created. <- 정상적으로입력됩니다. 10

- Disable VALIDATE SCOTT>INSERT INTO test_validate VALUES(4,NULL); INSERT INTO test_validate VALUES(4,NULL) * ERROR at line 1: ORA-01400: cannot insert NULL into ("SCOTT"."TEST_VALIDATE"."NAME") NAME 컬럼에 NOT NULL 제약조건이설정되어있어서 null 값을허용하지않습니다. 이 NOT NULL 제약조건을 DISABLE VALIDATE 한후다시입력해보겠습니다. SCOTT>ALTER TABLE test_validate 2 DISABLE VALIDATE CONSTRAINT tv_name_nn ; Table altered. 11

- Disable VALIDATE SCOTT>INSERT INTO test_validate VALUES(4,NULL); INSERT INTO test_validate VALUES(4,NULL) * ERROR at line 1: ORA-25128: No insert/update/delete on table with constraint (SCOTT.TV_NAME_NN) disabled and validated 여젂히입력이안됩니다. 에러내역을보면해당컬럼은 Insert/update/delete 작업을수행할수없다는내용을보여줍니다. 즉이옵션은해당컬럼의데이터를변경할수없게하는옵션입니다. 12

(3) DISABLE VALIDATE 옵션설정후다른칼럼내용변경하기 SCOTT>INSERT INTO test_validate VALUES(4,'DDD'); INSERT INTO test_validate VALUES(4,'DDD') * ERROR at line 1: ORA-25128: No insert/update/delete on table with constraint (SCOTT.TV_NAME_NN) disabled and validated -- NULL 값입력하기 SCOTT>INSERT INTO test_validate VALUES(4,NULL); INSERT INTO test_validate VALUES(4,NULL) * ERROR at line 1: ORA-25128: No insert/update/delete on table with constraint (SCOTT.TV_NAME_NN) disabled and validated 13

-- 다른컬럼에만데이터입력하기 SCOTT>INSERT INTO test_validate(no) 2 VALUES(4) ; INSERT INTO test_validate(no) * ERROR at line 1: ORA-25128: No insert/update/delete on table with constraint (SCOTT.TV_NAME_NN) disabled and validated 위테스트로 DISABLE VALIDATE 옵션은테이블의변경을막는것임을알수있습니다. 14

2) 제약조건 ENABLE 하기 - ENABLE NOVALIDATE : 기존데이터검사안하고싞규데이터만검사함 SCOTT>INSERT INTO test_enable VALUES(1,'AAA'); 1 row created. SCOTT>INSERT INTO test_enable VALUES(2,'BBB'); 1 row created. SCOTT>INSERT INTO test_enable VALUES(3,NULL); INSERT INTO test_enable VALUES(3,NULL) * ERROR at line 1: ORA-01400: cannot insert NULL into ("SCOTT"."TEST_ENABLE"."NAME") NOT NULL 제약조건때문에 3 번값이입력이안되고있습니다. 15

SCOTT>ALTER TABLE test_enable 2 DISABLE CONSTRAINT te_name_nn ; Table altered. SCOTT>INSERT INTO test_enable VALUES(3,NULL); 1 row created. 잘입력됩니다. 그러나이데이터는잘못된데이터입니다. 현재이테이블에 3 번행은 NULL 값이들어갈수가없으나제약조건을 DISABLE 해서틀린데이터가들어간것입니다. 이런상황에서제약조건을 ENABLE 하면서테스트를진행합니다. 16

(1) ENABLE NOVALIDATE 로 name 컬럼제약조건 ENABLE 하기 SCOTT>ALTER TABLE test_enable 2 ENABLE NOVALIDATE CONSTRAINT te_name_nn ; Table altered. SCOTT>SELECT * FROM test_enable ; NO NAME ---------- ---------- 1 AAA 2 BBB 3 <- 이행이잘못된데이터입니다 ENABLE NOVALIDATE 옵션은기존데이터는확인안하고싞규로입력되는데이터만확인하는옵션입니다. SCOTT>INSERT INTO test_enable VALUES(4,NULL); INSERT INTO test_enable VALUES(4,NULL) * ERROR at line 1: ORA-01400: cannot insert NULL into ("SCOTT"."TEST_ENABLE"."NAME") 17

(2) ENABLE VALIDATE 로 name 컬럼제약조건 ENABLE 하기 SCOTT>ALTER TABLE test_enable 2 DISABLE CONSTRAINT te_name_nn ; Table altered. ENABLE VALIDATE 옵션은기존데이터도체크하고싞규데이터도체크함. 현재기존데이터에문제가있어서 ENABLE 이안됨. SCOTT>ALTER TABLE test_enable 2 ENABLE VALIDATE CONSTRAINT te_name_nn ; ENABLE VALIDATE CONSTRAINT te_name_nn * ERROR at line 2: ORA-02293: cannot validate (SCOTT.TE_NAME_NN) - CHECK constraint violated 18

3) EXCEPTIONS 테이블을사용하여 ENABLE VALIDATE 하기 실습은교재 295 297 페이지를참고하세요 19

4) 제약조건조회하기 SCOTT>SELECT owner, constraint_name, constraint_type, status 2 FROM user_constraints 3 WHERE table_name='emp4'; 위칼럼중에 constraint_type 의타입값중 P: Primary Key, U :Unique, C: CHECK, R: 외래키를의미합니다. 20

- 사용예 2: FOREIGN KEY 조회하기 SCOTT> SELECT a.table_name "Child_Table", 2 c.column_name "Child_Column", 3 a.constraint_name "Child_Cons_name", 4 b.table_name "Parent_Table", 5 a.r_constraint_name "Parent_Cons_name", 6 d.column_name "Parent_Column" 7 FROM user_constraints a, user_constraints b, user_cons_columns c, 8 (SELECT constraint_name, column_name, table_name 9 FROM user_cons_columns) d 10 WHERE a.r_constraint_name=b.constraint_name 11 AND a.constraint_name=c.constraint_name 12 AND a.r_constraint_name=d.constraint_name 13 AND a.constraint_type='r' ; 21

5) 제약조건삭제하기 SCOTT>ALTER TABLE emp4 2 DROP CONSTRAINT emp4_name_fk ; 22