- What is RDBMS?
- Explain DML, DDL, DCL, and TCL statements with examples?
- What is the difference between Drop, Delete and Truncate statements in SQL Server?
- What is the Cascading referential integrity constraint?
- Explain the difference between where clause and having a clause in SQL Server
- What are the differences between primary key and unique key in SQL Server?
- How can we copy the data from one table to another?
- How to create a new table from an existing table or in how many ways we can create a new table from an existing table?
- What is normalization?
- What are the different normalization forms?
- What is the Cursor?
- What is the use of DBCC commands?
- What is a Linked Server?
- What is Collation?
- What are the different types of Collation Sensitivity?
- How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?
- What is a NOLOCK?
- What is the use of the UPDATE_STATISTICS command?
- What is sub-query? Explain the properties of sub-query.
- What is the SQL Profiler?
- What are the authentication modes in SQL Server? How can it be changed?
- What is an SQL server agent?
- What is log shipping?
- What are sp_configure commands and set commands?
- What are the different types of replication?
- What does it mean to have quoted_identifier on?
- What are the implications of having it off?
- What is the STUFF function and how does it differ from the REPLACE function?
- How to rebuild the Master Database?
- What are the basic functions for master, msdb, model, tempdb databases?
- What is De-normalization?
- What is Scheduled Jobs or What is a Scheduled Tasks?
- What is BCP? When does it use?
- How do you load large data to the SQL server database?
- Can we rewrite subqueries into simple select statements or with joins?
- Can SQL Servers link to other servers like Oracle?
- How to copy the tables, schema, and views from one SQL server to another?
- What is data warehousing?
- What is OLTP (Online Transaction Processing)?
- What is Data Integrity?
- What are the different levels of data integrity in SQL Server?
- What is Entity integrity?
- What is Referential integrity?
- What is Domain Integrity?
- What is user-defined integrity?
- What is a Constraint in SQL Server?
- Why do we need Constraints in SQL Server?
- What are the different types of Constraints available in SQL Server?
- What is Default Constraint in SQL Server?
- What is NOT NULL Constraint in SQL Server?
- What is a Unique Constraint in SQL Server?
- What is a composite constraint in SQL Server?
- What is CHECK Constraint in SQL Server?
- What is the Primary Key Constraint in SQL Server?
- What is a Composite Primary Key in SQL Server?
- What is Foreign Key Constraint in SQL Server?
- How can we provide the default value for a column?
- What is SELF REFERENTIAL INTEGRITY
- What is the difference between a primary key and a unique key?
- What is the difference between primary key and foreign key?
What is RDBMS?
Relational Database Management Systems (RDBMS) are database management systems that maintain data records and indices in tables. Relationships may be created and maintained across and among the data and tables. In a relational database, relationships between data items are expressed by means of tables. Interdependencies among these tables are expressed by data values rather than by pointers.
This allows for a high degree of data independence. An RDBMS has the capability to recombine the data items from different files, providing powerful tools for data usage.
Explain DML, DDL, DCL, and TCL statements with examples?
This is one of the most frequently asked SQL Server Interview Questions. Let us understand the above terms in detail.
DML:
DML stands for Data Manipulation Language. DML is used to retrieve, insert, update, and delete data in a database that means DML statements affect records in a table. These are basic operations we perform on data such as selecting a few records from a table, inserting new records, deleting unnecessary records, and updating/modifying existing records. DML statements include the following:
SELECT – select records from a table
INSERT – insert new records
UPDATE – update/Modify existing records
DELETE – delete existing records
DDL:
DDL stands for Data Definition Language. DDL statements are used to alter/modify a database or table structure and schema. These statements handle the design and storage of database objects.
CREATE – create a new Table, database, schema
ALTER – alter the existing table, column description
DROP – delete existing objects from a database
DCL:
DCL stands for data control language. Generally, we will use these commands to secure database objects by creating roles, permissions using GRANT, REVOKE operations. In SQL Server, the following operations will come under DCL operations
GRANT – allows users to read/write on certain database objects
REVOKE – keeps users from the read/write permission on database objects
TCL:
TCL stands for Transactional Control Language. TCL is used to manage transactions within a database. Examples: COMMIT, ROLLBACK, Begin Transaction statements
BEGIN Transaction – opens a transaction
COMMIT Transaction – commits a transaction
ROLLBACK Transaction – ROLLBACK a transaction in case of any error
What is the difference between Drop, Delete and Truncate statements in SQL Server?
This is one of the frequently asked SQL Server Interview Questions. The Drop, Delete, and Truncate – All operations can be rolled back.
Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command.
All the statements (Delete, Truncate, and Drop) are logged operations but the amount of information that is logged varies. Delete statement logs an entry in the transaction log for each deleted row, whereas Truncate Table logs only the Page deallocations. Hence, truncate is a little faster than Delete.
DELETE:
- The DELETE command is used to remove some or all rows from a table.
- A WHERE clause can be used with a DELETE command to remove some specific rows from a table.
- If the WHERE condition is not specified, then all rows are removed.
- The DELETE operation will cause all DELETE triggers on the table to fire.
- It does not reset the identity of the column value.
- It removes rows on a row-by-row basis and hence for each deleted row it records an entry in the transaction logs, thus this is slower than truncate.
- This is a DML command so it is just used to manipulate or modify the table data and it does not change any property of a table.
TRUNCATE:
- TRUNCATE removes all rows from a table, but the table structure and its columns, constraints, indexes, and so on remain.
- It does not require a WHERE clause, so we cannot filter rows while Truncating.
- IDENTITY columns are re-seeded on this operation if no seed was defined then the default value 1 is used.
- No Triggers are fired on this operation because it does not operate on individual rows.
- TRUNCATE removes the data by deallocating the data pages used to store the table’s data instead of rows and records, and only the page deallocations are recorded in the transaction log thus it is faster than delete.
- We cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint.
- TRUNCATE is DDL Command
DROP:
- The DROP command removes a table from the database.
- All the related Data, Indexes, Triggers, Constraints, and Permission specifications for the Table are dropped by this operation.
- Some objects like Views, Stored Procedures that reference the dropped table are not dropped and must be explicitly dropped.
- Cannot drop a table that is referenced by any Foreign Key constraint.
- No Triggers are fired on this operation because it does not operate on individual rows.
Note: If TRUNCATE is written in Query Editor surrounded by TRANSACTION and if the session is closed, it cannot be rolled back but DELETE can be rolled back.
What is the Cascading referential integrity constraint?
This is one of the most frequently asked SQL Server Interview Questions. Cascading referential integrity constraints are foreign key constraints that tell SQL Server to perform certain actions when a user attempts to delete or update a primary key to which an existing foreign keys point.
SET NULL: If a delete or update statement affects rows in a foreign key table, those values will be set to NULL when the primary key record is deleted or updated. The foreign key columns affected must allow NULL values.
CASCADE: If a delete statement affects one or more rows in a foreign key table, those rows will be deleted when the primary key record is deleted. If an update statement affects rows in the foreign key table, those rows will be updated with the value from the primary key record after it has been updated.
SET DEFAULT: If a delete or update statement affects rows in a foreign key table, then all rows containing those foreign keys are set to the default value. All foreign key columns in the related table must have default constraints defined on them.
NO ACTION: This is the default action. This specifies that if an update or deletes statement affects rows in foreign key tables, then the action will be denied and rolled back. An error message will be raised.
What is the difference between where clause and having clause in SQL Server?
This is one of the most frequently asked SQL Server Interview Questions and in almost all interviews this question being asked.
- WHERE clause cannot be used with aggregate functions whereas the HAVING clause can be used with aggregate functions. This means the WHERE clause is used for filtering individual rows on a table whereas the HAVING clause is used to filter groups.
- WHERE comes before GROUP BY. This means the WHERE clause filters rows before aggregate calculations are performed. HAVING comes after GROUP BY. This means the HAVING clause filters rows after aggregate calculations are performed. So, from a performance standpoint, HAVING is slower than WHERE and should be avoided when possible.
- WHERE and HAVING clause can be used together in a SELECT query. In this case WHERE clause is applied first to filter individual rows. The rows are then grouped and aggregate calculations are performed, and then the HAVING clause filters the groups.
- WHERE clause can be used with – Select, Insert, and Update statements whereas the HAVING clause can only be used with the Select statement.
What are the differences between primary key and unique key in SQL Server?
This is of the most asked SQL Server Interview Questions in Interviews. Let discuss this question in detail.
- A table can have only one primary key. On the other hand, a table can have more than one unique key.
- The primary key column does not accept any null values whereas a unique key column accepts one null value.
- Both Primary key and unique key enforce the uniqueness of the column on which they are defined. But By default, the primary key creates a unique clustered index on the column whereas a unique key creates a unique non clustered index.
How can we copy the data from one table to another?
This is one of the most frequently asked SQL Server Interview Questions and the interviewer basically asked to write the query. When we copy the data from one table to another table then the two tables should contain the same structure.
When we copy the data from one table to another table we use insert and select query. Tables always independent objects that mean a table does not depend on other tables
How to create a new table from an existing table or in how many ways we can create a new table from an existing table?
If required we can create a new table from an existing table as below.
Syntax1: (with all column from an existing table)
SELECT * INTO <NEW TABLE NAME> FROM <OLD TABLE NAME>
Example: SELECT * INTO NEWEMPLOYEE FROM EMPLOYEE
When we execute the above query it will create a new table with all records from an existing table.
Syntax2: (with specific columns from an existing table)
SELECT <REQUIREDCOLUMN> INTO <NEW TABLE NAME> FROM <OLD TABLE NAME>
Example: SELECT EID, SALARY INTO SPECEMP FROM EMPLOYEE
When we execute the above query it will create a new table with the specific column data from an existing table.
Syntax3: (creating a new table without data)
SELECT * INTO <NEW TABLE NAME> FROM <OLD TABLE NAME> WHERE 1 = 0
Example: SELECT * INTO DUMMYEMP FROM EMPLOYEE WHERE 1 = 0
OR
SELECT <REQUIRED COLUMNS> INTO <NEW TABLE NAME> FROM <OLD TABLE NAME>
SELECT EID, SALARY INTO TAB1 FROM EMPLOYEE WHERE 1 = 0
When we execute the above query it will create a new table without records from an existing table.
What is normalization?
Database normalization is a data design and organization process applied to data structures based on rules that help build relational databases. In relational database design the process of organizing data to minimize redundancy. Normalization usually involves dividing a database into two or more tables and defining relationships between the tables. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database via the defined relationships.
What are the different normalization forms?
This is one of the frequently asked SQL Server Interview Questions.
1NF: Eliminate Repeating Groups: Make a separate table for each set of related attributes, and give each table a primary key. Each field contains at most one value from its attribute domain.
2NF: Eliminate Redundant Data: If an attribute depends on only part of a multi-valued key, remove it to a separate table.
3NF: Eliminate Columns Not Dependent On Key: If attributes do not contribute to a description of the key, remove them to a separate table. All attributes must be directly dependent on the primary key
BCNF: Boyce-Codd Normal Form: If there are non-trivial dependencies between candidate key attributes, separate them out into distinct tables.
4NF: Isolate Independent Multiple Relationships: No table may contain two or more 1:n or n:m relationships that are not directly related.
5NF: Isolate Semantically Related Multiple Relationships: There may be practical constrains on information that justifies separating logically related many-to-many relationships.
ONF: Optimal Normal Form: A model limited to only simple (elemental) facts, as expressed in Object Role Model notation.
DKNF: Domain-Key Normal Form: A model free from all modification anomalies.
Remember, these normalization guidelines are cumulative. For a database to be in 3NF, it must first fulfill all the criteria of a 2NF and 1NF database. Please click here to learn Database Normalization in detail step by step with some real-time examples.
What is the Cursor?
A cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time.
In order to work with a cursor we need to perform some steps in the following order:
- Declare cursor
- Open cursor
- Fetch row from the cursor Process fetched row Close cursor
- Deallocate cursor
What is the use of DBCC commands?
DBCC stands for database consistency checker. We use these commands to check the consistency of the databases, i.e., maintenance, validation task, and status checks. For example,
- DBCC CHECKDB – Ensures that tables in the DB and the indexes are correctly linked.
- DBCC CHECKALLOC – To check that all pages in a DB are correctly allocated.
- DBCC CHECKFILEGROUP – Checks all tables file group for any damage.
What is a Linked Server?
Linked Servers is a concept in SQL Server by which we can add other SQL Server to a Group and query both the SQL Server DBS using T-SQL Statements. With a linked server, you can create very clean, easy to follow, SQL statements that allow remote data to be retrieved, joined, and combined with local data. Stored Procedure sp_addlinkedserver, sp_addlinkedsrvlogin will be used to add a new Linked Server.
What is Collation?
Collation refers to a set of rules that determine how data is sorted and compared. Character data is sorted using rules that define the correct character sequence, with options for specifying case-sensitivity, accent marks, kana character types, and character width.
What are the different types of Collation Sensitivity?
- Case sensitivity: A and a, B and b, etc.
- Accent sensitivity: a and á, o and ó, etc.
- Kana Sensitivity: When Japanese kana characters Hiragana and Katakana are treated differently, it is called Kana sensitive.
- Width sensitivity: When a single-byte character (half-width) and the same character when represented as a double-byte character (full-width) are treated differently than it is width sensitive.
How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?
- The one-to-one relationship can be implemented as a single table and rarely as two tables with primary and foreign key relationships.
- One-to-Many relationships are implemented by splitting the data into two tables with primary key and foreign key relationships.
- Many-to-Many relationships are implemented using a junction table with the keys from both the tables forming the composite primary key of the junction table.
What is a NOLOCK?
This is one of the most frequently asked SQL Interview Questions. Using the NOLOCK query optimizer hint is generally considered good practice in order to improve concurrency on a busy system. When the NOLOCK hint is included in a SELECT statement, no locks are taken when data is read. The result is a Dirty Read, which means that another process could be updating the data at the exact time you are reading it. There are no guarantees that your query will retrieve the most recent data.
The advantage of performance is that your reading of data will not block updates from taking place, and updates will not block your reading of data. SELECT statements take Shared (Read) locks. This means that multiple SELECT statements are allowed simultaneous access, but other processes are blocked from modifying the data. The updates will queue until all the reads have completed, and reads requested after the update will wait for the updates to complete. The result of your system is the delay(blocking).
When is the use of the UPDATE_STATISTICS command?
This is one of the most frequently asked SQL Interview Questions. This command is basically used when a large processing of data has occurred. If a large number of deletions any modification or Bulk Copy into the tables has occurred, it has to update the indexes to take these changes into account. UPDATE_STATISTICS updates the indexes on these tables accordingly.
What is sub-query? Explain the properties of sub-query.
This is one of the frequently asked SQL Server Interview Questions. Sub-queries are often referred to as sub-selects, as they allow a SELECT statement to be executed arbitrarily within the body of another SQL statement. A sub-query is executed by enclosing it in a set of parentheses. Sub-queries are generally used to return a single row as an atomic value, though they may be used to compare values against multiple rows with the IN keyword.
A subquery is a SELECT statement that is nested within another T-SQL statement. A subquery SELECT statement if executed independently of the T-SQL statement, in which it is nested, will return a result set. Meaning a subquery SELECT statement can stand alone and is not dependent on the statement in which it is nested. A subquery SELECT statement can return any number of values and can be found in, the column list of a SELECT statement, a FROM, GROUP BY, HAVING, and/or ORDER BY clauses of a T-SQL statement. A Subquery can also be used as a parameter to a function call. Basically, a subquery can be used anywhere an expression can be used.
Properties of Sub-Query
- A subquery must be enclosed in the parenthesis.
- The subquery must be put in the right hand of the comparison operator, and
- The subquery cannot contain an ORDER-BY clause.
- A query can contain more than one sub-queries.
What are the types of sub-queries?
- Single-row subquery, where the subquery returns only one row.
- Multiple-row subquery, where the subquery returns multiple rows
- Multiple column subquery, where the subquery returns multiple columns.
What is the SQL Profiler?
This is one of the most frequently asked SQL Server Interview Questions. SQL Profiler is a graphical tool that allows system administrators to monitor events in an instance of Microsoft SQL Server. You can capture and save data about each event to a file or SQL Server table to analyze later. For example, you can monitor a production environment to see which stored procedures are hampering performance by executing too slowly.
Use SQL Profiler to monitor only the events in which you are interested. If traces are becoming too large, you can filter them based on the information you want, so that only a subset of the event data is collected. Monitoring too many events adds overhead to the server and the monitoring process and can cause the trace file or trace table to grow very large, especially when the monitoring process takes place over a long period of time.
Which TCP/IP port does SQL Server run on? How can it be changed?
SQL Server runs on port 1433. It can be changed from the Network Utility TCP/IP properties –> Port number. both on the client and the server.
What are the authentication modes in SQL Server? How can it be changed?
Windows mode and mixed mode (SQL & Windows). To change authentication mode in SQL Server click Start, Programs, Microsoft SQL Server, and click SQL Enterprise Manager to run SQL Enterprise Manager from the Microsoft SQL Server program group. Select the server then from the Tools menu select SQL Server Configuration Properties, and choose the Security page.
Where are SQL server users names and passwords are stored in SQL server?
They get stored in master DB in the sysxlogins table.
Which command using Query Analyzer will give you the version of SQL server and operating system?
SELECT SERVERPROPERTY(‘productversion’), SERVERPROPERTY (‘productlevel’), SERVERPROPERTY (‘edition’)
What is the SQL server agent?
This is one of the frequently asked SQL Server Interview Questions. SQL Server agent plays an important role in the day-to-day tasks of a database administrator (DBA). It is often overlooked as one of the main tools for SQL Server management. Its purpose is to ease the implementation of tasks for the DBA, with its full-function scheduling engine, which allows you to schedule your own jobs and scripts.
What is log shipping?
Log shipping is the process of automating the backup of database and transaction log files on a production SQL server and then restoring them onto a standby server. Enterprise Editions only supports log shipping. In log shipping, the transnational log file from one server is automatically updated into the backup database on the other server. If one server fails, the other server will have the same DB that can be used as the Disaster Recovery plan. The key feature of log shipping is that it will automatically backup transaction logs throughout the day and automatically restore them on the standby server at a defined interval.
What command do we use to rename a DB?
sp_renamedb ‘oldname’, ‘newname’
If someone is using DB it will not accept sp_renmaedb. In that case, first, bring DB to the single user using sp_dboptions. Use sp_renamedb to rename the database. Use sp_dboptions to bring the database to multi-user mode.
What are sp_configure commands and set commands?
Use sp_configure to display or change server-level settings. To change database-level settings, use ALTER DATABASE. To change settings that affect only the current user session, use the SET statement.
What are the different types of replication? Explain.
The SQL Server 2000-supported replication types are as follows:
- Transactional
- Snapshot
- Merge
Snapshot replication distributes data exactly as it appears at a specific moment in time and does not monitor for updates to the data. Snapshot replication is best used as a method for replicating data that changes infrequently or where the most up-to-date values (low latency) are not a requirement. When synchronization occurs, the entire snapshot is generated and sent to Subscribers.
In transactional replication, an initial snapshot of data is applied at Subscribers, and then when data modifications are made at the Publisher, the individual transactions are captured and propagated to Subscribers.
Merge replication is the process of distributing data from Publisher to Subscribers, allowing the Publisher and Subscribers to make updates while connected or disconnected, and then merging the updates between sites when they are connected.
What are the OS services that the SQL Server installation adds?
MS SQL SERVER SERVICE, SQL AGENT SERVICE, DTC (Distribution transac co-ordinator)
What are three SQL keywords used to change or set someone’s permissions?
GRANT, DENY, and REVOKE.
What does it mean to have quoted_identifier on? What are the implications of having it off?
When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marks. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers.
What is the STUFF function and how does it differ from the REPLACE function?
STUFF function to overwrite existing characters. Using this syntax, STUFF(string_expression, start, length, replacement_characters), string_expression is the string that will have characters substituted, the start is the starting position, the length is the number of characters in the string that are substituted, and replacement_characters are the new characters interjected into the string.
REPLACE function to replace existing characters of all occurrences. Using this syntax REPLACE(string_expression, search_string, replacement_string), where every incidence of search_string found in the string_expression will be replaced with replacement_string. Using query analyzer, name 3 ways to get an accurate count of the number of records in a table?
SELECT * FROM table1 SELECT COUNT(*) FROM table1
SELECT rows FROM sysindexes WHERE id = OBJECT_ID(table1) AND indid < 2
How to rebuild the Master Database?
This is one of the frequently asked SQL Server Questions. Shutdown Microsoft SQL Server 2000, and then run Rebuildm.exe. This is located in the Program Files\Microsoft SQL Server\80\Tools\Binn directory.
In the Rebuild Master dialog box, click Browse. In the Browse for Folder dialog box, select the \Data folder on the SQL Server 2000 compact disc or in the shared network directory from which SQL Server 2000 was installed, and then click OK.
Click Settings. In the Collation Settings dialog box, verify or change settings used for the master database and all other databases.
Initially, the default collation settings are shown, but these may not match the collation selected during setup. You can select the same settings used during setup or select new collation settings. When done, click OK.
In the Rebuild Master dialog box, click Rebuild to start the process. The Rebuild Master utility reinstalls the master database. To continue, you may need to stop a server that is running.
What are the basic functions for master, MSDB, model, TEMPFB databases?
This is one of the frequently asked SQL Server Interview Questions. The Master database holds information for all databases located on the SQL Server instance and is the glue that holds the engine together. Because SQL Server cannot start without a functioning master database, you must administer this database with care.
The MSDB database stores information regarding database backups, SQL Agent information, DTS packages, SQL Server jobs, and some replication information such as for log shipping.
The TEMPDB holds temporary objects such as global and local temporary tables and stored procedures. The model is essentially a template database used in the creation of any new user database created in the instance.
What is De-normalization?
De-normalization is the process of attempting to optimize the performance of a database by adding redundant data. It is sometimes necessary because current DBMSs implement the relational model poorly. A true relational DBMS would allow for a fully normalized database at the logical level while providing physical storage of data that is tuned for high performance. De-normalisation is a technique to move from higher to lower normal forms of database modeling in order to speed up database access.
What is Scheduled Jobs or What is a Scheduled Tasks?
This is one of the frequently asked SQL Server Interview Questions. Scheduled tasks let users automate processes that run on regular or predictable cycles. Users can schedule administrative tasks, such as cube processing, to run during times of slow business activity. Users can also determine the order in which tasks run by creating job steps within a SQL Server Agent job. E.g. Back up the database, Update the Stats of Tables. Job steps give the user control over the flow of execution.
If one job fails, the user can configure SQL Server Agent to continue to run the remaining tasks or to stop the execution.
What is BCP? When does it use?
BulkCopy is a tool used to copy a huge amount of data from tables and views. BCP does not copy the structures same as source to destination.
How do you load large data to the SQL server database?
BulkCopy is a tool used to copy a huge amount of data from tables. BULK INSERT command helps to Imports a data file into a database table or view in a user-specified format.
Can we rewrite subqueries into simple select statements or with joins?
Subqueries can often be re-written to use a standard outer join, resulting in faster performance. As we may know, an outer join uses the plus sign (+) operator to tell the database to return all non-matching rows with NULL values. Hence we combine the outer join with a NULL test in the WHERE clause to reproduce the result set without using a sub-query.
Can SQL Servers link to other servers like Oracle?
This is one of the most frequently asked SQL Server Interview Questions. SQL Server can be lined to any server provided it has an OLE-DB provider from Microsoft to allow a link. E.g. Oracle has an OLE-DB provider for oracle that Microsoft provides to add it as a linked server to the SQL Server group.
How to copy the tables, schema, and views from one SQL server to another?
Microsoft SQL Server 2000 Data Transformation Services (DTS) is a set of graphical tools and programmable objects that lets user extract, transform, and consolidate data from disparate sources into single or multiple destinations.
What is Data Warehousing?
Subject-oriented, meaning that the data in the database is organized so that all the data elements relating to the same real-world event or object are linked together;
Time-variant, meaning that the changes to the data in the database are tracked and recorded so that reports can be produced showing changes over time;
Non-volatile, meaning that data in the database is never over-written or deleted, once committed, the data is static, read-only, but retained for future reporting;
Integrated, meaning that the database contains data from most or all of an organization’s operational applications and that this data is made consistent.
What is OLTP (Online Transaction Processing)?
In OLTP – online transaction processing systems relational database design uses the discipline of data modeling and generally follows the Codd rules of data normalization in order to ensure absolute data integrity. Using these rules complex information is broken down into its most simple structures (a table) where all of the individual atomic level elements relate to each other and satisfy the normalization rules.
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
- 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 which 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 the 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:
Table Level Imposing a Constraint:
What is a composite constraint in SQL Server?
There is no difference in behavior whether the constraint is imposed at table level or column level but if a constraint is imposed at table level we have the advantage of imposing composite constraints. That is one constraint on multiple columns. For example:
Note: The drawback with a NOT NULL constraint is it will allow duplicate values whereas in the 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:
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 to 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.
- The maximum number of columns is 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. 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 the new query option
- Go to the query option from the main menu bar
- Click on design query in the editor option
- Select primary key and foreign key tables one by one and click on the add button
- Click on the close button
What is the difference between a primary key and a unique key?
This is one of the frequently asked SQL Server Interview Questions. Let us understand the difference between them.
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 the unique key allows one NULL only.
What is the difference between primary key and foreign key?
This is one of the frequently asked SQL Server Interview Questions. Let us understand the difference between a primary key and a 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 a foreign key column value into the table if the primary key value not available but the reverse is possible and we can’t delete the 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.
No comments:
Post a Comment