SQL Commands
(DDL & Constraints)
Prof.B.Saleena
SCSE/VITCC
Components of SQL
Data Definition Language (DDL)
Data Manipulation Language (DML)
Transaction Control Language (TCL)
Data Control Language (DCL)
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Data Definition Language (DDL) - These SQL commands are used
for creating, modifying, and dropping the structure of database
objects. The commands are CREATE, ALTER, DROP, RENAME, and
TRUNCATE.
Data Manipulation Language (DML) - These SQL commands are
used for storing, retrieving, modifying, and deleting data. These
commands are SELECT, INSERT, UPDATE, and DELETE.
Transaction Control Language (TCL) - These SQL commands are
used for managing changes affecting the data. These commands
are COMMIT, ROLLBACK, and SAVEPOINT.
Data Control Language (DCL) - These SQL commands are used for
providing security to database objects. These commands are
GRANT and REVOKE.
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Create table command
CREATE TABLE table_name
( column1 datatype ,Column2 datatype ,
...column n data type);
create table student(regno number , name
varchar2(15) , branch varchar2(5));
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Alter keyword
ALTER TABLE table_name ADD (column_name
datatype[,...]);
ALTER TABLE table_name MODIFY (column_name
datatype[,...]);
ALTER TABLE table_name DROP (column_name
datatype[,...]);
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Examples
alter table student add ( course_code
varchar2(5));
alter table student modify ( branch varchar2(5)
not null);
alter table student drop ( course_code);
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
New version of Alter
ALTER TABLE table_name RENAME COLUMN
old_name to new_name;
ALTER TABLE table_name RENAME to new_name;
Examples:
alter table student rename column branch to
course;
alter table student rename to students;
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Drop & Truncate
Drop table table_name;
Truncate table table_name;
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Constraints
Not null
Primary key
Foreign key
Check
Default
Unique
Constraints can be added using create or alter
command
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
not null constraint
CREATE TABLE table_name
( column1 datatype [null/not null],Column2 datatype
[null/not null], ...column n data type);
create table student(regno number not null,
name varchar2(15) not null, branch varchar2(5));
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Adding constraints with create
CREATE TABLE table_name
( column1 datatype null/not null,
column2 datatype null/not null, ...
CONSTRAINT constraint_name PRIMARY KEY
(column1, column2, ... column_n) );
CREATE TABLE table_name
( column1 datatype primary key,
column2 datatype null/not null, ...);
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Primary key
create table student(regno number primary key,
name varchar2(15) not null, branch varchar2(5));
create table student(regno number not null,
name varchar2(15) not null, branch varchar2(5)
constraint reg_pk primary key(regno));
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Adding constraints with Alter
ALTER TABLE table_name ADD CONSTRAINT
constraint_name PRIMARY KEY (column1,
column2, ... column_n);
ALTER TABLE table_name DROP CONSTRAINT
constraint_name;
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Disable /Enable a Constraint
ALTER TABLE table_name DISABLE CONSTRAINT
constraint_name;
ALTER TABLE table_name ENABLE CONSTRAINT
constraint_name;
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Employee table
CREATE TABLE employees
( employee_number number(10) not null,
employee_name varchar2(50) not null,
department_id number(10),
salary number(6),
CONSTRAINT emp_pk PRIMARY KEY
(employee_number));
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Department table
CREATE TABLE department
( department_id number(10) not null,
department_name varchar2(50) not null,
CONSTRAINT dep_pk PRIMARY KEY
(department_id));
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Foreign key
CREATE TABLE employees (
employee_number number(10) not null,
employee_name varchar2(50) not null,
department_id number(10),
salary number(6),
CONSTRAINT emp_pk PRIMARY KEY (employee_number),
CONSTRAINT dep_fk FOREIGN KEY (department_id)
REFERENCES departments(department_id) );
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Check Constraint
CREATE TABLE employees
( employee_number number(10) not null,
employee_name varchar2(50) not null,
department_id number(10),
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number)
CONSTRAINT empno_ck CHECK(employee_number>100));
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Default Constraint
CREATE TABLE employees
( employee_number number(10) not null,
employee_name varchar2(50) not null,
department_id number(10),
salary number(6),
city varchar2(10) DEFAULT Chennai,
CONSTRAINT employees_pk PRIMARY KEY (employee_number));
18-01-2014 Compiled by B.Saleena/SCSE,VITCC
Unique Constraint
CREATE TABLE employees
( employee_number number(10) not null,
employee_name varchar2(50) not null,
department_id number(10),
salary number(6),
car_no number,
city varchar2(10) DEFAULT Chennai,
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT car_uk UNIQUE(car_no));
18-01-2014 Compiled by B.Saleena/SCSE,VITCC