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