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 Creating and Modifying an Index With SQL Creating and Modifying a Constraint With SQL Altering a Table With SQL Dropping a Table With SQL
1) 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. 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 in 2) Creating a Table with Constraints. -- create a table similar to the employees table in the HR schema (i.e you will develop HR sehma)
March 2017 1|Page
Kotebe Metropolitan University 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) );
3) 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; 4) Creating and Modifying a Constraint With SQL To add or modify a constraint on a table; use the SQL ALTER statement, as shown in Creating and Altering a Constraint. -- add a constraint a new constraint ALTER TABLE my_employees ADD CONSTRAINT ...
March 2017 2|Page
Kotebe Metropolitan University -- remove the constraint on email in the my_employees table ALTER TABLE my_employees DROP UNIQUE (email); 5) Altering a Table With SQL To alter a database object, such as a table, use the SQL ALTER statement, -- 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; 6) Dropping a Table With SQL To drop (remove completely) a table from the database uses the SQL DROP statement, as shown in Example: 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. -- drop tables from the database -- use caution when use the DROP statement! DROP TABLE my_birthdays; DROP TABLE temp_employees; 7) Example Do this on your SQL Command window CREATE TABLE "DEPT" ( "DEPTNO" NUMBER(2,0), "DNAME" VARCHAR2(14), "LOC" VARCHAR2(13), PRIMARY KEY ("DEPTNO") ENABLE ) ; CREATE TABLE "EMP" ( "EMPNO" NUMBER(4,0) NOT NULL ENABLE, "ENAME" VARCHAR2(10), "JOB" VARCHAR2(9), "MGR" NUMBER(4,0), "HIREDATE" DATE, "SAL" NUMBER(7,2), "COMM" NUMBER(7,2), "DEPTNO" NUMBER(2,0), PRIMARY KEY ("EMPNO") ENABLE ) ; ALTER TABLE "EMP" ADD FOREIGN KEY ("MGR") REFERENCES "EMP" ("EMPNO") ENABLE; ALTER TABLE "EMP" ADD FOREIGN KEY ("DEPTNO") REFERENCES "DEPT" ("DEPTNO") ENABLE;