DATA CONSTRAINT. Prepared By: Dr. Vipul Vekariya

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

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

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

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

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

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Database Programming - Section 10. Instructor Guide

Database Programming with SQL

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

Data Manipulation Language

RETRIEVING DATA USING THE SQL SELECT STATEMENT

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

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

CHAPTER4 CONSTRAINTS

ÇALIŞMA TEST SORULARI

HR Database. Sample Output from TechWriter 2007 for Databases

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

Data Definition Language (DDL)

Série n 6 Bis : Ateliers SQL Data Modeler (Oracle)

Oracle Database 10g: SQL Fundamentals I. Oracle Internal & Oracle Academy Use Only. Student Guide Volume 2. D17108GC30 Edition 3.0 January 2009 D57871

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Assignment Grading Rubric

Using the Set Operators. Copyright 2004, Oracle. All rights reserved.

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Lecture 07. Spring 2018 Borough of Manhattan Community College

Oracle Database SQL Basics

Intermediate SQL: Aggregated Data, Joins and Set Operators

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Appendix A Practices and Solutions

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

CS2300: File Structures and Introduction to Database Systems

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

School of Computing and Information Technology Session: Spring CSCI835 Database Systems (Bridging Subject) Sample class test 23 July 2018

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

EXISTS NOT EXISTS WITH

Retrieving Data from Multiple Tables

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

Tables From Existing Tables

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

SQL DATA MANIPULATION. Prepared By: Vipul Vekariya M.E( Computer) Gardi College Of Eng. & Tech. Rajkot.

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

SQL DATA MANIPULATION. Prepared By: Dr. Vipul Vekariya.

Introduction to Oracle9i: SQL

Database Programming with SQL

Downloaded from

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

Oracle MOOC: SQL Fundamentals

School of Computing and Information Technology. Examination Paper Autumn Session 2017

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

MySQL Introduction. By Prof. B.A.Khivsara

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

Creating Other Schema Objects. Copyright 2004, Oracle. All rights reserved.

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Vendor: Oracle. Exam Code: 1Z Exam Name: Oracle Database: SQL Fundamentals I. Q&As: 292

Exam : 1Z Title : Introduction to Oracle9i: SQL

From theory to practice. Designing Tables for a postgresql Database System. psql. Reminder. A few useful commands

3ISY402 DATABASE SYSTEMS

RMOUG Training Days 2018

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

Structure Query Language (SQL)

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

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Sun Certified MySQL Associate

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

Query Optimization on Compressed and Decompressed Object-Oriented Database Using Operators

Oracle Database 10g: SQL Fundamentals I

Oracle 1z z0-146 Oracle Database 11g: Advanced PL/SQL. Practice Test. Version QQ:

Designing Tables for an Oracle Database System. From theory to practice

Unit 1 - Chapter 4,5

KORA. RDBMS Concepts II

ALTER TABLE Statement

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

SELF TEST. List the Capabilities of SQL SELECT Statements

Join, Sub queries and set operators

Introduction to Relational Database Concepts. Copyright 2011, Oracle. All rights reserved.

Database Programming with PL/SQL

IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://

Using DbVisualizer Variables

Index. Defination of Constraint Column level Table level Data Constraint A Different Constraint

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

From theory to practice. Designing Tables for an Oracle Database System. Sqlplus. Sqlplus. Technicalities. Add the following to your.

RESTRICTING AND SORTING DATA

Handout 6 CS-605 Spring 18 Page 1 of 7. Handout 6. Physical Database Modeling

Oracle 1Z0-007 Introduction to Oracle9i: SQL 210 Q&A

SQL Data Definition Language

Example 1 - Create Horizontal View. Example 2 - Create Vertical View. Views. Views

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

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


RELATIONAL DATA MODEL

2. What privilege should a user be given to create tables? The CREATE TABLE privilege

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

Transcription:

DATA CONSTRAINT Prepared By: Dr. Vipul Vekariya

What is constraint? Constraints enforce rules at the table level. Constraints prevent the deletion of a table if there are dependencies. There are two types of data constraint. 1) I/O constraint 2) business rule constraint

The Primary Key Constraint A primary key is used to uniquely identify each row in table. It may be one or more column in a table. A table can have only one primary key. A primary key column in a table has special attributes. It defines the column, as a mandatory column ( column can not left blank). As the not null attribute active. The data held across the column must be unique. A single column primary key is called simple key. A multicolumn primary key is called a composite primary key.

Features of primary key It used for uniquely identify a row. It will not allow the duplicate values. It will not allow the null values. It is not compulsory but it is recommended. Only one primary key allowed per table. One table can combine up to 16 columns in a composite primary key.

Syntax of primary key <column name> <data type>(<size>) primary key Primary key at column level. Example: CREATE TABLE CUST_MSTR ( "CUST_NO" VARCHAR2(10) PRIMARY KEY, "FNAME" VARCHAR2(25), "MNAME" VARCHAR2(25), "FORM60" VARCHAR2(1));

Primary key at table level Example: CREATE TABLE FD_MSTR( "FD_SER_NO" VARCHAR2(10), "MANAGER_SIGN" VARCHAR2(1), PRIMARY KEY(FD_SER_NO, CORP_CUST_NO));

The Foreign key Foreign key represent relationship between two tables. It is a column or group of column whose value are derived from the primary key or unique key of some other table. The table in which foreign key is defined is called foreign table. The table that defines the primary or unique key and is referenced by foreign key is called the primary table. It defines at create table statement or alter table statement.

