Guid,newid, for the id__ database in a database table

Source: Internet
Author: User
As we all know, the IDs in database tables are not repeatable, you can automatically fill out the growth ID by identifying the ID column as identity when you create the table, without requiring personal maintenance.
However, due to various reasons, the initial ID column of the table is not set up identity then you need a method of service identity to assign a value to the ID.
The ID column is not repeatable, it can be text or a number.
There are two methods in SQL that provide NEWID () and GUID () to generate a value that is not duplicated. So let's explore the ID column identified as identity can automatically fill in the growth ID, no need for personal maintenance.
However, due to various reasons, the initial ID column of the table is not set up identity then you need a method of service identity to assign a value to the ID.
The ID column is not repeatable, it can be text or a number.
SQL provides both NEWID () and GUID () methods to generate a duplicate and GUID ()

For a comparison of the two methods, you can see the difference between the blog GUID and the ID. Here is not a thin table.

This is a major illustration of how to add IDs in T-SQL and C # code when IDs are numeric types.

Random functions commonly used in C # to get numbers


GUID ()


Bitconverter.toint64 (Guid.NewGuid (). Tobytearray (), 0);


Random ()
A = new Random (). Next (99);


The random functions commonly used in T-SQL are NEWID () and Rand ()
Random function: rand ()
Executing in Query Analyzer: Select rand (), you can see the result will be similar to the random decimal: 0.36361513486289558, such as decimals in the actual application is not much, the general to take random numbers will take random integers. Look at the following two methods of randomly taking integers:


1,
The number of A:select floor (rand () *n)---generated is this: 12.0
B:select CAST (Floor (rand () *n) as int)---the number generated is this: 12


2,
The number of A:select ceiling (rand () * N)---Generated is this: 12.0
B:select cast (Ceiling (rand () * N) as int)---the number generated is this: 12


Where the n is an integer you specify, such as 100, you can see that the a method of the two methods is a decimal with a. 0, and the B method is a real integer.
Generally speaking, the two methods are no different, really no difference. There's a little bit of that, that's the range of their generated random numbers:
The number range of Method 1:0 through N-1, such as CAST (Floor (rand () *100) as int), generates any integer between 0 and 99
Method 2 has a range of digits between 1 and N, such as cast (ceiling rand () () as int), which generates any integer between 1 and 100
For this distinction, look at SQL's online Help for a cicada:


So this sentence can generate random numbers.
Select CAST (Ceiling (rand () * 1000000000) as int)


Random function newid ()
The type of NewID () is uniqueidentifier, and only the uniqueidentifier type can be converted to a char type, converted to a number
Statements are as follows
Select CONVERT (Int,ascii (NEWID ())
This statement is the first number of newid (), this method of repetition rate can be quite high, forgive the villain ignorant, if there are methods through the NEWID () to get random numbers please enlighten.



Reference articles Oracle numeric functions, character type functions, date functions, conversion functions, TO_CHAR, automatic type conversions, date type conversions

SQL NEWID () random function http://blog.163.com/szs1980@126/blog/static/478922032011102512838336/



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.