SEQUENCE in SQL Server
Sequence is a set of integers that are ordered by order. Sequences are often used in databases because meeting the requirements of many applications is to require each row in a table to contain a unique value similar to the primary key.
The article will give you the syntax and examples of how to create and delete sequences in SQL Server.
CREATE SEQUENCE (Create Sequence)
Syntax
To create a sequence, we have the following syntax:
CREATE SEQUENCE [schema.] Sequence_name
[AS datatype]
[START WITH value]
[INCREMENT BY value]
[MINVALUE value | NO MINVALUE]
[MAXVALUE value | NO MAXVALUE]
[CYCLE | NO CYCLE]
[CACHE value | NO CACHE];
Parameters:
- AS datatype: can be BIGINT, INT, TINYINT, SMALLINT, DECIMAL, or NUMERIC types. If you do not specify a specific type, the program will default to your datatype in BIGINT form.
- START WITH value: The value that the sequence returns.
- INCREMENT BY value : A sequence of rules for increasing / decreasing sequence, which can be positive or negative. If the value here is positive, the sequence will be a sequence of incremental values. The opposite is negative, the sequence will decrease.
- MINVALUE value : The smallest value in the string.
- NO MINVALUE : Do not specify the minimum value.
- MAXVALUE value : Maximum value in the string.
- NO MAXVALUE : Do not specify the maximum value.
- CYCLE : Sequence will start from the beginning when it completes the sequence.
- NO CYCLE : Sequence will fail at the end of the string, it will not start again when completed.
- CACHE value: Save in cache (cache) to minimize disk IO.
- NO CACHE: Do not save in cache.
For example
CREATE SEQUENCE contacts_seq
AS BIGINT
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999
NO CYCLE
CACHE 10;
Here we have created a sequence named contacts_seq , starting from value 1, each of the following values increases by one unit (ie 2, 3, 4 .). The string will store about 10 values into the cache. The maximum value in the sequence is 99999 and it will not restart the string after the sequence ends at the largest value.
You can also execute the above command simply as follows:
CREATE SEQUENCE contacts_seq
START WITH 1
INCREMENT BY 1;
So now you've created a sequence that simulates an autonumber field. Next, to output a value from this sequence we use the NEXT VALUE FOR command
SELECT NEXT VALUE FOR contacts_seq;
This statement will retrieve the next value from contacts_seq. Then use the necessary commands for your execution. For example:
INSERT INTO contacts
(contact_id, last_name)
VALUES
(NEXT VALUE FOR contacts_seq, 'Smith');
This INSERT statement inserts a new record into the contact table. The contact_id field will be assigned the next number from contacts_seq, the last_name field will be 'Smith'.
DROP SEQUENCE (Delete Sequence)
Once a sequence has been created, there will also be cases where you want to remove the sequence from the database for several reasons.
Syntax
To delete a sequence, we have the following syntax:
DROP SEQUENCE sequence_name;
Parameters:
sequence_name: The sequence name you want to delete.
For example
DROP SEQUENCE contacts_seq;
By executing this command, you have just removed the contacts_seq string from the database.
Sequence properties
To test sequence properties, we have the following syntax:
SELECT * FROM sys.sequences WHERE name = ' sequence_name' ;
Parameters:
sequence_name: The name of the string to test the property.
For example
SELECT *
FROM sys.sequences
WHERE name = 'contacts_seq';
This example queries information from the sys.sequences system and retrieves the result for the contacts_seq string .
Sys.sequences system includes the following columns:
COLLECTION NOTE name of the sequence created in the statement CREATE SEQUENCE object_id ID of the principal_id object of the sequence ID principal (numeric value) schema_id Schema ID of sequence parent_object_id ID of the parent object type SO type_desc SEQUENCE_OBJECT create_date Date / Time created sequence with the CREATE SEQUENCE command modify_date Date / Time of last modification sequence is_ms_shipped Value 0 or 1 is_published Value 0 or 1 is_schema_published Value 0 or 1 start_value Start value of the sequence increment Rule value of sequence increase / decrease minimum_value Minimum value in string maximum_value Maximum value in is_cycling string Value 0 or 1. 0 = NO CYCLE, 1 = CYCLE is_cached Value 0 or 1, 0 = NO CACHE, 1 = CACHE cache_size Memory size buffer when is_cached = 1 system_type_id ID system of sequence user_type_id ID worry at sequence precision user Maximum accuracy for data type of maximum range scale for the sequence of sequence current_value Last value retrieved from sequence is_exhausted Value 0 or 1. 0 = Multiple values in sequence. 1 = No value
Previous article: Declare variables in SQL Server
Next lesson: FUNCTION (Function) in SQL Server
You should read it
- Truth Arena 9.16: Top team is easy to win
- String (String) in PHP
- Create private cloud with Hyper-V (Part 3)
- Auto-Increment Sequence in MongoDB
- How to write leading zeros in Excel
- Year / year, fourth / fourth, fashion / one ... how to read in the natural sequence?
- How to convert a sequence of numbers into dates in Excel
- How to Create a Spirallic Numbers Dataset
May be interested
- New points in SQL Server 2017the sql server 2017 version is primarily connected to linux, bringing the power of sql to linux. in short, you can install sql server 2017 on linux, using sql server 2017 on linux-based docker containers. sql server 2017 also allows you to choose development languages, develop it on-premise or cloud-based.
- Instructions for setting up and managing FTP Server on Windows 10if you want to create a private cloud for sharing and converting large files without restrictions, you can create an ftp server (file transfer protocol server) on your windows 10 computer.
- Create VPN Server on Windows 8no need to install any additional applications, you can easily 'turn' your computer into a vpn server if you're using windows 8. in this way, you can share data from the computer. as a simple lan system in the form of remote access. & a
- What is the future of server virtualization?server virtualization can help combat poor server performance, make better use of computing capabilities, limit energy consumption and improve data center flexibility.
- Learn about the architecture of MS SQL Serverin the previous articles, you already know briefly about sql server, how to install sql server on the computer. in this section we will learn about the architecture of sql server.
- Instructions for installing MS SQL Serverthis is a step by step guide to installing ms sql server.
- How to install DNS Server on Windows Server 2019from microsoft, the domain name system (dns) is one of the industry standard protocol sets that includes tcp / ip, along with dns client and dns server that provide name resolution services that map names to the ip addresses of computers. .
- Install Windows Server 2003 and create a backup servernetwork management documentation server 2003 as details of how to install, create a companion server in case the main server is malfunctioning, create a domain, join the client to the domain, the conection {remote desktop from the client data to server}, set adsl router parameters to server, turn off 1 working machine immediately.
- Instructions to change DNS Server on Windows, Mac, iOS and Androidby default, your computer will automatically obtain dns information from your isp (isp). sometimes, dns servers are unstable and you are blocked from accessing certain websites. or the default dns server is blocked by some websites for a number of reasons. in this case you should change the dns server with the free public dns server.
- How to configure DNS Server on Ubuntu Server 11.04dns server is a server with domain name resolution function. in this article, we will detail the steps to install and configure dns server on linux with ubuntu server version 11.04.