Professional Documents
Culture Documents
language)
ddl
objectives :
–list the types of ddl commands
–state & demonstrate the use of create table
command
–list & define the integrity constraints
■ what is ddl?
–data definition language is a set of commands used to
create, modify or drop various database objects such as
tables, views, synonyms etc.
ddl commands
■ create
–creating database objects
■ alter
–modifying database object’s definition/structure
■ drop
–removing/deleting database objects
be unique
–the column name must be specified with their data types
■ syntax :
naming rules
■ naming rules for oracle database objects :
–the name must begin with an alphabet
–names may contain letters, numerals, special characters
by you
–the name can not be a sql reserved word
integrity constraints
■ while creating a table, constraints can be specified
■ integrity constraints help you enforce business rules
on data in your database
■ sql rejects any value that violets the constraint
■ types of constraints
–column constraints
■ columns constraints apply to individual columns
–table constraints
■ table constraints apply to a group of one or more columns
■ constraints can be specified with create or alter
command
integrity constraints
■ advantages
–do not have to program your application to check for the
validity of data
–the integrity constraints for a table are global to all
programs/users using it
–easy to specify and maintain
centrally
constraints: null
■ this constraint is placed immediately after the column
definition
–null
■ column can contain null values
–not null
■ column can not contain null values
■ example :
create table emp
(empno number (4) not null, ……..);
constraints: unique
■ unique constraint
–ensures uniqueness of the values in a column(s)
■ example :
create table emp
(empno number (4) not null unique, ……..);
constraints: primary key
■ the primary key of a table should satisfy not null and
unique constraints
■ sql supports primary key definition directly with the
primary key constraint
■ only one primary key can be defined per table
■ example :
create table emp
( empno number (4) constraint emp_pk_empno primary
key, ……..);
■ example :
create table emp
( empno number (4), …..
constraint emp_pri_key primary key (empno) , ……..);
■ example :
create table emp
( …….., constraint emp_foreign_key foreign key (deptno)
references dept (deptno) on delete cascade,
…….. );
constraints: check
■ check constraint is used to enforce rules that must
be satisfied by each row
■ uses a logical expression or condition to define the
rule
■ pseudo columns such as currval, nextval, rownum or
subqueries are not allowed in the condition
■ call to sysdate, uid, user functions is not allowed
constraints: check
■ example 1 :
create table itemmaster
( ……..,
class char (1) check (class in (`a’, `b’, `c’)),
…….. );
■ example 2 :
create table itemmaster
( ……..,
class char(1) not null, ……..,
rate number (8,2) not null,…..,
check
((class = `a’ and rate < 1000)
or (class = `b’ and rate > 1000 and rate < 4500)
or (class = `c’ and rate > 4500)
)
…...);
constraints: default
■ the default integrity constraint is used to assign a
default value to be assigned to a column
■ the default value is assigned to a column if
subsequent insert statements omit the value for
the column
■ example 1 :
create table itemmaster
( ……..,
qoh number(5) default 100,
…….. );
■ example 2 :
create table emp
( ……..,
hiredate date default sysdate,
……..);
constraints: enable/disable
■used with the other integrity constraints to enable or disable
an integrity constraint
■used with create / alter statement
■example 1 :
■ syntax :
–desc[ribe] <table_name>
■ example :
–desc emp
creating a table using subquery
■ you can create a table from the existing table(s) by
using an appropriate subquery
■ syntax :
create table <table_name>
[(column_name, ……..)]
as
select statement;
■ syntax :
alter table <table_name>
[ modify] | [add ] (column_definitions),
[ enable | disable] [constraint constraint_name ];
■ example :
alter table emp
add ( address varchar2 (20) );
■ the option is
–make it null first
–subsequently fill the data
■ example :
alter table dept
drop primary key
cascade;
■ syntax :
rename <old_table_name> to <new_table_name>;
■ example :
rename dept to department;
truncate table
■ syntax :
truncate table table_name;
–removes all row data quickly
–once command is executed data in table cannot be
recovered
–table structure remains as it is including definition of