To create an Oracle data representation SQL

Source: Internet
Author: User

CREATE TABLE "Brand_relation"
(
"ID" number (10,0) is not NULL ENABLE,
"CATID" number (10,0) is not NULL ENABLE,
"brand_id" number (10,0) is not NULL ENABLE,
PRIMARY KEY ("ID")
);
CREATE SEQUENCE "Br_id_seq" NOCACHE;


CREATE INDEX "Br_catid" On "Brand_relation" ("CATID");
CREATE INDEX "br_brand_id" On "Brand_relation" ("brand_id");

CREATE OR REPLACE TRIGGER ' BR_ID_TRG ' before INSERT on ' brand_relation ' referencing old as ' old ' new as ' new ' for each ROW ENABLE
DECLARE
V_newval Number (12): = 0;
V_incval Number (12): = 0;
BEGIN
IF INSERTING AND:new.id is NULL then
SELECT Br_id_seq. Nextval to V_newval from DUAL;
-If This is the first time this table has been inserted into (sequence = = 1)
IF V_newval = 1 Then
--get the max indentity value from the table
SELECT NVL (max (ID), 0) into V_newval from Brand_relation;
V_newval: = V_newval + 1;
--set the sequence to that value
LOOP
EXIT when v_incval>=v_newval;
SELECT Br_id_seq.nextval to v_incval from dual;
END LOOP;
END IF;
--used to emulate last_insert_id ()
--mysql_utilities.identity: = V_newval;
--Assign the value from the sequence to emulate the identity column
: new.id: = V_newval;
END IF;
END;

To create an Oracle data representation SQL

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.