Keys are fields in a table which participate in below activities in RDBMS systems:

Similar documents
DC62 Database management system JUNE 2013

Downloaded from

Handout 4. Logical Database Modeling, Part 1: Relational Data Model. Transforming EER model to Relational.

SQL Server 2008 Tutorial 3: Database Creation

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

Conceptual Design. The Entity-Relationship (ER) Model

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

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

The Relational Model

Exam code: Exam name: Database Fundamentals. Version 16.0

Chapter 1 SQL and Data

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Unit 1 - Chapter 4,5

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

8) A top-to-bottom relationship among the items in a database is established by a

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

Structure Query Language (SQL)

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Relational Model (cont d) & Entity Relational Model. Lecture 2

Referential Integrity and Other Table Constraints Ray Lockwood

DBS Assignment I. 1. With necessary examples, explain any four major disadvantages of keeping organizational information in a file-processing system.

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

Lecture 07. Spring 2018 Borough of Manhattan Community College

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

ALTER TABLE Statement

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Physical Design of Relational Databases

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

MTA Database Administrator Fundamentals Course

Basic SQL. Basic SQL. Basic SQL

Translating an ER Diagram to a Relational Schema

Introduction to SQL Server 2005/2008 and Transact SQL

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

SQL Interview Questions

Full file at

SQL Data Definition and Data Manipulation Languages (DDL and DML)

MySQL Introduction. By Prof. B.A.Khivsara

Introduction to Database Systems CSE 414

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

Relational Model. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Chapter 6: Entity-Relationship Model. The Next Step: Designing DB Schema. Identifying Entities and their Attributes. The E-R Model.

Every Byte Counts. Why Datatype Choices Matter. Andy Yun, Database Architect/Developer

The Next Step: Designing DB Schema. Chapter 6: Entity-Relationship Model. The E-R Model. Identifying Entities and their Attributes.

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

Introduction to Databases

Chapter 6: Entity-Relationship Model

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

Chapter 6. Advanced Data Modeling. Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

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

CMP-3440 Database Systems

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Database Systems. Answers

Database Management Systems

COSC Assignment 2

Database Logical Design

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

ER Model. Objectives (2/2) Electricite Du Laos (EDL) Dr. Kanda Runapongsa Saikaew, Computer Engineering, KKU 1

Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition. Chapter 8 Data Modeling Advanced Concepts

Homework 2. Develop an EER model for the following situation using the EER diagram,

Database Programming with SQL

Introduction to Data Management CSE 344

Assignment Grading Rubric

Course Prerequisites: This course requires that you meet the following prerequisites:

Relational Database Systems Part 01. Karine Reis Ferreira

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

SQL Commands & Mongo DB New Syllabus

Introduction to relational databases and MySQL

CS2300: File Structures and Introduction to Database Systems

Item: 1 (Ref:Cert-1Z )

Slides by: Ms. Shree Jaswal

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

SQL STRUCTURED QUERY LANGUAGE

Database Applications (15-415)

From ER to Relational Model. Book Chapter 3 (part 2 )

Database Technology Introduction. Heiko Paulheim

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

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

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

Data Modeling with the Entity Relationship Model. CS157A Chris Pollett Sept. 7, 2005.

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

How to use SQL to create a database

Interview Questions on DBMS and SQL [Compiled by M V Kamal, Associate Professor, CSE Dept]

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

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Lab # 4. Data Definition Language (DDL)

Chapter 2: Entity-Relationship Model

CS 377 Database Systems

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

Objectives Definition iti of terms List five properties of relations State two properties of candidate keys Define first, second, and third normal for

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

Employment Information System (EIS) File Format Layouts Last update September 25, 2017

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