Features of Foreign Key Foreign key is column that references a column of a table. Parent record can be delete provided if no child record exit. Master table cannot be updated if child record exit. This constraint establish relationship between records(column data). Across master and detail table. Record can not be inserted into detail table if corresponding record in master table do not exist. Record of the master table cannot be deleted if the corresponding record in the detail table actually exist.

Syntax of foreign key <column name> <data type>(<size>) REFRENCES <table name> [(<column name>)] Example: CREATE TABLE EMP_MSTR( "EMP_NO" VARCHAR2(10) PRIMARY KEY, "BRANCH_NO" VARCHAR2(10) REFERENCES BRANCH_MSTR, "FNAME" VARCHAR2(25), "DESIG" VARCHAR2(30));

Foreign key at table level foreign key (<column name> [,<column name>] ) references <table name> [(<column name>,<column name>)] Example: CREATE TABLE ACCT_FD_CUST_DTLS( "ACCT_FD_NO" VARCHAR2(10), "CUST_NO" VARCHAR2(10), FOREIGN KEY (CUST_NO) REFERENCES CUST_MSTR(CUST_NO));

Assigning user defined name to constraints CREATE TABLE departments (department_id NUMBER(4), department_name VARCHAR2(30), manager_id NUMBER(6), location_id NUMBER(4), CONSTRAINT dept_id_pk PRIMARY KEY(department_id));

CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE NOT NULL, department_id NUMBER(4), CONSTRAINT emp_dept_fk FOREIGN KEY (department_id) REFERENCES departments(department_id), CONSTRAINT emp_email_uk UNIQUE(email));

Adding a Constraint Syntax Use the ALTER TABLE statement. Add or drop a constraint, but not modify its structure Enable or disable constraints ALTER TABLE tablename ADD [CONSTRAINT NAME] constraint type (column); ALTER TABLE employees ADD PRIMARY KEY(employee_no); Table altered.

Dropping a Constraint Remove the manager constraint from the EMPLOYEES table. ALTER TABLE employees DROP CONSTRAINT emp_manager_fk; Table altered ALTER TABLE departments DROP PRIMARY KEY; Table altered.

Disabling Constraints Execute the DISABLE clause of the ALTER TABLE statement to deactivate an integrity constraint. Example: ALTER TABLE employees DISABLE CONSTRAINT emp_id_pk; Table altered.

Enabling Constraints Activate an integrity constraint currently disabled in the table definition by using the ENABLE clause. Example: ALTER TABLE employees ENABLE CONSTRAINT emp_id_pk; Table altered.

Viewing Constraints Query the USER_CONSTRAINTS table to view all constraint definitions and names. Example: SELECT constraint_name, constraint_type, search_condition FROM user_constraints WHERE table_name= branch_mstr ;

Result of Previous query

Viewing the Columns Associated with Constraints View the columns associated with the constraint names in the USER_CONS_COLUMNS view. Example: SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = 'EMPLOYEES';

Result of Previous query

Unique key constraint Unique key constraint permits the multiple NULL into column. The NULL values are clubbed at the top of the column. This is difference between primary key and unique key. Unique key will not allow duplicate values. Unique index is created automatically. A table can have more than one unique key which is not possible in primary key. Unique key can combine up to 16 columns in a composite unique key. Unique key can not be LONG or LONG RAW data type.

Example of unique key. CREATE TABLE CUST_MSTR ( "CUST_NO" VARCHAR2(10) UNIQUE, "FNAME" VARCHAR2(25), "MNAME" VARCHAR2(25), "LNAME" VARCHAR2(25), "DOB_INC" DATE, "OCCUP" VARCHAR2(25), "PHOTOGRAPH" VARCHAR2(25), "SIGNATURE" VARCHAR2(25), "PANNO" VARCHAR2(1) UNIQUE, \ "FORM60" VARCHAR2(1));

Business Rule Constraints Check Constraint: Syntax: CHECK (<logical expression>) Example: CREATE TABLE CUST_MSTR( "CUST_NO" VARCHAR2(10) CHECK(CUST_NO LIKE C% ), "FNAME" VARCHAR2(25) CHECK (FNAME = upper(fname)), "MNAME" VARCHAR2(25) CHECK (MNAME = upper(mname)), "LNAME" VARCHAR2(25) CHECK (LNAME = upper(lname)), "DOB_INC" DATE, "OCCUP" VARCHAR2(25), "PHOTOGRAPH" VARCHAR2(25), "SIGNATURE" VARCHAR2(25), "PANCOPY" VARCHAR2(1));

NULL values Concept Setting NULL value is appropriate when the actual value is unknown. A NULL value is not equivalent to zero or space. A NULL value will evaluate to NULL if any expression. NULL value can be inserted into column of any data type. If the column has NULL value, oracle ignores any constraint that match with that column.

NOT NULL constraints NOT NULL concept means column can not be left empty. syntax: <column name> < data type>(<size>) NOT NULL Example: CREATE TABLE employees( employee_id NUMBER(6), Last_name VARCHAR2(25) NOT NULL, salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE NOT NULL);

Default value Syntax: <column name> < data type>(<size>) Default<value>; Example: CREATE TABLE ACCT_MSTR ("ACCT_NO" VARCHAR2(10), "SF_NO" VARCHAR2(10), "LF_NO" VARCHAR2(10), "BRANCH_NO" VARCHAR2(10), "INTRO_CUST_NO" VARCHAR2(10), "STATUS" VARCHAR2(1) DEFAULT 'A');