Database normalization is the process of organizing data to minimize data redundancy (data duplication), which in turn ensures data consistency.
Database normalization is a step by step process. There are 6 normal forms, First Normal form (1NF) to Sixth Normal Form (6NF). Most databases are in third normal form (3NF).
Employee Name | Gender | Salary | Dept | DeptHead | DeptLocation |
---|---|---|---|---|---|
ASHISH | Male | 50000 | IT | Shree | Kolhapur |
Ashutosh | Male | 50000 | HR | Mahesh | Mumbai |
Abhishek | Male | 50000 | IT | Shree | Kolhapur |
Swapnil | Male | 50000 | IT | Shree | Kolhapur |
In above Table (Employees),For every employee with in the same department, we are repeating, all the 3 columns (DeptName, DeptHead and DeptLocation),this may cause data redudancy and data inconsistency
to reduce the data redundancy and inconsistency, we can divide this badly organised table into two (Employees and Departments), as shown below.
DeptId | DeptName | DeptHead | DeptLocation |
---|---|---|---|
1 | IT | Shree | Kolhapur |
2 | HR | Mahesh | Mumbai |
Employee Name | Gender | Salary | DeptId |
---|---|---|---|
ASHISH | Male | 50000 | 1 |
Ashutosh | Male | 50000 | 2 |
Abhishek | Male | 50000 | 1 |
Swapnil | Male | 50000 | 1 |
A table is said to be in 1NF, if
A table is said to be in 2NF, if
A subquery is simply a select statement, that returns a single value and can be nested inside a SELECT, UPDATE, INSERT, or DELETE statement.
It is also possible to nest a subquery inside another subquery.
Subqueries are always encolsed in paranthesis and are also called as inner queries, and the query containing the subquery is called as outer query.
Most of the times subqueries can be very easily replaced with joins.
Select [Id], [Name], [Description] from tblProducts where Id not in (Select Distinct ProductId from tblProductSales)
Function | Description |
---|---|
ASCII | Returns the ASCII value for the specific character |
CHAR | Returns the character based on the ASCII code |
LEN | Returns the length of a string |
LOWER | Converts a string to lower-case |
REVERSE | Reverses a string and returns the result | UPPER | Converts a string to upper-case |
SQL Server comes with the following data types for storing a date or a date/time value in the database
Function | Date Time Format | Description |
---|---|---|
GETDATE() | 2021-08-31 20:15:04.543 | to get current datetime |
CURRENT_TIMESTAMP | 2021-08-31 20:15:04.543 | ANSI SQL equivalent to GETDATE |
SYSDATETIME() | 2021-08-31 20:15:04.5380028 | More fractional seconds precision |
SYSDATETIMEOFFSET() | 2012-08-31 20:15:04.5380028 + 01:00 | More fractional seconds precision + Time zone offset |
GETUTCDATE() | 2021-08-31 19:15:04.543 | UTC Date and Time |
SYSUTCDATETIME() | 2012-08-31 19:15:04.5380028 | UTC Date and Time, with More fractional seconds precision |
Function | Description |
---|---|
ABS | Returns the absolute value of a number |
CEILING | Returns the smallest integer value that is >= a number |
FLOOR | Returns the largest integer value that is <= to a number |
POWER | Returns the value of a number raised to the power of another number |
RAND | Returns a random number |
SQRT | Returns the square root of a number |
SQUARE | Returns the square of a number |
ROUND | Rounds a number to a specified number of decimal places |
SQL Server user-defined functions are routines that accept parameters, perform an action, such as a complex calculation, and return the result of that action as a value.
CREATE FUNCTION [database_name.]function_name (parameters)
RETURNS data_type AS
BEGIN
SQL statements
RETURN value
END;
Temporary Tables are most likely as Permanent Tables. Temporary Tables are Created in TempDB and are automatically deleted as soon as the last connection is terminated. Temporary Tables helps us to store and process intermediate results. Temporary tables are very useful when we need to store temporary data. The Syntax to create a Temporary Table is given below:
CREATE TABLE #EmpDetails (id INT, name VARCHAR(25))
to insert records-
INSERT INTO #EmpDetails VALUES (01, 'Atharva')
to select records-
SELECT * FROM #EmpDetails