Interview Questions and Answers
What is Data Integirty? What are the different types of Constraints available in SQL Server?
What is Data Integrity?
- Data integrity means the data contained in the database is accurate and reliable.
- To provide data integrity, RDBMS provides a set of integrity constraints that ensures that data entered into the database is accurate, valid and consistent.
What are the different levels of data integrity in SQL Server?
- Entity Integrity (uniquely identify- PK, UQ)
- Domain Integrity (Follow defined rule- Check, Default, Not Null)
- Referential integrity (relationships- FK)
- User-defined integrity
What is Entity integrity?
- Entity integrity ensures each row in a table is a uniquely identifiable entity. We can achieve entity integrity in a table by using either PRIMARY KEY or UNIQUE KEY constraints.
- In simple words, we can say Entity Integrity ensures that there are no duplicate rows in a table.
What is Referential integrity?
- Referential integrity ensures that relationships between tables remain preserved as data is inserted, deleted and modified. We can apply referential integrity using a FOREIGN KEY constraint.
- In simple words, we can say that Referential integrity ensures that rows cannot be updated or deleted which are used by other records.
What is Domain Integrity?
- Domain integrity ensures that the values going to store in a column must follow some defined rules such as range, type, and format. A database can enforce these rules by using the CHECK KEY constraint.
- In simple words, we can say that Domain Integrity enforces valid entries for a given column by restricting the type, the format, or the range of possible values.
What is user-defined integrity?
User-Defined Integrity enforces some specific business rules that do not fall into an entity, domain, or referential integrity categories. Each of these categories of data integrity can be enforced by the appropriate constraints.
What is a Constraint in SQL Server?
A constraint is a property that is assigned to a column or set of columns in a table that is used to enforce data integrity means it ensures that the data is going to store in a table is valid, consistent and accurate.
Why do we need Constraints in SQL Server?
- A constraint is used to restrict the insertion of unwanted data in any columns.
- We can create constraints on single or multiple columns of any table.
- It maintains the data integrity i.e. accurate data or original data of the table.
What are the different types of Constraints available in SQL Server?
SQL Server supports five constraints for maintaining data integrity those are
- UNIQUE KEY constraint
- NOT NULL constraint
- CHECK KEY constraint
- PRIMARY KEY constraint
- FOREIGN KEY constraint.
NOTE: Constraints are imposed on columns of a table.
What is Default Constraint in SQL Server?
- The default constraint is used to fill the column with a default value that is defined during the creation of a table if the user does not supply any value while inserting the data.
- IDENTITY columns and timestamp columns can’t be associated with a default constraint.
- In simple words, we can say that Default constraints enable the SQL Server to write default value to a column when the user doesn’t specify a value.
- A default allows us to specify a constant value, NULL or the run-time value of a system function if no known value exists or if the column is missing in an INSERT statement.
- Let us see an example for understanding this concept
What is NOT NULL Constraint in SQL Server?
- If a NOT NULL constraint is imposed on a column then that column will not allow null values into it.
- The NOT NULL Constraint is used to avoid NULL values but accepted duplicate values into a column.
- It enforces that the column will not accept NULL values. The NOT NULL constraints are used to enforce domain integrity as the check constraints.
- A table can contain any number of NOT NULL constraints.
- We can apply the NOT NULL constraint on any data type column such as integer, character, money, etc.
Note: When we INSERT a null value into a column on which the NOT NULL constraint is imposed. The execution of the insert statement is terminated by displaying a user-friendly message telling the reason for termination and also specifies the database, the table and the column where the problem got occurred.
What is a Unique Constraint in SQL Server?
- If this constraint is imposed on a column then that column will not allow duplicate values into it.
- A UNIQUE constraint is used to avoid duplicate values but accepted null values in a column.
- IT enforces the uniqueness of the values in a set of columns, so no duplicate values are entered. The unique key constraints are used to enforce entity integrity as the primary key constraints.
- A table can contain any number of UNIQUE constraints.
- We can apply the UNIQUE constraint on any data type column such as integer, character, money, etc.
- The UNIQUE constraint will accept 1 NULL value only.
Note: The unique key constraint is used to make sure that there is no duplicate value in that column. Both unique key and primary key enforces the uniqueness of column but there is one difference between them unique key constraint allows null value but the primary key does not allow null value.
IMPOSING CONSTRAINT ON TABLE:
We can impose constraints on a table in two different ways
- Column-level imposing a constraint
- Table level imposing a constraint
In the first case, we provide the constraint information inside the column only whereas in the second case we first define all the columns and then we define constraints on the columns.
Note: We cannot impose NOT NULL constraint in table level. It is possible only for the rest of the four constraints.
Column Level Imposing a Constraint:
- CREATE TABLE CUSTOMER
- (
- CID INT CONSTRAINT CUSTID_UNIQUE UNIQUE,
- CNAME VARCHAR(50),
- BALANCE MONEY,
- CITY VARCHAR(50)
- )
Table Level Imposing a Constraint:
- CREATE TABLE CUSTOMER
- (
- CUSTID INT,
- CNAME VARCHAR(50),
- BALANCE MONEY,
- CITY VARCHAR(50),
- CONSTRAINT CUSTID_UNIQUE UNIQUE(CUSTID)
- )
What is a composite constraint in SQL Server?
There is no difference in behavior whether the constraint is imposed in table level or column level but if a constraint is imposed in table level we have the advantage of imposing composite constraints. That is one constraint on multiple columns.
For example:
- CREATE TABLE BRANCHDETAILS
- (
- CITY VARCHAR(50),
- BRANCHCODE VARCHAR(10),
- BRANCHLOCATION VARCHAR (30),
- CONSTRAINT CITY_BC_UNIQUE UNIQUE(CITY, BRANCHCODE)
- )
Note: The drawback with a NOT NULL constraint is it will allow duplicate values whereas in case of the UNIQUE constraint it will allow null values.
What is CHECK Constraint in SQL Server?
- The CHECK constraint is used to enforce domain integrity.
- Domain integrity ensures that the values going to store in a column must follow some defined rules such as range, type, and format.
- In simple words, we can say that Domain Integrity enforces valid entries for a given column by restricting the type, the format, or the range of possible values.
- Check constraints allow us to define an expression for a TABLE that must not evaluate to FALSE for a data modification statement to succeed.
Example:
- CREATE TABLE EMPLOYEE
- (
- EMP_ID INT NOT NULL PRIMARY KEY CHECK(EMP_ID BETWEEN 0 AND 1000),
- EMP_NAME VARCHAR(30) NOT NULL,
- ENTERED_DATE DATETIME NOT NULL CHECK (ENTERED_DATE >= CURRENT_TIMESTAMP),
- DEPT_NO INT CHECK(DEPT_NO > 0 AND DEPT_NO < 100)
- )
Check constraints will be useful to limit the range of possible values in a column.
We could create check constraints at two different levels
- Column-level check constraints are applied only to the column and cannot reference data in other columns
- Table-level check constraints can reference any column within a table but cannot reference columns in other tables
A table can contain any number of check constraints and will apply on any column data type like integer, character, and decimal, date, etc.
What is the Primary Key Constraint in SQL Server?
- PRIMARY KEY is the combination of UNIQUE and NOT NULL constraint which does not allow either NULL or Duplicate values into a column or columns.
- Using the primary key we can enforce entity integrity.
- PRIMARY KEY is also used to make a relationship with the FOREIGN KEY constraint on the table.
- A table should contain 1 PRIMARY KEY constraint only which can be either on a single or multiple columns i.e. composite primary key also allowed.
- Every TABLE should have a primary key constraint to uniquely identify each row and only one primary key constraint can be created for each table.
- PRIMARY KEY can apply to any data type like integer, character, decimal, money, etc.
What is a Composite Primary Key in SQL Server?
- When the primary key constructed with more than 1 column is called a composite primary key.
- A maximum number of columns are including in the composite primary key is 16 columns.
- The composite primary key we need to define after all columns definition i.e. at the end of the table definition.
- In a composite primary key, each column can accept duplicate values but the duplicate combinations should not be duplicated.
Note: The primary key is also called a candidate key.
What is Foreign Key Constraint in SQL Server?
- One of the most important concepts in a database is creating a relationship between database tables.
- These relationships provide a mechanism for linking data stores in multiple tables and retrieving them in an efficient manner.
- In order to create a link between two tables, we must specify a FOREIGN KEY in one table that references a column in another table.
- FOREIGN KEY constraints are used for relating or binding two tables with each other and then verify the existence of one table data in other tables.
- A foreign key in one TABLE points to a primary key in another table. Foreign keys prevent actions that would leave rows with foreign key values when there are no primary keys with that value. The foreign key constraints are used to enforce referential integrity.
To impose a FOREIGN KEY Constraint we require the following things
- We require two tables for binding with each other and those two tables must have a common column for linking the tables.
- The common column that is present in both the tables need not have the same name but their data type must be the same.
- The common column that is present under the parent table or master table is known as a reference key column and the reference key column should not contain duplicate values in it. So we need to impose either UNIQUE or PRIMARY key constraint on that column.
- If we impose a primary key constraint on the reference key column that column will also be the identity column of the table.
- The common column which is present in the child or detailed table is known as the FOREIGN key column and we need to impose a FOREIGN key constraint on the column which refers to the reference key column of the master table.
How can we provide the default value for a column?
The default, default value for any column is NULL, provided the column is not imposed with a NOT NULL constraint.
At the time of creating the table, we have a chance of giving our own default value on the column. So that when we insert a record into the table without specifying a value to that column automatically the default value comes into the picture and inserted into the column whereas if we provide a value that provided only will be inserted.
What is SELF REFERENTIAL INTEGRITY?
This is the same as the referential integrity we have learned earlier. In earlier cases, we are binding one column of a table with another column of another table whereas in self-referential integrity we bind a column of a table with another column of the same table i.e. both the foreign key and primary key will be present in one table only.
Steps to view the primary key and foreign key relationship ER diagram representation.
- Go to open SQL Server management studio
- Click on new query option
- Go to query option from the main menu bar
- Click on design query in editor option
- Select primary key and foreign key tables one by one and click on the add button
- Click on close button
What is the difference between primary key and a unique key?
Both primary key and unique key enforces the uniqueness of the column on which they are defined. But by default primary key creates a unique clustered index on the column where are unique key creates a unique non-clustered index by default. Another major difference is that the primary key doesn’t allow NULLs, but unique key allows one NULL only.
What is the difference between primary key and foreign key?
Primary key:
- A primary key uniquely identifies a record in the table.
- Primary Key can’t accept null values and duplicate values by default,
- The primary key is a clustered index and data in the database table is physically organized in the sequence of the clustered index.
- We can have only one Primary key in a table.
Foreign key:
- A foreign key is a field in the table that is the primary key (or unique key) in another table.
- The foreign key can accept null values and duplicate values.
- A foreign key does not automatically create an index, clustered or non-clustered. We can manually create an index on the foreign key.
- We can have more than one foreign key in a table.
Note: We can’t insert foreign key column value into the table if primary key value not available but the reverse is possible and we can’t delete primary key value if the foreign key reference is set into the table but the reverse is possible.
CAN A TABLE HAVE MULTIPLE UNIQUE, FOREIGN, AND/OR PRIMARY KEYS?
A table can have multiple unique and foreign keys. However, a table can have only one primary key.
CAN A UNIQUE KEY HAVE NULL VALUES? CAN A PRIMARY KEY HAVE NULL VALUES?
Unique key columns are allowed to hold a single NULL value. The values in a primary key column, however, can never be NULL.
CAN A FOREIGN KEY REFERENCE A NON-PRIMARY KEY?
Yes, a foreign key can actually reference a key that is not the primary key of a table. But a foreign key must reference a unique key.
CAN A FOREIGN KEY CONTAIN NULL VALUES?
Yes, a foreign key can hold NULL values. Because foreign keys can reference unique, non-primary keys – which can hold NULL values – this means that foreign keys can themselves hold NULL values as well.
Post a Comment
0 Comments