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

Similar documents
Creating SQL Tables and using Data Types

School of Computing, Engineering and Information Sciences University of Northumbria. Set Operations

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

SQL Structured Query Language Introduction

SQL. Char (30) can store ram, ramji007 or 80- b

Downloaded from

Notes. CS 640 Relational Algebra Winter / 16. Notes. CS 640 Relational Algebra Winter / 16. Notes

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

An Introduction to Structured Query Language

CS430 Final March 14, 2005

Sample Question Paper

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

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

Creating and Managing Tables Schedule: Timing Topic

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

DATABASE DEVELOPMENT (H4)

CS2 Current Technologies Note 1 CS2Bh

An Introduction to Structured Query Language

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

An Introduction to Structured Query Language

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

After completing this unit, you should be able to: Use inner joins to retrieve data from more than one table

Programming the Database

Unit 1 - Chapter 4,5

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

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

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

Table Joins and Indexes in SQL

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

CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

Tables From Existing Tables

Similarity Flooding: A versatile Graph Matching Algorithm and its Application to Schema Matching

An Introduction to Structured Query Language

Database implementation Further SQL

5 Integrity Constraints and Triggers

An Introduction to Structured Query Language

Oracle Tables TECHGOEASY.COM

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

INDEX. 1 Introduction. 2 Types of Cursors 2.1 Explicit cursor 2.2 Attributes 2.3 Implicit cursor 2.4 Attributes. 3 Parameterized cursor

Slides by: Ms. Shree Jaswal

Database Management Systems,

ALTER TABLE Statement

CIS Slightly-different version of Week 10 Lab, also intro to SQL UPDATE and DELETE, and more

Babu Madhav Institute of Information Technology 2015

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Visit for more.

Microsoft MOS- Using Microsoft Office Access Download Full Version :

Practice for Test 1 (not counted for credit, but to help you prepare) Time allowed: 1 hour 15 minutes

CS2300: File Structures and Introduction to Database Systems

Database Laboratory Note (MS SQL Server 2000)

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

Databases IIB: DBMS-Implementation Exercise Sheet 13

Lecture Handout. Database Management System. Overview of Lecture: Normalization Summary. Lecture No. 21. Reading Material

Database Systems Laboratory 5 Grouping, DCL & TCL

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Advance Database Management System

ENHANCING DATABASE PERFORMANCE

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

Illustrative Example of Logical Database Creation

Semantic Errors in Database Queries

Chapter-14 STRUCTURES

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

The Relational Model

SELF TEST. List the Capabilities of SQL SELECT Statements

Create Rank Transformation in Informatica with example

Outer Join, More on SQL Constraints

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Translating Entity-Relationship to Relational Tables

Name: Sample Answers Q.2(c) 7 7

Chapter 2. DB2 concepts

Suppose we need to get/retrieve the data from multiple columns which exists in multiple tables...then we use joins..

Illustrative Example of Logical Database Creation

CS6312 DATABASE MANAGEMENT SYSTEMS LABORATORY L T P C

The Relational Model Constraints and SQL DDL

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

ACS-2914 Normalization March 2009 NORMALIZATION 2. Ron McFadyen 1. Normalization 3. De-normalization 3

Overview of PL/SQL. About PL/SQL. PL/SQL Environment. Benefits of PL/SQL. Integration

E-R Diagram to Relational Schema. Translating Entity-Relationship to Relational Tables. Representing Weak Entity Sets. Representing Strong Entity Sets

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

1) Introduction to SQL

IBM A Assessment: DB2 9 Fundamentals-Assessment. Download Full Version :

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

Downloaded from

Downloaded from

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

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

Arbori Starter Manual Eugene Perkov

Q.1 Short Questions Marks 1. New fields can be added to the created table by using command. a) ALTER b) SELECT c) CREATE. D. UPDATE.

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

COSC344 Database Theory and Applications. Lecture 11 Triggers

Translating Entity-Relationship to Relational Tables

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

Enhanced Data Models for Advanced Applications. Active Database Concepts

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

Database Management Systems,

Advanced SQL and the Power of Rewriting Queries

MySQL Introduction. By Prof. B.A.Khivsara

Lecture 07. Spring 2018 Borough of Manhattan Community College

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

Transcription:

CM0719: Database Modelling Seminar 5 (b): Candidate and Primary Keys Exercise Aims: To understand the concept of candidate and primary keys and their application in table creation. Outline of Activity: 1. Declare candidate and primary keys for the tables previously established. The following pages give you detailed instructions for performing this task.

