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.