Various Key Constraints in RDBMS

Dr. V. V. Subrahmanyam Associate Professor, SOCIS, IGNOU Relational Schema

• A list of attributes that describe the structure of a . • A relation is a set of tuples that conform to the schema. • Examples: – SALESPERSON(SSN, NAME, YEAR_OF_JOIN, DEPT) – STUDENT(SNO, SNAME, MAJOR, DOB) – BOOK(ISBN, BOOK_TITLE, PUBLISHER, AUTHOR, YEAR_OF_PUB) RELATIONAL DB and RELATIONAL DB SCHEMA

• A relational is a collection of relations with distinct relation names. • The schema is the collection of schemas for the relations in the database. RELATIONAL DB SCHEMA – AN EXAMPLE • EMPLOYEE(FNAME, LNAME, SSN, DOB, ADD1, ADD2, SEX, SALARY, SUPERSSN, DEPTNO) • DEPARTMENT(DNAME,DEPTNO, MGRSSN) • DEPT_LOCATIONS(DEPTNO, DLOCATION) • WORKS_ON(ESSN, PNO,HOURS) • PROJECT(PNAME, PNO,PLOCATION, DEPTNO) • DEPENDENT(ESSN, DEPND_NAME, SEX, DOB, REALTIONSHIP) Constraints

• Various restrictions on that can be specified on a relational database in the form of constraints. • These include: – Domain Constraints – Key Constraints – Integrity Constraints – () Contd..

• Others – Data Dependencies – Functional Dependencies Domain Constraints

• Domain Constraints specify that the value of each attribute A must be an atomic value from the domain Integrity Constraints

• It is a condition that is specified on a database schema, an restricts the data that can be stored in an instance of the database. • A DBMS enforces the integrity constraints. Contd…

• These are specified and enforced at different times: – When the DBA or end user defines a DB schema, s/he specifies them. – When a DB application is run, the DBMS checks for violations and disallows changes to the data that violate the specified Integrity constraints. Example

• No two students have the same StudentID value. • No two employees can have the same Social Security Number(SSN) • No two citizens of India can have same Aadhaar card Number. Key Constraints

• A key constraint is a statement that a certain minimal subset of the fields of a relation is a unique identifier for a tuple. Various Key Constraints

• Super Key • • Foreign Key • • Alternate key • Secondary Key Consider…

• Employee (ENo, EName, Address, DeptID, Salary) • Department(DeptID, DName, MngrID, Location_ID) Super Key

• Super Key is a set of one or more than one key that can be used to identify a record uniquely in a . • Primary key, Unique Key, Alternate Key are subset of Super keys. • A table can have many Super keys. Example • In Employee Table, i. ENo ii. ENo, EName iii. ENo, Address iv. ENo, DeptID v. ENo, Salary vi. EName, Address vii. Ename, Address, DeptID Any combination shown above can identify the records uniquely will be a Super Key.

Candidate Key

• It is a set of one or more fields/columns that can identify a record uniquely in a table. • There can be multiple candidate keys in one table. • Each candidate key can work as a Primary key. • It can be defined as minimal Super Key or irreducible Super Key.

Contd…

Candidate Keys are: I. ENo II. EName, Address ENo can identify the record uniquely and similarly combination of EName and Address can identify the record Uniquely. Primary Key

• Primary key is a set of one or more fields of a table that uniquely identify a record in database table. • It can not accept NULL, duplicate values. • Only one candidate key can be Primary Key • In the example of candidate keys, ENo will be the Primary Key. Alternate Key

• Alternate key can be any of the candidate keys except for the Primary Key. • In the above example of candidate keys, as ENo is identified as primary key, “EName, Address” is an alternate key Foreign Key • Sometimes the information stored in a relation is linked to the information stored in another relation. • If one relation is modified, the other must be checked, and perhaps modified, to keep the data consistent. • The most common Integrity constraint involving two relations is a foreign key constraint.

Contd…

• A foreign key is an attribute or combination of attributes in one base table that points to the candidate key of another table. • In the below example, DeptID of Employee is Foreign Key Employee (ENo, EName, Address, DeptID, Salary) Department(DeptID, DName, MngrID, Location_ID)

Composite Key

• It is a combination of more than one field or of a table to form a candidate key or a primary key. • If we use “Name, Address” as a primary key then it is called as the Composite primary key. Secondary Key

• The attributes those are not the Super Keys, but still can identify the records (not uniquely) • Example: Any key EName, Address, Salary, DeptID, as they can identify the records but they might not be unique. Unique Key

• Unique Key is a set of one or more fields/columns of a table that uniquely identify a record in database table. • It is like Primary key, but it can accept a NULL value and it cannot have duplicate values. Functional Dependencies

• A Functional Dependency describes a relationship between attributes within a single relation. • An attribute is functionally dependent on another if we can use the value of one attribute to determine the value of another.

• Example: Employee_Name is functionally dependent on Social_Security_Number because Social_Security_Number can be used to uniquely determine the value of Employee_Name. • We use the arrow symbol → to indicate a functional dependency. • X → Y is read as, X functionally determines Y • Student_ID → Student_Major • Student_ID, CourseNumber, Semester → Grade • Course_Number, Section → Professor, Classroom, NumberOfStudents • CarModel, Options, TaxRate → Car_Price