﻿
{"id":5456,"date":"2016-12-12T08:11:21","date_gmt":"2016-12-12T08:11:21","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=5456"},"modified":"2016-12-12T08:11:21","modified_gmt":"2016-12-12T08:11:21","slug":"sql-serverda-newsequentialid-fonksiyonu","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/sql-serverda-newsequentialid-fonksiyonu\/","title":{"rendered":"SQL Server&#8217;da NewsequentialID Fonksiyonu"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Biliyorsunuz ki, SQL Server&#8217;da tarih, saat vs. gibi bilgilerle birlikte bilgisayar\u0131n di\u011fer \u00f6zellikleri ile harmanlan\u0131p ortaya konulan ve d\u00fcnyada e\u015fi benzeri olmayacak cinsten \u00fcretilen de\u011feri UniqueIdentifier t\u00fcr\u00fcyle ifade etmekteyiz. Bu tipte bir de\u011feri NewID() fonksiyonu arac\u0131l\u0131\u011f\u0131yla \u00fcretebilmekte ve bu de\u011feri GUID(Globally Unique Identifier) olarak nitelendirmekteyiz. Benzer \u015fekilde NewsequentialID fonksiyonuda k\u00fc\u00e7\u00fck bir farkla ilgili de\u011feri \u00fcretmemizi sa\u011flamaktad\u0131r. Bu i\u00e7eri\u011fimizde NewsequentialID fonksiyonunun ne oldu\u011fundan ve NewID fonksiyonuyla olan fark\u0131ndan bahsedece\u011fiz.<\/p>\n<p>\u00d6ncelikle bu iki fakt\u00f6r\u00fcn aras\u0131ndaki farka de\u011finmekle ba\u015flayal\u0131m. NewID fonksiyonu ile NewsequentialID fonksiyonunun aras\u0131nda temel fark biri d\u00fczensiz de\u011fer \u00fcretirken(NewID) di\u011feri s\u0131ral\u0131 de\u011fer \u00fcretmektedir(NewsequentialID).<\/p>\n<p>Hemen bu girizgahtan sonra bir \u00f6rneklendirme yaparak bahsimizi kuvvetlendirmek gerekebilir. Lakin NewsequentialID fonksiyonunun kullan\u0131m\u0131 ek bilgiler gerektirdi\u011finden dolay\u0131 bu \u00f6rneklendirmeyi bir ka\u00e7 paragraf sonras\u0131na b\u0131rakmak mecburiyetindeyiz.<\/p>\n<p>NewID() fonksiyonu bildi\u011fimiz gibi her yerde(select, insert vs.) kullan\u0131labilirken, NewsequentialID fonksiyonu bir tek kolonlarda Default Constraint olarak kullan\u0131labilmektedir. Bu durumda a\u015fa\u011f\u0131daki gibi bir problemle kar\u015f\u0131la\u015fmaktay\u0131z.<\/p>\n<table style=\"width: 100%;\">\n<tbody>\n<tr>\n<td><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/SQL-Serverda-NewsequentialID-\u00d6zelli\u011fi.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5461\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/SQL-Serverda-NewsequentialID-\u00d6zelli\u011fi.png\" alt=\"SQL Server'da NewsequentialID \u00d6zelli\u011fi\" width=\"270\" height=\"105\" \/><\/a><\/td>\n<td><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/SQL-Serverda-NewsequentialID-\u00d6zelli\u011fi-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5462\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/SQL-Serverda-NewsequentialID-\u00d6zelli\u011fi-1.png\" alt=\"SQL Server'da NewsequentialID \u00d6zelli\u011fi\" width=\"483\" height=\"142\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/SQL-Serverda-NewsequentialID-\u00d6zelli\u011fi-1.png 483w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/SQL-Serverda-NewsequentialID-\u00d6zelli\u011fi-1-300x88.png 300w\" sizes=\"auto, (max-width: 483px) 100vw, 483px\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td>NewID<\/td>\n<td>NewsequentialID<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi NewsequentialID fonksiyonunu Select sorgusunda \u00e7a\u011f\u0131rd\u0131\u011f\u0131m\u0131zda kar\u015f\u0131m\u0131za bir de\u011fer \u00fcretip \u00e7\u0131karmamaktad\u0131r. Bunun sebebi yukar\u0131da bahsetti\u011fimiz gibi sadece kolon \u00fczerinde Default Constraint ile kullan\u0131lmas\u0131d\u0131r.<br \/>\nKi zaten al\u0131nan hataya \u015f\u00f6yle g\u00f6z atarsak meali a\u015fa\u011f\u0131 yukar\u0131 a\u015fa\u011f\u0131daki manaya gelmektedir.<\/p>\n<blockquote><p>\n<strong><em>NewsequentialID fonksiyonu , tablo \u00fcretirken yahut g\u00fcncellerken UniqueIdentifier t\u00fcr\u00fcnde bir s\u00fctun i\u00e7in Default ifadesiyle(Constraint) kullan\u0131labilir.<\/em><\/strong><\/p><\/blockquote>\n<p>Evet, bu demek oluyor ki a\u015fa\u011f\u0131daki gibi bir tablo in\u015fa eder ve UniqueIdentifier t\u00fcr\u00fcnden bir kolona Default Constraint ile NewsequentialID fonksiyonunu atarsak \u00f6rneklendirmede es ge\u00e7ti\u011fimiz paragraf\u0131n uygulama k\u0131sm\u0131n\u0131 tamamlam\u0131\u015f oluruz.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nCREATE TABLE Ogrenciler\r\n(\r\n\tID INT PRIMARY KEY IDENTITY,\r\n\tADI NVARCHAR(MAX),\r\n\tSOYADI NVARCHAR(MAX),\r\n\tOZELNO UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID()\r\n)\r\n<\/pre>\n<p>\u015eimdi bu tabloya rastgele veriler girelim ve NewsequentialID fonksiyonunun tan\u0131mland\u0131\u011f\u0131 kolonu inceleyelim.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nINSERT Ogrenciler(ADI, SOYADI) VALUES('Gen\u00e7ay', 'Y\u0131ld\u0131z')\r\nINSERT Ogrenciler(ADI, SOYADI) VALUES('Turgay', 'Arslaner')\r\nINSERT Ogrenciler(ADI, SOYADI) VALUES('Mevl\u00fct', 'Al\u00e7ay')\r\nINSERT Ogrenciler(ADI, SOYADI) VALUES('Kerem O\u011fuz', '\u00d6zg\u00fcr')\r\nINSERT Ogrenciler(ADI, SOYADI) VALUES('Selman', 'Candemir')\r\nINSERT Ogrenciler(ADI, SOYADI) VALUES('R\u0131fk\u0131', 'Malko\u00e7lar')\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/SQL-Serverda-NewsequentialID-\u00d6zelli\u011fi-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/SQL-Serverda-NewsequentialID-\u00d6zelli\u011fi-2.png\" alt=\"SQL Server&#039;da NewsequentialID \u00d6zelli\u011fi\" width=\"578\" height=\"155\" class=\"aligncenter size-full wp-image-5469\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/SQL-Serverda-NewsequentialID-\u00d6zelli\u011fi-2.png 578w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/SQL-Serverda-NewsequentialID-\u00d6zelli\u011fi-2-300x80.png 300w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/a><br \/>\nG\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere NewsequentialID fonksiyonu ile olu\u015fturulan de\u011ferlerimizde bir s\u0131ral\u0131l\u0131k s\u00f6z konusu. Amma velakin NewID fonksiyonundan b\u00f6yle bir s\u0131ra ve d\u00fczenden bahsetmemiz m\u00fcmk\u00fcn de\u011fildir. Keza bu g\u00fcne kadar say\u0131s\u0131z kez \u00f6rneklendirmeler \u00fczerinde kulland\u0131\u011f\u0131m\u0131z i\u00e7in NewID \u00fczerine bu i\u00e7eri\u011fimizde bir \u00f6rne\u011fe l\u00fczum g\u00f6rm\u00fcyorum&#8230;<\/p>\n<p>Gelelim bu s\u0131ral\u0131 de\u011ferlerin bizlere getirisi \u00fczerinde konu\u015fmaya&#8230; Yani NewID ile NewsequentialID fonksiyonlar\u0131 aras\u0131nda hangi durumlarda se\u00e7im yapmal\u0131y\u0131z? sorunsal\u0131na cevap uydurmaya&#8230;<\/p>\n<p>UniqueIdentifier t\u00fcr\u00fcnden de\u011ferler genellikle takip edilmesi zor, hesaplanmas\u0131 yahut tahmin edilmesi neredeyse imkans\u0131z de\u011ferler oldu\u011fu i\u00e7in y\u00fcksek g\u00fcvenlikli durumlarda kulland\u0131\u011f\u0131m\u0131z bir de\u011fer tipidir. Ee haliyle s\u0131ral\u0131 de\u011fer \u00fcreten NewsequentialID fonksiyonu bu t\u00fcr\u00fcn tahmin edilebilirlik katsay\u0131s\u0131n\u0131 artt\u0131rd\u0131\u011f\u0131ndan dolay\u0131 g\u00fcvenlik \u00fczerinde kararl\u0131ysan\u0131z NewID fonksiyonuyla \u00e7al\u0131\u015fman\u0131z tabi ki de daha do\u011fru bir tercih olacakt\u0131r. Lakin select, insert ve update i\u015flemlerinde NewsequentialID fonksiyonu ile \u00fcretilen de\u011fer, NewID&#8217;ye nazaran daha h\u0131zl\u0131 ve az maliyetli \u015fekilde \u00fcretilecektir.<\/p>\n<p>Bu olumlu ve olumsuz durumlar\u0131 g\u00f6z \u00f6n\u00fcne al\u0131p ihtiyac\u0131n\u0131za d\u00f6n\u00fck en \u00e7ok hizmeti ve e\u015fli\u011fi edecek fonksiyonu tercih etmek sizlere kalm\u0131\u015f&#8230;<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u015eimdilik ho\u015f\u00e7akal\u0131n&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Biliyorsunuz ki, SQL Server&#8217;da tarih, saat vs. gibi bilgilerle birlikte bilgisayar\u0131n di\u011fer \u00f6zellikleri ile harmanlan\u0131p ortaya konulan ve d\u00fcnyada e\u015fi benzeri olmayacak cinsten \u00fcretilen de\u011feri UniqueIdentifier t\u00fcr\u00fcyle ifade etmekteyiz. Bu tipte bir de\u011feri&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3415,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1082,22],"tags":[1569,1568,200,1566,1567,245,119],"class_list":["post-5456","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-server","category-veritabani","tag-default-constraint","tag-globally-unique-identifier","tag-guid","tag-newid","tag-newsequentialid","tag-sql-server","tag-uniqueidentifier"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=5456"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5456\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3415"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=5456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=5456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=5456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}