0% found this document useful (0 votes)
2 views

SQL

The document provides an overview of various database concepts including normalization, denormalization, OLTP vs OLAP, primary and unique keys, data types, indexing, functions, stored procedures, triggers, transactions, joins, synonyms, ACID properties, common table expressions (CTE), views, and temporary tables. It explains the importance of normalization to eliminate redundant data and the role of denormalization in improving search performance. Additionally, it covers the implementation of these concepts with examples and outlines the differences between local and global temporary tables.

Uploaded by

Suraj Javarat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL

The document provides an overview of various database concepts including normalization, denormalization, OLTP vs OLAP, primary and unique keys, data types, indexing, functions, stored procedures, triggers, transactions, joins, synonyms, ACID properties, common table expressions (CTE), views, and temporary tables. It explains the importance of normalization to eliminate redundant data and the role of denormalization in improving search performance. Additionally, it covers the implementation of these concepts with examples and outlines the differences between local and global temporary tables.

Uploaded by

Suraj Javarat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

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

Id Name Country Id Name


1 Suraj 1 1 India
2 Deva 1 2 Nepal
3 Mack 2
4 Jack 2

3. Denormalization
- Denormalization is database design technique to improve search performance.

Table1

Single
Split merge Table

Table2

Normalization Denormalization

4. OLTP and OLAP?


- OLTP – online transaction process – Normalization
- OLAP – online analytical process – Denormalization

5. 1,2,3 normal form


- 1normal form – create different different column, every column should have a single value.

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.

Id Name Country Id Name


1 Suraj 1 1 India
2 Deva 1 2 Nepal
3 Mack 2
4 Jack 2

- 3normal form - Has no transitive functional dependencies

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

6. Primary key and unique key


- 1st N Nulls – Unique can have nulls, but primary key cannot have nulls
- 2nd N Numbers – Many unique keys but only one primary key.

7. Char and Varchar


- Char – Fixed length
- Varchar – Variable length

Char(6) Varchar(6)
CharL VarL
CharCode en VarCode en
IND 6 IND 3
India 6 India 5

8. Char and NChar


- Char only accept A – Z, a-z and 0 – 9 & 1 char = 1 byte
- NChar accept multi language & 1 char = 2 byte

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

less than More than


50 50

1 50 51 100

10. Types
- Cluster index
- Non-Cluster index

11. Function and Stored procedure


- Function – Computed values but will not make any permanent changes to the environment. Only select
allowed, insert/update/ delete not allowed.
- Stored procedure – Mini batch program. Can change the environment. Insert/update/ delete allowed.

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.

CREATE TRIGGER datamanipulation

ON tblemployee

AFTER INSERT UPDATE DELETE

AS

BEGIN

Insert into bkpemployee(date) value (getdate());

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

INSERT INTO EMP(NAME) VALUES (‘SURAJ’);

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.

SELECT * FROM EMP CROSS JOIN EMP1

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.

20. When use CTE?


- To query real time data on result set in query execution scope.

21. VIEW
- View is virtual table based on result set of an SQL statement which stored physically on database schema.

22. When use view?


- You can restrict users to access directly to a table and allow them to access a subset of data via view.

23. Temp Table


- Temp table is type of table which exists temporarily on database server.
- Its remain available till the present session in live.

24. Types of Temp Table


- Local Temp Table – Local temp table present only live session & different session local table not available
- I can create multiple local temp table with same name.
- #Employee____________________000000C
- #Employee____________________000000D
-
- Global Temp Table -
- --- Temp table
- create table #Student
- (
- Id int,
- Name varchar(50)
- );
-
- select * from #Student
-
- insert into #Student(Id,Name) values(1,'Suraj');
-
- --- Global table
-
- create table ##Student
- (
- Id int,
- Name varchar(50)
- );
-
-
- select * from ##Student
-
- insert into ##Student(Id,Name) values(1,'Suraj');

You might also like