SQL
SQL
Normalization
- Normalization is a database design technique to remove redundant or to avoid redundant data.
- Example: I have transaction table and that transaction table I have country data.
- In the country data like India, IND, Nepal, NPL
- IND and India is one and the same thing, this called redundant data.
- To avoid redundant data, we use normalization.
Id Name Country
1 Suraj India
2 Deva IND
3 Mack Nepal
4 Jack NPL
2. Implementation Normalization
- Normalization is implementation is splitting the tables in to two, one with reference table and other is
transaction table
3. Denormalization
- Denormalization is database design technique to improve search performance.
Table1
Single
Split merge Table
Table2
Normalization Denormalization
First Last
Id
Name Name
1 Suraj Java
2 Deva Java
3 Mack Tik
4 Jack Tok
- 2normal form – second normal it should be fully depended on primary key.
Salutatio
Id Name Country Id Name
n
1 1 Suraj 1 1 India
2 2 Deva 1 2 Nepal
3 1 Mack 2
4 1 Jack 2
Id Name
1 Mr
2 Mrs
Char(6) Varchar(6)
CharL VarL
CharCode en VarCode en
IND 6 IND 3
India 6 India 5
Char(3) Nchar(3)
CharL NCharL
CharCode en NCharCode en
IND 3 IND 6
NPL 3 NPL 6
9. INDEX
- Index is help you to increase SQL server search performance.
- For example
- You have 100 record like 1, 2, 3, till 99 ,100.
- If you have not index then it works sequential search, it will start 1,2,3
- So when you create index it creates nodes and leaf nodes. Its create balance tree structure
- Let’s assume internal create two nodes one is less than 50 and others is more than 50 and user says 52, so
it will go to second node and fetch data.
- And bypass all 50 recorder
52
1 50 51 100
10. Types
- Cluster index
- Non-Cluster index
12. Triggers
- Triggers are a small piece of logic execute when certain events like insert, update or delete or any others
event happened, once those event happen you want to execute that logic.
- When insert, update and delete happens on the tblemployee make insert into the bkpemployee table with
the current date and time.
ON tblemployee
AS
BEGIN
END
13. Type of trigger
- After Trigger – After event has happened logic executed.
- Instead of Trigger – Instead of the event the logic executed.
14. Identity
- Numeric data type you get something called as identity specification. When you define a column as Yes its
values increment automatically. It is an incremented column by sql server.
15. Transaction
- Transaction is helps us to treat series of activity one logical unit. Either everything is successful and or
everything rollback.
BEGIN TRY
BEGIN TRAN
COMMIT TRAN
END TRY
BEGIN CATCH
ROLL BACK
END CATCH
16. Joins
- INNER JOIN – Matching records from both table
- SELECT A.NAME FROM EMP A INNER JOIN MANAGER M ON A.EMPID = M.EMPID
- LEFT JOIN – All records from left table and matching only from right table
- RIGHT JOIN – All records from right table and matching only from left table
- FULL OUTER JOIN - matching and un-matching records from left and right table
- CROSS JOIN – Return every record of left table into right table.
17. Synonym
- Is an alias or alternative name of database object like table, view, store procedure & user define functions.
- CREATE SYNONYM TempEmployee for Employee;
- Select * from TempEmployee;
- DROP SYNONYM IF EXISTS TempEmployee;
18. ACID
- A – Atomic
- All statements in the transaction either completed successfully or they were all rollback.
- C – Consistency
- At the end every transaction all the data must be left in consistency state.
- &
- Data must be consistent state after and before transaction.
- I – Isolation
- No other process can change the data while the transaction is running.
- &
- Concurrency transaction has handle by isolation.
- D – Durable
- The changes made by a transaction must persist.
19. CTE
- Common table expression it can be thought as a temporary result set that is defined within the execution
scope of a single SELECT, INSERT, UPDATE, DELETE AND VIEW Statement. A CTE is similar to a derived table.
21. VIEW
- View is virtual table based on result set of an SQL statement which stored physically on database schema.