Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53
UNIT- II
Constraints and Keys
Integrity Constraints Integrity constraints in a Database Management System (DBMS) are rules that ensure the accuracy and consistency of data within a database. They enforce data integrity, ensuring that the data entered into the database adheres to certain standards and rules. Here are the main types of integrity constraints commonly used in DBMS: 1. Domain Integrity: – Domain Constraint: Ensures that all values in a column fall within a certain range or set of values. For example, a column for ages might be constrained to values between 0 and 150. – NOT NULL Constraint: Ensures that a column cannot have a NULL value. This is used to ensure that a field always has a value. • Example: CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, Name VARCHAR(100) NOT NULL, Age INT CHECK (Age >= 18 AND Age <= 65), Email VARCHAR(100) UNIQUE ); INSERT INTO Employees (EmployeeID, Name, Age, Email) VALUES (1, 'Alice Johnson', 30, '[email protected]'), (2, 'Bob Smith', 45, '[email protected]'), (3, 'Charlie Brown', 25, '[email protected]'), (4, 'Diana Prince', 35, '[email protected]'); 2. Entity Integrity: • Primary Key Constraint: Ensures that a column (or a set of columns) has unique values across all rows in the table and that none of the values are NULL. This uniquely identifies each row in a table. • Unique Constraint: Ensures that all the values in a column are unique. Unlike the primary key, a table can have multiple unique constraints. • Example: CREATE TABLE Departments ( DepartmentID INT PRIMARY KEY, DepartmentName VARCHAR(100) NOT NULL ); 3. Referential Integrity: • Foreign Key Constraint: Ensures that the value in a column (or a set of columns) matches the value in a column of another table, maintaining a relationship between the two tables. This is used to maintain the integrity of data across different tables. • Example: • CREATE TABLE EmployeeDepartment ( EmployeeID INT, DepartmentID INT, FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID), FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ); 4. User-Defined Integrity: • Check Constraint: Allows for more complex rules that cannot be enforced by the basic integrity constraints. It specifies a condition that each row must satisfy. For example, a check constraint might ensure that the salary of an employee is always greater than zero. • Example: CREATE TABLE Orders ( OrderID INT PRIMARY KEY, OrderDate DATE NOT NULL, TotalAmount DECIMAL(10, 2) CHECK (TotalAmount > 0) ); Keys in Relational Model
Keys are one of the basic requirements of a relational
database model. It is widely used to identify the tuples(rows) uniquely in the table. We also use keys to set up relations amongst various columns and tables of a relational database. Different Types of Database Keys 1. Primary key • It is the first key used to identify one and only one instance of an entity uniquely. The key which is most suitable from those lists becomes a primary key. • In the EMPLOYEE table, ID can be the primary key since it is unique for each employee. In the EMPLOYEE table, we can even select License_Number and Passport_Number as primary keys since they are also unique. 2. Candidate key • A candidate key is an attribute or set of attributes that can uniquely identify a tuple. • Except for the primary key, the remaining attributes are considered a candidate key. The candidate keys are as strong as the primary key. • For example: In the EMPLOYEE table, id is best suited for the primary key. The rest of the attributes, like SSN, Passport_Number, License_Number, etc., are considered a candidate key. 3. Super Key • Super key is an attribute set that can uniquely identify a tuple. A super key is a superset of a candidate key. 4. Foreign key • Foreign keys are the column of the table used to point to the primary key of another table. • Every employee works in a specific department in a company, and employee and department are two different entities. So we can't store the department's information in the employee table. That's why we link these two tables through the primary key of one table. 5. Alternate key • There may be one or more attributes or a combination of attributes that uniquely identify each tuple in a relation. These attributes or combinations of the attributes are called the candidate keys. In other words, the total number of the alternate keys is the total number of candidate keys minus the primary key. For example, employee relation has two attributes, Employee_Id and PAN_No, that act as candidate keys. In this relation, Employee_Id is chosen as the primary key, so the other candidate key, PAN_No, acts as the Alternate key. 6. Composite key • Whenever a primary key consists of more than one attribute, it is known as a composite key. This key is also known as Concatenated Key. • For example, in employee relations, we assume that an employee may be assigned multiple roles, and an employee may work on multiple projects simultaneously. So the primary key will be composed of all three attributes, namely Emp_ID, Emp_role, and Proj_ID in combination. So these attributes act as a composite key since the primary key comprises more than one attribute. 7. Artificial key • The key created using arbitrarily assigned data are known as artificial keys. These keys are created when a primary key is large and complex and has no relationship with many other relations. The data values of the artificial keys are usually numbered in a serial order. Also known as surrogate key. • For example, the primary key, which is composed of Emp_ID, Emp_role, and Proj_ID, is large in employee relations. So it would be better to add a new virtual attribute to identify each tuple in the relation uniquely MCQs Which of the following is not a type of integrity constraint in a DBMS? a) Domain Integrity b) Referential Integrity c) Entity Integrity d) Functional Integrity Answer: d) Functional Integrity What does a primary key constraint ensure? a) All values in a column are unique and non-null b) Values in a column must match values in another table c) All values in a column fall within a certain range d) Values in a column can be duplicated Answer: a) All values in a column are unique and non-null Which integrity constraint enforces a relationship between two tables? a) Primary Key b) Foreign Key c) Check Constraint d) Unique Constraint Answer: c) NOT NULL Constraint A composite key is: a) A primary key that consists of two or more columns b) A foreign key that refers to multiple columns c) A key that contains duplicate values d) A key used to create indexes Answer: a) A primary key that consists of two or more columns What type of constraint would you use to ensure that an employee's salary is always greater than zero? a) Primary Key b) Foreign Key c) Check Constraint d) Unique Constraint • Answer: c) Check Constraint Which constraint can be applied to ensure the uniqueness of values in a column, but allows NULLs? a) Primary Key b) Foreign Key c) Unique Constraint d) NOT NULL Constraint Answer: c) Unique Constraint A table can have how many primary keys? a) One b) Two c) As many as needed d) Zero Answer: a) One Which of the following is a user-defined constraint? a) Primary Key b) Foreign Key c) Check Constraint d) NOT NULL Constraint Answer: c) Check Constraint What does referential integrity ensure in a DBMS? a) Data in a table is accurate and consistent b) Data in a table is unique and non-null c) Relationships between tables are maintained correctly d) Data is stored in a secure manner Answer: c) Relationships between tables are maintained correctly Which of the following is a unique identifier for a row in a table? a) Foreign Key b) Primary Key c) Composite Key d) Candidate Key Answer: b) Primary Key What is a foreign key used for in a database? a) To uniquely identify records within a table b) To link two tables together c) To ensure that all values in a column are unique d) To enforce domain integrity Answer: b) To link two tables together Which key can be composed of two or more columns? a) Primary Key b) Foreign Key c) Composite Key d) Super Key Answer: c) Composite Key A surrogate key is: a) A key that is not derived from application data b) A natural key c) Always a primary key d) A key that combines two or more columns Answer: a) A key that is not derived from application data Which of the following is a potential candidate for the primary key in a table? a) Alternate Key b) Super Key c) Foreign Key d) Candidate Key Answer: d) Candidate Key What is an alternate key? a) A key that serves as an alternative to the foreign key b) A key that serves as an alternative to the primary key c) A key that is automatically generated by the DBMS d) A composite key Answer: b) A key that serves as an alternative to the primary key Which key ensures that no two rows have the same combination of values in a set of columns? a) Primary Key b) Unique Key c) Foreign Key d) Composite Key Answer: b) Unique Key Which of the following can be a super key? a) Any combination of columns that uniquely identifies a row b) A single column that contains unique values c) A set of columns that includes the primary key and additional columns d) All of the above Answer: d) All of the above What is the purpose of a candidate key in a table? a) To identify records for foreign key relationships b) To act as an alternate to the primary key c) To uniquely identify a row in the table d) To enforce referential integrity Answer: c) To uniquely identify a row in the table Which key must be unique across the table and cannot contain NULL values? a) Unique Key b) Foreign Key c) Primary Key d) Composite Key Answer: c) Primary Key