In a relational database, each table (relation) must have at least one Candidate Key. Remember, tuples must have unique values on the Candidate Key(s) at all times. Exercise 2.2: What Candidate Key(s) can you identify for the EMP table? and for the DEPT table? and for the PROJ table? One Candidate Key per table is selected to be the Primary Key, and an appropriate constraint is set up in Oracle 11g. However, if there are any other Candidate Keys (Alternate Keys), it is possible, and indeed desirable, to constrain them also to have unique values. SETTING PRIMARY AND ALTERNATE KEYS IN SQL The CHECK constraint can be used to implement the PRIMARY KEY constraint. As with other types of constraint, each one should be given a name, and it is helpful if we adopt a consistent convention. We shall use names of the form PKEYunderscore-tablename, e.g. PKEY_EMP for the EMP table. Implement the constraint by dropping the existing EMP table from the database and then re-creating it with a modified CREATE TABLE statement (the columns, the datatypes and the other constraints are as shown above) SQL> DROP TABLE EMP; Table dropped. SQL> CREATE TABLE EMP 2 (EMP_NO CHAR(2) 3 CONSTRAINT PKEY_EMP PRIMARY KEY, 4 EMP_NAME CHAR(10), 5 SALARY NUMBER(10), 6 MARITAL_STATUS CHAR(1) 7 ); Table created. In the case of the EMP table, there was only one Candidate Key, which has now become the Primary Key. DEPT on the other hand has two Candidate Keys, DEPT_NO (the obvious one) and also DEPT_NAME. Although a company could conceivably have two departments with the same name, it clearly does not make sense to give the same name to more than one Department. We will make DEPT_NO the Primary Key, but will also set a UNIQUE constraint on the Alternate Key, DEPT_NAME:

Exercise 2.3: You should complete and run the following SQL statement to recreate the DEPT table. What else do you need to do first? SQL> CREATE TABLE DEPT 2 (DEPT_NO CHAR(2) 3 CONSTRAINT PKEY_DEPT PRIMARY KEY, 4 DEPT_NAME CHAR(10) 5 CONSTRAINT UNIQ_DEPT_NAME UNIQUE, : : ADDING CONSTRAINTS TO AN EXISTING ORACLE TABLE You will probably have decided by now that it is exceedingly cumbersome to drop and re-create a table whenever an additional constraint needs to be added. Fortunately, SQL provides an ALTER TABLE statement which, among other uses, enables us to add constraints more simply. A partial description of its syntax is: ALTER TABLE table-name <add clause> ; where the add clause may be defined, again partially, as: ADD ( {<table constraint> } ) Constraints are dealt with more generally in the exercises for week 3. The important thing to note for present purposes is that we must specify which column(s) of the table a PRIMARY KEY or UNIQUE constraint refers to. Here is an example to try out, in which an additional constraint is added to the DEPT table. It happens to be company policy that no-one can manage more than one department at the same time hence the MANAGER_NO column in DEPT has unique values. That does not in fact make MANAGER_NO another Alternate Key (for reasons which will become apparent later), but it does allow us to set a UNIQUE constraint:

SQL> ALTER TABLE DEPT 2 ADD CONSTRAINT UNIQ_MANAGER UNIQUE (MANAGER_NO); Table altered. COMPOSITE KEYS IN SQL Candidate Keys do not always consist of just one column. When we have a choice, we normally select the smallest candidate to be the Primary Key, but nevertheless it sometimes happens that we end up with a composite Primary Key of more than one column. In such a case it is not possible to set the Primary Key by means of a column constraint; we are obliged to use a table constraint even when setting the key at the same time as we create the table. For this purpose we need to use the extension of the CREATE TABLE syntax that we saw in Exercise 2, when setting a table constraint to prevent the DEADLINE for a project being before it s START_DATE: CREATE TABLE table-name ( <column definition> [, etc.] [, <table constraint> [, etc.] ] ) ; This indicates that the column definitions may optionally be followed, still within brackets, by one or more table constraints, separated from the column definitions and from each other by commas. By way of an example, here is an additional table for your database, to indicate which employees are working on which projects: Table Column Name Description Data-type Name ALLOC EMP_NO An Employee s Number. CHAR(2) PROJ_NO The Number of a Project. CHAR(2) Exercise 2.4: Why is EMP_NO on its own not a Candidate Key? Why is PROJ_NO not a Candidate Key? Hence the only Candidate Key consists of both columns. Set up the table as follows:

SQL> CREATE TABLE ALLOC 2 (EMP_NO CHAR(2), 3 PROJ_NO CHAR(2), 4 CONSTRAINT PKEY_ALLOC PRIMARY KEY (EMP_NO, PROJ_NO) 5 );