DDL Commands

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

SQL Commands

(DDL & Constraints)

SCSE/VITCC
Components of SQL

Data Definition Language (DDL)

Data Manipulation Language (DML)

Transaction Control Language (TCL)

Data Control Language (DCL)


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.
Create table command
CREATE TABLE table_name
( column1 datatype ,Column2 datatype , ...column n
data type);

Sql>create table student(regno number , name


varchar2(15) , branch varchar2(5));
sql>desc table_name
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[,...]);
Examples
alter table student add ( course_code
varchar2(5));

alter table student modify ( branch varchar2(5)


not null);

alter table student drop ( course_code);


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;
Drop & Truncate
sql>drop table table_name;
Sql>desc table_name
After dropping a table ,you have to create the
table once again as the schema will be lost.

Sql>truncate table table_name;


Sql>select * from table_name;
DML COMMAND-insert values into
table
Types of 2 insert commands

1.SQL>insert into student


values(10001,reena,cse);
2. SQL>insert into student values(&regno,
&name , &branch);
SQL>/
Every time you want to execute the previous
command use / key.
Copying a Table

If you have a table with columns or records you want to


use without damaging the table, for example to
perform some test, you can copy the table to get a new
one that is similar but only different by name.
To copy a table in SQL, you can use the following
formula:
CREATE TABLE NewTableName AS SELECT * FROM
ExistingTable;
Here is an example:
CREATE TABLE Contractors AS SELECT * FROM
Employees
Default values for values in table
Sometimes most records under a certain column
may hold the same value although just a few
would be different.
In such a case, you can assist the user by
automatically providing a value for that column.
The user would then simply accept the value and
change it only in the rare cases where the value
happen to be different.
To assist the user with this common value, you
create what is referred to as a default value.
This is a new table
SQL> CREATE TABLE Employees (
EmployeeNumber varchar2(6),
FirstName nvarchar2(20) DEFAULT 'John',
LastName varchar2(20) DEFAULT 'Doe',
HourlySalary number(6, 2) DEFAULT 12.50 7 );
>Table created.
During data entry, the user does not have to
provide a value for a column that has a default.
If the user does not provide the value, the default
would be used when the record is saved.
This value can be later changed or updated as
necessary

You might also like