You are on page 1of 12

sql – ddl (data definition

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

creating database tables


■ tables are created using the create table command
–tables are owned by the user who creates them
–the names of all the tables owned by a given user must

be unique
–the column name must be specified with their data types
■ syntax :

create table <table_name>


(
<column_name> <datatype (size)>
[default<expr>]
[constraint <constraint_name>]
[<column_constraint>]
[ enable / disable],
……..
[constraint <constraint_name>]
[<table_constraint>]
[ enable / disable],
…………
);

naming rules
■ naming rules for oracle database objects :
–the name must begin with an alphabet
–names may contain letters, numerals, special characters

like _ (underscore), $ (dollar), # (hash)


–maximum length is 30 characters

–names are not case sensitive

–the name must not be duplicate of another object created

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

–when business rules change, constraints can be modified

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

■ primary key: column constraint


–if based on a single column then can be defined as a column
constraint
–its definition is placed after column definition

■ example :
create table emp
( empno number (4) constraint emp_pk_empno primary
key, ……..);

■ primary key: table constraint


–the primary key can also be based on multiple columns. if so, then
it is defined as a table constraint
–table constraint is placed after the last column definition

■ example :
create table emp
( empno number (4), …..
constraint emp_pri_key primary key (empno) , ……..);

constraints: foreign key


■ tables are related to each other by one or more
columns
■ a foreign key is used in a relationship with either a
primary key or a unique key of the same table or
another table
■ defines a referential integrity constraint
■ references clause
–used with foreign key definition to define referential
integrity
constraint

constraints: foreign key


■ example1 : (foreign key as a column constraint)

create table emp


( empno number(4) ……..,
deptno number (2) references dept (deptno), …….
);

■ example2 : (foreign key as a table constraint) create table emp


( ……..,
constraint emp_foreign_key
foreign key (deptno)
references dept (deptno),
…….. );

■ on delete cascade clause


–additional option used with foreign key constraint
–allows deletion of the parent row and its dependent rows

from the child table

■ 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 :

create table itemmaster


( ……..,
class char (1) check (class in (`a’, `b’, `c’)) disable,
…….. );

creating a table : an example


create table emp
( empno number (4) constraint con_pri_key primary key,
ename varchar2 (10) constraint con_chk_upper
check (ename = upper(ename)),
job varchar2 (10),
mgr number (4) references emp(empno),
hiredate date,
sal number (7,2),
comm number (7,2),
deptno number (2) references dept (deptno)
);

displaying the table structure


■ sql*plus command describe is used

■ 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;

■ the rows retrieved from the subquery will be added


to the newly created table
■ column name and data type will be exactly same as
the column list of the subquery unless the column
list is supplied explicitly
■ in the column list before as clause,
–you can specify the column names only
–data type and size can not be given

–constraint information is not inherited from the table used

in subquery (except not null constraint)

creating a table using subquery


■ example1 :
create table emp1
as
(select empno, ename, job from emp);
■ example 2 :
create table emp2 (employee, designation)
as
( select empno, job from emp
where empno is null);

sql - ddl(data definition language)


objectives :
–state & demonstrate the use of alter table command
–state & demonstrate the use of drop table command

modifying the table structure


■ after a table has been created and has been in use,
you might want to alter its structure
■ you can change the structure of table in three ways
–adding columns to the table
–redefining existing column definitions

–redefining integrity constraints

■ alter table command is used to modify the table


structure.

■ syntax :
alter table <table_name>
[ modify] | [add ] (column_definitions),
[ enable | disable] [constraint constraint_name ];

modifying the table structure


■ adding columns to a table :
■ syntax :
alter table <table_name>
add (<column_name> <datatype>(size), ……..);

■ example :
alter table emp
add ( address varchar2 (20) );

modifying the table structure


■ alter statement can also be used to redefine the data type,
size and default value of existing columns
■ syntax :
alter table <table_name>
modify
(<column_name> datatype(size) default default_value,
…… );
■ example :
alter table emp
modify (address varchar2 (25) );

■ you can increase the size of a column at any time


■ you can not decrease the size of a column or change
its datatype, unless there is no data in it
■ you can change a column not null to null

■ to change a column from null to not null you must


have a value for each row
■ to change the data type of a column values in the
column must be null in all rows

modifying the table structure


■ you can add one or more columns
■ you can not add a new column that is not null

■ the option is
–make it null first
–subsequently fill the data

–change it to not null

■ you can modify null, not null and default constraint


with the help of modify constraint clause
■ you can not modify rest of the constraints but you
can drop them and add new one

modifying the table structure


■ drop clause of alter table command
–used to remove a constraint from a table
–used to drop column from a table
■ syntax :
alter table <table_name>
drop
[ constraint <constraint_name> ]
[ primary key ]
[ unique <( column, column, …….. )>]
[ cascade ];

modifying the table structure


■ cascade option of drop clause
–removes all the dependent constraints

■ example :
alter table dept
drop primary key
cascade;

■ rename command is used to rename the table

■ syntax :
rename <old_table_name> to <new_table_name>;
■ example :
rename dept to department;

deleting the table structure


■ drop table command is used
■ deletes the table definition and all the rows in the
database
■ syntax :
drop table <table_name>
[ cascade constraints ];
■ example :
drop table emp;

deleting the table structure


■ the cascade constraints clause removes all
referential integrity constraints

■ only the table owner or database administrator can


remove the table

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

constraints and any associated database objects such as


indexes, triggers on the table

You might also like