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