我们正在测试Oracle,并负责在Oracle上构建所有数据库对象(表,proc,触发器等),并且当前使用Microsoft SQL Server 2008 R2。我们几乎对所有ID列都使用uniqueidentifier。我使用此函数来创建GUID:
CREATE OR REPLACE FUNCTION NEWID RETURN CHAR IS guid CHAR(36) ; BEGIN SELECT SYS_GUID() INTO guid FROM DUAL; guid := SUBSTR(guid, 1, 8) || '-' || SUBSTR(guid, 9, 4) || '-' || SUBSTR(guid, 13, 4) || '-' || SUBSTR(guid, 17, 4) || '-' || SUBSTR(guid, 21); RETURN guid; END NEWID; /
但是现在我无法弄清楚如何在创建表时将其用作列的默认值。这是一个不起作用的示例:
CREATE TABLE "NonWorkingExample" ( "ID" CHAR(36) NOT NULL DEFAULT NEWID(), "UnitNumber" NUMBER(38) NOT NULL, "StartDateTime" TIMESTAMP NOT NULL, "EndDateTime" TIMESTAMP NULL, CONSTRAINT PK_RentalAgreements PRIMARY KEY ("ID") );
错误:
Error starting at line 1 in command: CREATE TABLE "NonWorkingExample" ( "ID" CHAR(36) NOT NULL DEFAULT NEWID(), "UnitNumber" NUMBER(38) NOT NULL, "StartDateTime" TIMESTAMP NOT NULL, "EndDateTime" TIMESTAMP NULL, CONSTRAINT PK_RentalAgreements PRIMARY KEY ("ID") ) Error at Command Line:3 Column:58 Error report: SQL Error: ORA-00907: missing right parenthesis 00907. 00000 - "missing right parenthesis" *Cause: *Action:
任何帮助将非常感激。谢谢你。
建议:
例如:
CREATE TRIGGER SetGUIDforTableXYZ BEFORE INSERT ON TableXYZ FOR EACH ROW BEGIN :new.ID := NEWID(); END;
那应该可以解决问题(假设我还没有弄乱语法)。