Relational Data Base 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 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.
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.
A - Atomic. Transaction must be Atomic (it is one unit of work and does not dependent on previous and following transactions).
C - Consistent. Data is either committed or roll back, no “in-between” case where something has been updated and something hasn’t.
I - Isolated. No transaction sees the intermediate results of the current transaction.
D - Durable. The values persist if the data had been committed even if the system crashes right after.
A stored procedure is a named group of SQL statements that have been previously created and stored in the server database. Stored procedures accept input parameters so that a single procedure can be used over the network by several clients using different input data. And when the procedure is modified, all clients automatically get the new version. Stored procedures reduce network traffic and improve performance. Stored procedures can be used to help ensure the integrity of the database.
e.g. sp_helpdb, sp_renamedb, sp_depends etc.
A trigger is a SQL procedure that initiates an action when an event (INSERT, DELETE or UPDATE) occurs. Triggers are stored in and managed by the DBMS. Triggers are used to maintain the referential integrity of data by changing the data in a systematic fashion. A trigger cannot be called or executed; the DBMS automatically fires the trigger as a result of a data modification to the associated table. Triggers can be viewed as similar to stored procedures in that both consist of procedural logic that is stored at the database level. Stored procedures, however, are not event-drive and are not attached to a specific table as triggers are. Stored procedures are explicitly executed by invoking a CALL to the procedure while triggers are implicitly executed. In addition, triggers can also execute stored procedures.
Trigger uses Inserted and Deleted virtual tables
Nested Trigger: a trigger can also contain INSERT, UPDATE and DELETE logic within itself, so when the trigger is fired because of data modification it can also cause another data modification, thereby firing another trigger. A trigger that contains data modification logic within itself is called a nested trigger.
We can define view as stored query. A simple view can be thought of as a subset of a table. It can be used for retrieving data, as well as updating or deleting rows. Rows updated or deleted in the view are updated or deleted in the table the view was created with. It should also be noted that as data in the original table changes, so does data in the view, as views are the way to look at part of the original table. The results of using a view are not permanently stored in the database. The data accessed through a view is actually constructed using standard T-SQL SELECT command and can come from one to many different base tables or even other views.
An index is a set of ordered references to rows of a table. Indices are created in an existing table to locate rows more quickly and efficiently. It is possible to create an index on one or more columns of a table, and each index is given a name. The users cannot see the indexes, they are just used to speed up queries. Effective indexes are one of the best ways to improve performance in a database application.
A table scan happens when there is no index available to help a query. In a table scan SQL Server examines every row in the table to satisfy the query results. Table scans are sometimes unavoidable, but on large tables, scans have a terrific impact on performance.
A clustered index is a special type of index that reorders the way records in the table are physically stored. It defines the physical sorting of a database table’s rows. The leaf nodes of a clustered index contain the data pages. For this reason, each database table may have only one clustered index.
A non-clustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a nonclustered index does not consist of the data pages. Instead, the leaf nodes contain index rows. Non-clustered index created outside of the database table and contain a sorted list of references to the table itself.
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:
Both primary key and unique enforce uniqueness of the column on which they are defined. But by default, primary key creates a clustered index on the column, where are unique creates a nonclustered index by default.
Another major difference is that, primary key doesn’t allow NULLs, but unique key allows one NULL only.
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.
When the NOLOCK hint is included in a SELECT statement, no locks are taken when data is read. Using the NOLOCK query optimiser hint is generally considered good practice in order to improve concurrency on a busy system. The result is a Dirty Read, which means that another process could be updating the data at the exact time we are reading it. There are no guarantees that your query will retrieve the most recent data. The advantage to performance is that our reading of data will not block updates from taking place, and updates will not block our 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 to our system is delay ( because of blocking).
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.
TRUNCATE
User-Defined Functions allow to define its own T-SQL functions that can accept 0 or more parameters and return a single scalar data value or a table data type.
There are three types of User-Defined functions:
Joins are used in queries to explain how different tables are related. Joins also let you select data from a table depending upon data from another table.
Types of joins are:
Both WHERE and HAVING clauses specify a search condition for a group or an aggregate.
The WHERE clause is applied to each row before they are part of the GROUP BY function in a query. The HAVING criteria is applied after the grouping of rows has occurred.
HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.
Sub-queries are often referred to as sub-selects, as they allow a SELECT statement to be executed 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 standalone and is not depended 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.
Sub-query has the following properties: