Friday, September 29, 2017

Same Origin Policy vs Cross-origin resource sharing (CORS)

Introduction
During the working on the javascript and Web API , I met the two new concept , which want to share with all

1.Same Origin Policy (SOP)
2.Cross-origin resource sharing (CORS)

Both concepts are very interesting and useful for javascript and web API Programing
 
1.Same Origin Policy (SOP) :
By the definition ,Same Origin Policy prevents a web site's scripts from accessing and interacting with scripts used on other sites.

An Origin is defined as a combination of URI Scheme , host name and port name.

browsers allow websites to store information on a client's computer in the form of cookies , hackers can retrive these information without user's knowledge.This policy prevents a malicious script on one page from obtaining access to sensitive data on another web page through that page's Document Object Model.

2.Cross-origin resource sharing (CORS)

"Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources (e.g. fonts) on a web page[1] to be requested from another domain outside the domain from which the first resource was served" (WikiPedia definition).

In the IFrame era, multiple web applications work together and share the resource between each other,so Same Origin Policy (SOP) can be circumvented , whenever cross site requests are required. setting CORS at the server-side will allow a request to be sent to the server via an XMLHttpRequest even if the request was sent from a different domain. This becomes useful if your server was intended to serve requests from other domains.



Summary
Hackers always look for dissipate the security of the web. CORS allows  secure and unbroken communication between web services without  making their users vulnerable to attack.

Wednesday, April 12, 2017

Example extact file name from column table


create table #temp
(
 id int,
 name varchar(100) null,

)

insert into #temp values(1,'c:\temp\abc.jpg')
insert into #temp values(2,'c:\temp\aDc.jpg')
insert into #temp values(3,'c:\temp\aec.jpg')
insert into #temp values(4,'c:\temp\afc.jpg')


select  RIGHT(name, CHARINDEX('\', REVERSE(name)) -1)  [file_name],
 SUBSTRING(name,len(name)-2,3),* from #temp where CHARINDEX('jpg',name) > 0


Sunday, October 26, 2014

When Not to Use a Heap


  • Do not use a heap when the data is frequently returned in a sorted order. A clustered index on the sorting column could avoid the sorting operation.
  • Do not use a heap when the data is frequently grouped together. Data must be sorted before it is grouped, and a clustered index on the sorting column could avoid the sorting operation.
  • Do not use a heap when ranges of data are frequently queried from the table. A clustered index on the range column will avoid sorting the entire heap.
  • Do not use a heap when there are no nonclustered indexes and the table is large. In a heap, all rows of the heap must be read to find any row.

When to Use a Heap

If a table is a heap and does not have any nonclustered indexes, then the entire table must be examined (a table scan) to find any row. This can be acceptable when the table is tiny, such as a list of the 12 regional offices of a company.

When a table is stored as a heap, individual rows are identified by reference to a row identifier (RID) consisting of the file number, data page number, and slot on the page.
The row id is a small and efficient structure. Sometimes data architects use heaps when data is always accessed through nonclustered indexes

the RID is smaller than a clustered index key.

Saturday, October 11, 2014

Rebuild Index Task and Reorganization Index Task

Rebuild Index Task

1. Rebuilds the indexes specified in the task configuration
  • When multiple database are selected in a single task, all indexes in the selected database will be rebuilt
  • When a single database is selected, individual objects can be selected in that database to rebuild all of the indexes on the selected objects

2. As a side effect all index based statistics are updated with full scan

Limitations

1. Indexes are rebuilt regardless of the fragmentation level
2. Does not support partition level rebuilds for partitioned tables or indexes


Free space options -specifies the free space to leave in the leaf level of the index pages during the rebuild

1.Default free space per page - maintains the currently configured fill factor value for the indexes

2. Change free space per page to -specifies that the free space will be changed to the configured value as a part of the rebuild.


Sort in tempdb option - specifies to store intermediate sort runs in the tempdb database rather than in the database the index is being rebuilt in

Offer the ability to rebuild indexes online if using enterprise edition

- for indexes that can not be rebuilt online, this provide the option to rebuild offline or do nothing at all



Reorganize Index Task

This is reorganizes indexes but does not include a update of the index statistics like the rebuild Index task does

=> As a result, when the reorganize index task is used a subsequent update statistics task should also be included to update the column statistics of the database

Monday, May 20, 2013

Delete the duplicate rows in the sql server


DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)
==============================================================
WITH TempUsers (FirstName,LastName, duplicateRecordCount)
AS
(
SELECT FirstName,LastName,
ROW_NUMBER()OVER(PARTITIONBY FirstName, LastName ORDERBY FirstName) AS duplicateRecordCount
FROM dbo.Users
)
DELETE
FROM TempUsers
WHERE duplicateRecordCount > 1
GO
===============================================================
delete x from (
  select *, rn=row_number() over (partition by EmployeeName order by empId)
  from Employee
) x
where rn > 1;
========================================================
delete T1
from MyTable T1, MyTable T2
where T1.dupField = T2.dupField
and T1.uniqueField > T2.uniqueField

Sunday, April 28, 2013

How to exchange the data of column in the same table in the Sql server


1. Swap with the help of temporary variable:
-------------------------------------------------
DECLARE @temp AS varchar(10) UPDATE Employee1 SET @temp=LastName, LastName=FirstName, FirstName=@temp
 
 
2.directly:
------------

UPDATE Employee1 SET LastName=FirstName, FirstName=LastName

Wednesday, April 24, 2013

Find N-th salary from employee table

Row Number :



SELECT Salary,EmpName FROM
   ( SELECT Salary,EmpName,ROW_NUMBER() OVER(ORDER BY Salary) As RowNum
      FROM EMPLOYEE ) As A
    WHERE A.RowNum IN (2,3)

Sub Query :


SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)





