Oracle: DDL


Published on

Oracle: DDL

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Oracle: DDL

  1. 1. 1<br />Using ORACLE®<br />Data Definition Language<br />(Creating our sample database) <br /> Indexes and Views<br />
  2. 2. 2<br />DATA DEFINATION LANGUAGE<br />Data Definition language abbreviated as DDL refers to the set of commands used to define the data containers in Oracle like Tables and also defining Views and Indexes.<br />Basically there are 3 DDL commands viz: <br />
  3. 3. 3<br />THE CREATE COMMAND<br />The “CREATE” command can be used to create a table, view, index etc.<br />Let us look at creating a table:<br />In order to create a table a user must have the privileges to create tables i.e.<br /> The “CREATE TABLE” privilege.<br />The basic syntax to create a table is :<br />CREATE TABLE [schema.]TABLENAME<br /> ( colm1_name datatype[size] [colm_level_constraint],<br /> colm2_name datatype[size]…<br /> .<br /> . <br /> [table level constraints]<br /> [ON COMMIT clause] or [ON UPDATE clause] };<br />Constraints enforce certain rules on the table to maintain data integrity and quality.<br />
  4. 4. 4<br />NAMING CONVENTIONS<br />Within the table definition the user has to specify :<br />Table name.<br />Column name.<br />Column data type.<br />Size of the column.<br />Constraint [ if any].<br />The naming conventions for the table name and column names are:<br />Must begin with a character(either in lower or upper case) and may be up to 30 characters long.<br />Can contain a combination of alphabets(A-Z & a-z), numbers(0-9),_, $ and #.<br />Must not be a previously specified name for another oracle object owned by the same user and must not be an Oracle reserved keyword.<br />
  5. 5. 5<br />DATATYPES<br />Following the column name every column must be specified with a data type. Data types provided by Oracle are:<br />
  6. 6. 6<br />CONSTRAINTS<br />
  7. 7. 7<br />THE CREATE COMMAND<br />Another keyword “DEFAULT” is used to specify the value to be inserted if the user does not enter a value.<br />
  8. 8. 8<br />THE CREATE COMMAND<br />Constraints can be given a name or the Oracle server itself names the contraints in a SYS_Cn format where n stands for the constraint number.<br />To name a constraint the syntax is :<br />CREATE TABLE TABLE_NAME(colm1_name datatype[size] [CONSTRAINT] constraint_name1 constraint, column level constraint<br />.<br />.<br />CONSTRAINT constraint_name2 constraint(colm2_name));<br />table level constraint<br />Constraints can either be defined at column level or table level.<br />You can notice that constraint_name1 is defined at a column level and constraint_name2 is defined at a table level. All constraints except “NOT NULL” can be defined at table level.<br />
  9. 9. 9<br />EXAMPLE TABLE CREATION<br />To get a better grasp of things let us create a set of example tables covering one by one the points leant in the previous slides:<br />EXAMPLE 1:<br />CREATE TABLE MySchema.InfoTable ( name varchar2(15), <br /> age number(3), <br /> phone number(10) );<br />
  10. 10. 10<br />TABLE WITH CONSTRAINTS<br />The previous table contained no constraints .We will create a new table with the following constraints:<br />
  11. 11. 11<br />TABLE WITH CONSTRAINTS<br />The SQL code for creating the above table is:<br />CREATE TABLE ConTable<br /> (<br /> ID number(5),<br /> name varchar2(15)CONSTRAINTS ConTable_name NOT NULL,<br /> age number(3) DEFAULT 18,<br /> phone number(10)CONSTRAINTS ConTable_phone_unique UNIQUE,<br /> city varchar2(15)CONSTRAINTS ConTable_city_check<br /> CHECK(city IN( ‘MUMBAI’,’PUNE’,’HYDERABAD’)),<br /> CONSTRAINTS<br />ConTable_pk PRIMARY KEY (ID),<br />ConTable_fk FOREIGN KEY(ID) REFERENCES CustDetails(ID)<br /> ); <br /> Here we have defined NOT NULL constraint at a column level while the PRIMARY KEY constraint at the table level.<br />
  12. 12. 12<br />CREATING TABLES USING THE AS CLAUSE (SUBQUERY)<br />In the previous examples we have created tables but not populated them.<br />There a method of creating tables and populating them with data from other tables.<br />It is the method of using subqueries:<br />SYNTAX:<br />CREATE TABLE TABLE_NAME (colm_namedatatype(size) [constraint]…<br /> …<br /> .. )<br />AS<br />SUBQUERY(the SELECT statement);<br />
  13. 13. 13<br />CREATING TABLES USING THE AS CLAUSE (SUBQUERY)<br />Consider the table in EXAMPLE1 to be populated with the data:<br />Now we will create a table InfoTable40 and populate it with data <br />from the above table. <br />EXAMPLE3:<br />CREATE TABLE InfoTable40<br />AS<br />( SELECT * FROM Infotable WHERE age=40);<br />On viewing the data in InfoTable40 we see:<br />i.e. It contains the rows from InfoTable with age = 40 as per <br />the SELECT subquery.<br />Hence we have created and populated a table in one statement.<br />
  14. 14. 14<br />ALTERING TABLES<br />Once a table is created it structure can be altered using the ALTER command.<br />Using the alter command we can<br />Add a column or constraint or Default value.<br />Modify a column.<br />Delete a column.<br />The ALTER command can be used along with the following keywords:<br />
  15. 15. 15<br />ALTERING THE EXAMPLE TABLE <br />The SYNTAX for ALTER command is:<br />ALTER TABLE TABLE_NAME [ADD/MODIFY/DROP] (colm_namedatatype[size] [constraint]);<br />Consider the EXAMPLE1 table InfoTable:<br />We will alter the table in three steps:<br />First modify the precision of “age” column to 4.<br />Add a new column “city”.<br />Drop the column “phone” .<br />
  16. 16. 16<br />ALTERING TABLES<br />MODIFYING:<br />ALTER TABLE InfoTable MODIFY(age number(4) DEFAULT 20);<br />ADDING:<br />ALTER TABLE InfoTable ADD(city varchar2(15));<br />DROPPING:<br />ALTER TABLE InfoTable drop(phone);<br />
  17. 17. 17<br />DROPPING TABLES<br />Once a tables is created and populated, we can drop the table using the DROP command.<br />Once the DROP command has been exercised :<br />It cannot be reversed and the data and structure are lost permanently <br />Also dropping a table renders the views and indexed referring to the table invalid.<br />Also drops the all its constraints.<br />The SYNTAX for the DROP command is:<br />DROP TABLE TABLE_NAME;<br />EXAMPLE:<br />DROP TABLE InfoTable;<br />DESC InfoTable;<br />
  18. 18. 18<br />CREATING OTHER ORACLE OBJECTS<br />The CREATE command can not only be used to create tables but also other database objects like views, indexes and sequences.<br />
  19. 19. 19<br />CREATING A VIEW<br />A view is a logical representation of a subset of data from one or more tables/views.<br />The advantages of a view are:<br />Restrict data access.<br />Simplifying queries for the end user.<br />Displaying the same data in different views.<br />A view can be updatable (manipulate base table using view) or a read only view.<br />A view can be created using a CREATE command.<br />The SYNTAX to create a view is:<br />CREATE [OR REPLACE] VIEW VIEW_NAME<br />[ alias , alias…]<br />AS SUBQUERY a select statement<br />[WITH CHECK OPTION] specifies the only the rows displayable in the view can be inserted or updated<br />[WITH READ ONLY] specifies the DML operations cannot be performed using the view.<br />
  20. 20. 20<br />EXAMPLE VIEW CREATION<br />CREATE VIEW InfoTable_View<br />AS<br />SELECT * FROM InfoTable WHERE age = 40;<br />SELECT * FROM InfoTable_View;<br />VIEW WITH ALIAS:<br />CREATE VIEW InfoTable_ViewAlias<br />AS<br />SELECT name ENAME,ageEAGE,phone CONTACT_NO FROM InfoTable WHERE age = 40;<br />SELECT * FROM InfoTable_ViewAlias ;<br />
  21. 21. 21<br />ALTERING AND DROPPING A VIEW<br />ALTERING the InfoTable_View view:<br />CREATE OR REPLACE VIEW InfoTable_View<br />AS<br />SELECT * FROM InfoTable WHERE age > 40; <br />SELECT * FROM InfoTable_View;<br />DROPPING A VIEW:<br />DROP VIEW InfoTable_View;<br />SELECT * FROM InfoTable_View;<br />
  22. 22. 22<br />CREATING AN SEQUENCE<br />A sequence is an object that can generate a series of numbers.<br />A sequence can be shared by other objects and it speeds up accessing a sequence if cached in memory.<br />The basic syntax to create an sequence is:<br />CREATE SEQUENCE SEQUENCE_NAME<br />[INCREMENT BY n] - value by which it must be incremented<br />[START WITH n] - value from which sequence generation must begin.<br />[MINVALUE n] - specifies a maximum value the sequence can generate. Default is 10^26 for ascending sequence and 1 for descending sequence.<br />[MAXVALUE n] - specified the minimum value for a sequence generate. Default is 1 for ascending sequence and 10^26 for descending sequence.<br />[CYCLE / NOCYCLE ] - specifies whether once maximum value is reached can it cycle from the start value.<br />[CACHE n / NOCACHE ] - specifies how many sequence values can be stored in the cache for quick access.<br /> Once a sequence is created we can assess it using two methods:<br />CURRVAL : returns the current value of the sequence number.<br />NEXTVAL : returns the next value in the sequence and it returns a unique value even when being used by multiple tables.<br />
  23. 23. 23<br />EXAMPLE SEQUENCE<br />We create an example sequence.<br />CREATE SEQUENCE ascseq<br />INCREMENT BY 5<br />MAXVALUE 100<br />CYCLE<br />NOCACHE;<br />The above sequence an be accessed by ascseq.currval or ascseq.nextval and can be used to generate a primary key.<br />USAGE IN A TABLE:<br />INSERT INTO ConTable VALUES(ascseq.nextval,'bill' ,40,9000666000 ,'PUNE' );<br />
  24. 24. 24<br />ALTERING AND DROPPING SEQUENCE<br />To alter a sequence we use the ALTER commnad.<br />SYNTAX: EXAMPLE:<br />ALTER SEQUENCE SEQUENCE_NAME ALTER SEQUENCE ascseq<br />[INCREMENT BY n] INCREMENT BY 10 <br />[MINVALUE n] <br />[MAXVALUE n] MAXVALUE 200 <br />[CYCLE / NOCYCLE ] NOCYCLE <br />[CACHE n / NOCACHE ]; CACHE 10;<br />The START WITH parameter cannot be altered once a sequence is created.nly future values will be affected. Hence the MAXVALUE must be greater than at least the value last generated.<br />DROPPING A SEQUENCE: EXAMPLE:<br />DROP SEQUENCE SEQUENCE_NAME; DROP SEQUENCE ascseq;<br />
  25. 25. 25<br />CREATING AN INDEX<br />An index is an object that creates a rapid path of access to the rows of specified tables and helps in improving the retrieval of data .<br />The index is independent of the table and is used and maintained by the Oracle server.<br />Oracle server automatically creates indexes on columns of a table having Primary key or Unique as constraints. Users can manually create Indexes for other non key columns.<br />SYNTAX: Index on phone column:<br />CREATE INDEX INDEX_NAME CREATE INDEX ConTable_Idx<br />ON TABLE_NAME( column1,…[column n]); ON ConTable (phone);<br />DROPPING AN INDEX:<br />DROP INDEX INDEX_NAME; DROP INDEX ConTable_Idx;<br />Create an Index only when the column contains a large number of rows, is frequently used and is present in queries returning a large number of rows to avoid wastage of resources.<br />