Monday 12 January 2009

Create a database using OSDM (Oracle SQL Developer Modeling)

In this post I show you how easy it is to create a database using Oracle SQL Developer Data Modeling.


Suppose your database requirements are as simple as follows:

  • You want an event management database system. Not a calendar, but something where you can write events into.
  • You want to be able to invite friends to attend to these events.
  • You also want your friends/users to choose the types of events they like most.



Data model it with OSDM


Using OSDM you can quickly draw the data model with foreign keys and primary keys for your database design requirements above. Is not difficult to use just filling in dialog boxes about the properties of the tables.


(Click to enlarge)



OSDM will not only draw you the model, but will also give you the ability to extract the DDL and then run it on something like Oracle Apex's SQL command prompt. How great is that! I just hope that Oracle WILL give this tool free with Oracle SQL developer in later releases.



Extract the DDL from the Model

In OSDM use File -> Export -> DDL File to extract the SQL statements from the data model.

Using the simple OSDM you can extract the DDL from the above model and you get a nice SQL file with the CREATE TABLE and ALTER TABLE ... CONSTRAINT ... commands which will look like this:


-- Generated by Oracle SQL Developer Data Modeling Version: 1.5.1 Build: 518
-- at: 2009-01-12 21:56:49
-- for: Oracle Database 10g
-- Oracle Database 10g



CREATE TABLE EVENTS
(
EVENT_ID NUMBER NOT NULL ,
EVENT_DESCRIPTION CLOB ,
EVENT_DATE DATE ,
EVENT_TYPE_ID NUMBER NOT NULL
) LOGGING
;



ALTER TABLE EVENTS
ADD CONSTRAINT EVENTS_PK PRIMARY KEY ( EVENT_ID ) ;


CREATE TABLE USERS
(
USER_ID NUMBER NOT NULL ,
USER_EMAIL VARCHAR2 ,
USER_NAME VARCHAR2
) LOGGING
;



ALTER TABLE USERS
ADD CONSTRAINT USERS_PK PRIMARY KEY ( USER_ID ) ;


CREATE TABLE ATTEND
(
USER_ID NUMBER NOT NULL ,
EVENT_ID NUMBER NOT NULL ,
ATTEND_DATE DATE
) LOGGING
;




CREATE TABLE EVENT_TYPES
(
EVENT_TYPE_ID NUMBER NOT NULL ,
EVENT_TYPE_DESCRIPTION VARCHAR2 NOT NULL
) LOGGING
;



ALTER TABLE EVENT_TYPES
ADD CONSTRAINT EVENT_TYPES_PK PRIMARY KEY ( EVENT_TYPE_ID ) ;


CREATE TABLE USER_EVENT_PREFERENCES
(
USER_ID NUMBER NOT NULL ,
EVENT_TYPE_ID NUMBER NOT NULL
) LOGGING
;





ALTER TABLE EVENTS
ADD CONSTRAINT EVENTS_EVENT_TYPES_FK FOREIGN KEY
(
EVENT_TYPE_ID
)
REFERENCES EVENT_TYPES
(
EVENT_TYPE_ID
)
NOT DEFERRABLE
;


ALTER TABLE ATTEND
ADD CONSTRAINT ATTEND_USERS_FK FOREIGN KEY
(
USER_ID
)
REFERENCES USERS
(
USER_ID
)
NOT DEFERRABLE
;


ALTER TABLE ATTEND
ADD CONSTRAINT ATTEND_EVENTS_FK FOREIGN KEY
(
EVENT_ID
)
REFERENCES EVENTS
(
EVENT_ID
)
NOT DEFERRABLE
;


ALTER TABLE USER_EVENT_PREFERENCES
ADD CONSTRAINT USER_EVENT_PREFERENCES_USERS_FK FOREIGN KEY
(
USER_ID
)
REFERENCES USERS
(
USER_ID
)
NOT DEFERRABLE
;


ALTER TABLE USER_EVENT_PREFERENCES
ADD CONSTRAINT USER_EVENT_PREFERENCES_EVENT_TYPES_FK FOREIGN KEY
(
EVENT_TYPE_ID
)
REFERENCES EVENT_TYPES
(
EVENT_TYPE_ID
)
NOT DEFERRABLE
;



-- Oracle SQL Developer Modeling Summary Report:
--
-- CREATE TABLE 5
-- CREATE INDEX 0
-- ALTER TABLE 5
-- CREATE VIEW 0
-- CREATE PROCEDURE 0
-- CREATE TRIGGER 0
-- CREATE STRUCTURED TYPE 0
-- CREATE COLLECTION TYPE 0
-- CREATE CLUSTER 0
-- CREATE CONTEXT 0
-- CREATE DATABASE 0
-- CREATE DIMENSION 0
-- CREATE DIRECTORY 0
-- CREATE DISK GROUP 0
-- CREATE ROLE 0
-- CREATE ROLLBACK SEGMENT 0
-- CREATE SEQUENCE 0
-- CREATE SNAPSHOT 0
-- CREATE SYNONYM 0
-- CREATE TABLESPACE 0
-- CREATE USER 0
--
-- DROP TABLESPACE 0
-- DROP DATABASE 0
--
-- ERRORS 0
-- WARNINGS 0



To get Oracle SQL Developer Data Modeling go to Oracle's Website here

No comments: