Using SQL Data Definition Language Statements

Data definition language (DDL) statements include CREATE, ALTER, and DROP for defining database objects. When managing database objects, SQL Developer provides a simple and easy-to-use interface that can be utilized instead of SQL DDL statements.

In this guide, some basic SQL DDL statements are used in the code examples and a brief description of some DDL statements are discussed here.

This topic includes the following topics:

Creating a Table With SQL

To create a database object, such as a table, use the SQL CREATE statement as shown in Example: Creating a Simple Table. When you create a table, you need to provide data types for each column. For more information about tables, see Managing Tables.

Creating a Simple Table

-- create a simple table for keeping track of birthdays
CREATE TABLE my_birthdays
  ( first_name     VARCHAR2(20),
    last_name      VARCHAR2(25),
    bday_date      DATE
  ); 

Optionally, you can provide constraints as shown inExample: Creating a Table With Constraints. The use of constrains is discussed in Ensuring Data Integrity With Constraints.

Creating a Table With Constraints

-- create a table similar to the employees table in the HR schema
CREATE TABLE my_employees
  ( employee_id    NUMBER(6),
    first_name     VARCHAR2(20),
    last_name      VARCHAR2(25) CONSTRAINT my_emp_last_name_nn NOT NULL,
    email          VARCHAR2(25) CONSTRAINT my_emp_email_nn NOT NULL,
    phone_number   VARCHAR2(20),
    hire_date      DATE DEFAULT SYSDATE CONSTRAINT my_emp_hire_date_nn NOT NULL,
    job_id         VARCHAR2(10) CONSTRAINT my_emp_job_nn NOT NULL,
    salary         NUMBER(8,2) CONSTRAINT  emy_mp_salary_nn NOT NULL,
    commission_pct NUMBER(2,2),
    manager_id     NUMBER(6), 
    department_id  NUMBER(4),
    CONSTRAINT     my_emp_salary_min CHECK (salary > 0),
    CONSTRAINT     my_emp_email_uk UNIQUE (email)
  ); 

Creating and Modifying an Index With SQL

To create, modify, or drop an index, use the SQL CREATE, ALTER, or DROP INDEX statement as shown in Example: Creating, Modifying, and Dropping an Index.

Creating, Modifying, and Dropping an Index

-- create a new index on the employees table using the email column
CREATE INDEX email_ix 
  ON employees (email);

-- disable the index
ALTER INDEX email_ix 
  RENAME TO my_email_ix;

-- drop the index
DROP INDEX my_email_ix;

-- create an index on a single column to make queries faster on that column
CREATE INDEX emp_last_name_ix ON employees (last_name);
DROP INDEX emp_last_name_ix;

-- create an index on two columns to make queries faster on the first column
-- or both columns
CREATE INDEX emp_mgr_id_ix ON employees (employee_id, manager_id);
DROP INDEX emp_mgr_id_ix;

-- a function-based index precalculates the result and speeds up queries that
-- use the function for searching or sorting, in this case UPPER(last_name)
CREATE INDEX emp_upper_last_name_ix ON employees (UPPER(last_name));
DROP INDEX emp_upper_last_name_ix;

Creating and Modifying a Constraint With SQL

To add or a modify a constraint on a table, use the SQL ALTER statement as shown in Example: Creating and Altering a Constraint.

Creating and Altering a Constraint

-- add a constraint a new constraint
ALTER TABLE my_employees 
  ADD CONSTRAINT ...

-- remove the constraint on email in the my_employees table
ALTER TABLE my_employees
  DROP UNIQUE (email); 

Altering a Table With SQL

To alter a database object, such as a table, use the SQL ALTER statement as shown in Example: Altering a Table.

Altering a Table

-- add a new column to my_birthdays
ALTER TABLE my_birthdays
  ADD (age NUMBER(3));

-- rename the my_employees table
ALTER TABLE my_employees RENAME to temp_employees; 

Dropping a Table With SQL

To drop (remove completely) a table from the database use the SQL DROP statement as shown inExample: Dropping a Table. Be very careful when using the DROP statement to remove database objects.

If you want to delete the rows in the table and keep the table, use the DELETE statement. See "Deleting Data With the DELETE Statement".

Dropping a Table

-- drop tables from the database
-- use caution when use the DROP statement!
DROP TABLE my_birthdays;
DROP TABLE temp_employees;

Creating and Dropping a Sequence

Example: Creating a Sequence creates a sequence that can be used with the employees table. The sequence could also be used with other tables. For more information on sequences, see Managing Sequences.

Creating a Sequence

-- create a new sequence to use with the employees table
CREATE SEQUENCE new_employees_seq START WITH 1000 INCREMENT BY 1;

-- to use the sequence, first initialize the sequence with NEXTVAL
SELECT new_employees_seq.NEXTVAL FROM DUAL;

-- after initializing the sequence, use CURRVAL as the next value in the sequence
INSERT INTO employees VALUES 
  (new_employees_seq.CURRVAL, 'Pilar', 'Valdivia', 'pilar.valdivia',
  '555.111.3333', '01-SEP-05', 'AC_MGR', 9100, .1, 101, 110);

-- query the employees table to check the current value of the sequence
-- which was inserted used as employee_id in the previous INSERT statement
SELECT employee_id, last_name FROM employees WHERE last_name = 'Valdivia';

Example: Dropping a Sequence drops the sequence that you previously created.

Dropping a Sequence

-- drop the sequence
DROP SEQUENCE new_employees_seq;

Creating and Dropping a Synonym

Example: Creating a Synonym creates a synonym that is alias for the employees table. For more information on synonyms, see Managing Synonyms.

Creating a Synonym

-- create a synonym for the employees table
CREATE SYNONYM emps for HR.employees;

-- query the employees table using the emps synonym
SELECT employee_id, last_name FROM emps WHERE employee_id < 105;

Example: Dropping a Synonym drops a synonym.

Dropping a Synonym

-- drop the synonym
DROP SYNONYM emps;