/* --6). Name: Christopher Singleton Date: 02/12/2017 Class: PROG 140 Project: Module 04

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

Transcription:

Keys are fields in a table which participate in below activities in RDBMS systems: 1. To create relationships between two tables. 2. To maintain uniqueness in a table. 3. To keep consistent and valid data in database. 4. Might help in fast data retrieval by facilitating indexes on column(s). SQL Server supports various types of keys, which are listed below: 1. Candidate Key 2. Primary Key 3. Unique Key 4. Alternate Key 5. Composite Key 6. Super Key 7. Foreign Key

Candidate Key Candidate key is a key of a table which can be selected as a primary key of the table. A table can have multiple candidate keys, out of which one can be selected as a primary key. Example: Employee_Id, License_Number and Passport_Number are candidate keys Primary Key Primary key is a candidate key of the table selected to identify each record uniquely in table. Primary key does not allow null value in the column and keeps unique values throughout the column. In above example, Employee_Id is a primary key of Employee table. In SQL Server, by default primary key creates a clustered index on a heap tables (a table which does not have a clustered index is known as a heap table). We can also define a nonclustered primary key on a table by defining the type of index explicitly. A table can have only one primary key and primary key can be defined in SQL Server using below SQL statements: 1. CRETE TABLE statement (at the time of table creation) In this case, system defines the name of primary key 2. ALTER TABLE statement (using a primary key constraint) User defines the name of the primary key Example: Employee_Id is a primary key of Employee table. Unique Key Unique key is similar to primary key and does not allow duplicate values in the column. It has below differences in comparison of primary key: 1. It allows one null value in the column. 2. By default, it creates a nonclustered index on heap tables. Alternate Key Alternate key is a candidate key, currently not selected as primary key of the table. Example: License_Number and Passport_Number are alternate keys.

Composite Key Composite key (also known as compound key or concatenated key) is a group of two or more columns that identifies each row of a table uniquely. Individual column of composite key might not able to uniquely identify the record. It can be a primary key or candidate key also. Example: In salary table, Employee_Id and Salary_Month_Year are combined together to identify each row uniquely in Salary table. Independently Employee_Id or Salary_Month_Year column cannot identify each row uniquely. We can create a composite primary key on Salary table using Employee_Id and Salary_Month_Year columns. Super Key Super key is a set of columns on which all columns of the table are functionally dependent. It is a set of columns that uniquely identifies each row in a table. Super key may hold some additional columns which are not strictly required to uniquely identify each row. Primary key and candidate keys are minimal super keys or you can say subset of super keys. In above example, In Employee table, column Employee_Id is sufficient to uniquely identify any row of the table, so that any set of column from Employee table which contains Employee_Id is a super key for Employee Table. For example: {Employee_Id}, {Employee_Id, Employee_Name}, {Employee_Id, Employee_Name, Address} etc. License_Number and Passport_Number columns can also identify any row of the table uniquely. Any set of column which contains License_Number or Passport_Number or Employee_Id is a super key of the table. For example: {License_Number, Employee_Name, Address}, {License_Number, Employee_Name, Passport_Number}, {Passport_Number, Employee_Name, Address, License_Number}, {Passport_Number, Employee_Name}, {Passport_Number, Employee_Id} etc. Foreign Key In a relationship between two tables, a primary key of one table is referred as a foreign key in another table. Foreign key can have duplicate values in it and can also keep null values if column is defined to accept nulls. Example: Employee_Id (primary key of Employee table ) is a foreign key in Salary table.

Natural key. A key that is formed of attributes that already exist in the real world. For example, U.S. citizens are issued a Social Security Number (SSN) that is unique to them (this isn't guaranteed to be true, but it's pretty darn close in practice). SSN could be used as a natural key, assuming privacy laws allow it, for a Person entity (assuming the scope of your organization is limited to the U.S.). Surrogate key. A key that has no business meaning. An example of which is the AddressID column of the Address table in Figure 1. Addresses don't have an "easy" natural key because you would need to use all of the columns of the Address table to form a key for itself (you might be able to get away with just the combination of Street and ZipCode depending on your problem domain), therefore introducing a surrogate key is a much better option in this case. Create Commands 1. Primary Key : A. Create Primary Key 2. CREATE TABLE CUSTOMERS( 3. ID INT NOT NULL, 4. NAME VARCHAR (20) NOT NULL, 5. AGE INT NOT NULL, 6. ADDRESS CHAR (25), 7. SALARY DECIMAL (18, 2), 8. PRIMARY KEY (ID) ); B. Add primary key in existing Table ALTER TABLE CUSTOMER ADD PRIMARY KEY (ID); C. Add more than one column as Primary key ALTER TABLE CUSTOMERS ADD CONSTRAINT PK_CUSTID PRIMARY KEY (ID, NAME); D. Delete primary key ALTER TABLE CUSTOMERS DROP PRIMARY KEY ;

1. Foreign Primary Key : A. Create Key CREATE TABLE ORDERS ( ID INT NOT NULL, DATE DATETIME, CUSTOMER_ID INT references CUSTOMERS(ID), AMOUNT double, PRIMARY KEY (ID) ); B. Add key in existing Table ALTER TABLE ORDERS ADD FOREIGN KEY (Customer_ID) REFERENCES CUSTOMERS (ID); D. Delete key ALTER TABLE ORDERS DROP FOREIGN KEY;