Top Keyword :


SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary

Thursday, April 22, 2010

Index Question 23 April 2010

Index Question 23 April 2010

1. Identity columns is index column?
2. Can we use multiple cluster index in sql query? if yes how?
3. what is composite index ?
4. Can an index declared on multiple column ?
5. if a table has already clustered index , can we create primary key on the table ?
6. what is page split?
7. How to rebuild the index?
8. Can we delete an index used by a primary key or unique constraint ?
9. Can we create index on table variable or temporary table?
10. can we create index on system table?

Backup plans Question from Vikram

Question on Backup plans:
1.What is full, differential and transaction log Backup in SQL Server, and how do they interact with each other?
2. How many Recovery model exist in SQL Server, and their role in the Backup strategy?
3. How does the Transaction log backup maintain log space in different recovery models?
4.In which system databases you can not Backup Transaction logs?
5. What is the differential and incremental backup in SQL Server?
6. What is Point-in-Time recovery in the SQL Server?
7. What is Copy-Only Backups feature in SQL Server?
8. Which System databases can not be backed up at all?
9. What is the Tail-end Transaction log backup, and how is it performed?
10. How is file or filegroup Backups helpful for very large databases?

Wednesday, April 21, 2010

Indexl Question 22 April 2010

  1. Can we alter the index?
  2. Which system are used in very large number of indexing system ?
  3. What happened if the clustered index is not unique ?
  4. where logged the entry of index in sql server ?
  5. what is default sorting order of index?
  6. which constraint is created index by default?
  7. what is use of DBCC showconfig?
  8. How to use fill factor with pad? Explain
  9. Explain hash join and merge join ?
  10. What is maximum size of row ?

Index Question -21 April 2010

Date: 20/04/2010
1. If a table has not any index column, table is unordered structure, what will say it unordered structure?
2. What is row locator?
3. Can we create non- clustered index, without clustered index?
4. Can cluster and non- clustered index can be unique?
5. What is role of partitions in table and index?
6. How many non-clustered indexes can be in a table?
7. How maintained the index, when table or view is modified?
8. What is spatial index?
9. What type of data type, where we not create the index?
10. Create Index testIndex on (columnname>
What type of index will get created after executing the above statement ?