Create a sequence and view, and create a sequence View

Source: Internet
Author: User

Create a sequence and view, and create a sequence View


The project must ensure that the order number is unique.


When the time is used to generate a large volume of business, there will still be duplicates.


Solution: Create a sequence table and view to retrieve from the View


CREATE SEQUENCE general_order_no_seq  INCREMENT 1  MINVALUE 1  MAXVALUE 9223372036854775807  START 1  CACHE 1;


CREATE OR REPLACE VIEW get_next_seq_view AS SELECT nextval('general_order_no_seq'::regclass) AS nextval;


Obtain:

ActiveRecord::Base.connection.execute('select * from get_next_seq_view;').first["nextval"].to_i




SQL code for creating a view

Amount
Create View
Create view VTest_A
AS
SELECT 'hello-world' AS

Query View
SELECT * FROM VTest_A

When creating a view, you can add an auto-increment column to the view.

Oracle uses Rownum

SQL Server uses ROW_NUMBER () OVER (ORDER BY (SELECT 0 ))

That is
Create view v _ VIEW name
SELECT
Rownum AS auto-incrementing sequence,
Other Columns
FROM
Table
WHERE
Condition

In this case, when you SELECT * FROM view, there is a column that is auto-incrementing in order.

Related Article

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.