DDL& DML Commands
DDL& DML Commands
Introduction to SQL :
Structure Query Language(SQL) is a database query language used
for storing and managing data in Relational DBMS. SQL was the first
commercial language introduced for E.F Codd's Relational model of
database. Today almost all DBMS(MySQL, Oracle, Infomix, Sybase,
MS Access) use SQL as the standard database query language. SQL
is used to perform all types of data operations in DBMS.
SQL Command
SQL defines following ways to manipulate data stored in an DBMS.
DDL: Data Definition Language
This includes changes to the structure of the table like creation of
table, altering table, deleting a table etc.
All DDL commands are auto-committed. That means it saves all the
changes permanently in the database.
Command Description
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 1
DATABASE LANGUAGES
DML: Data Manipulation Language
DML commands are used for manipulating the data stored in the table
and not the table itself.
DML commands are not auto-committed. It means changes are not
permanent to database, they can be rolled back.
Command Description
Command Description
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 2
DATABASE LANGUAGES
Command Description
DDL COMMAND :
1.Create queryuery
2.Alter query
Creating a Database
To create a database in DBMS, create command is used. Following is the
syntax,
CREATE DATABASE <DB_NAME>;
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 3
DATABASE LANGUAGES
Creating a Table
create command can also be used to create tables. Now when we
create a table, we have to specify the details of the columns of the
tables too. We can specify the names and datatypes of various
columns in the create command itself.
Following is the syntax,
CREATE TABLE <TABLE_NAME>
column_name1 datatype1,
column_name2 datatype2,
column_name3 datatype3,
column_name4 datatype4
);
create table command will tell the database system to create a new
table with the given table name and column information.
student_id INT,
name VARCHAR(100),
age INT);
The above command will create a new table with name Student in the
current database with 3 columns, namely student_id, name and age.
Where the column student_id will only store integer, name will hold
upto 100 characters and age will again store only integer value.
VARCHAR used for columns which will be used to store characters and
integers, basically a string.
CHAR used for columns which will store char values(single character).
TEXT used for columns which will store text which is generally long in
length. For example, if you create a table for storing profile
information of a social networking website, then for about
me section you can have a column of type TEXT.
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 5
DATABASE LANGUAGES
ALTER Command: Add a new Column
Using ALTER command we can add a column to any existing table.
Following is the syntax,
The above command will add three new columns to the student table
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 6
DATABASE LANGUAGES
ALTER Command: Add Column with default value
ALTER command can add a new column to an existing table with a
default value too. The default value is used when no value is inserted
in the column. Following is the syntax,
ALTER TABLE table_name ADD(
);
);
The above command will add a new column with a pre-set default
value to the table student.
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 7
DATABASE LANGUAGES
ALTER Command: Rename a Column
Using ALTER command you can rename an existing column.
Following is the syntax,
ALTER TABLE table_name RENAME
old_column_name TO new_column_name;
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 8
DATABASE LANGUAGES
TRUNCATE TABLE student;
The above query will delete all the records from the table student.
In DML commands, we will study about the DELETE command
which is also more or less same as the TRUNCATE command. We
will also learn about the difference between the two in that tutorial.
DROP command
DROP command completely removes a table from the database. This
command will also destroy the table structure and the data stored in it.
Following is its syntax,
RENAME Query
RENAME command is used to set a new name for any existing table.
Following is the syntax,
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 9
DATABASE LANGUAGES
DML COMMAND:
INSERT command:
Insert command is used to insert data into a table.
Following is its general syntax,
101 Adam 15
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 10
DATABASE LANGUAGES
Insert NULL value to a column:
Both the statements below will insert NULL value into age column of
the student table.
101 Adam 15
102 Alex
101 Adam 15
102 Alex
103 chris 14
Suppose the column age in our tabel has a default value of 14.
Also, if you run the below query, it will insert default value into the
age column, whatever the default value may be.
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 11
DATABASE LANGUAGES
INSERT INTO Student VALUES(103,'Chris');
UPDATE Command
UPDATE command is used to update any record of data in a table.
Following is its general syntax,
101 Adam 15
102 Alex
103 chris 14
101 Adam 15
102 Alex 18
103 chris 14
In the above statement, if we do not use the WHERE clause, then our
update query will
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 12
DATABASE LANGUAGES
update age for all the columns of the table to 18.
101 Adam 15
102 Alex 18
103 Abhi 17
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 13
DATABASE LANGUAGES
DELETE Command:
DELETE command is used to delete data from a table.
Following is its general syntax,
101 Adam 15
102 Alex 18
103 Abhi 17
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 14
DATABASE LANGUAGES
101 Adam 15
102 Alex 18
TCL COMMAND :
Transaction Control Language(TCL) commands are used to manage
transactions in the database.
COMMIT Command:
COMMIT command is used to permanently save any transaction into the
database.
When we use any DML command like INSERT, UPDATE or DELETE, the
changes made by these commands are not permanent, until the current session is
closed, the changes made by these commands can be rolled back.
To avoid that, we use the COMMIT command to mark the changes as
permanent.
Following is commit command's syntax,
COMMIT;
ROLLBACK Command:
This command restores the database to last commited state. It is also used
with SAVEPOINT command to jump to a savepoint in an ongoing transaction.
If we have used the UPDATE command to make some changes into the
database, and realise that those changes were not required, then we can use
the ROLLBACK command to rollback those changes, if they were not
commited using the COMMIT command.
Following is rollback command's syntax,
ROLLBACK TO savepoint_name;
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 15
DATABASE LANGUAGES
SAVEPOINT Command:
SAVEPOINT command is used to temporarily save a transaction so
that you can rollback to that point whenever required.
Following is savepoint command's syntax,
SAVEPOINT savepoint_name;
In short, using this command we can name the different states of our
data in any table and then rollback to that state using
the ROLLBACK command whenever required.
id name
1 Abhi
2 Adam
4 Alex
Lets use some SQL queries on the above table and see the results.
INSERT INTO class VALUES(5, 'Rahul');
COMMIT;
SAVEPOINT A;
SAVEPOINT B;
SAVEPOINT C;
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 16
DATABASE LANGUAGES
NOTE: SELECT statement is used to show the data stored in the
table.
The resultant table will look like,
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
6 Chris
7 Bravo
Now let's use the ROLLBACK command to roll back the state of data to
the savepoint B.
ROLLBACK TO B;
SELECT * FROM class;
Now our class table will look like,
id name
1 Abhi
2 Adam
Alex
4
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 17
DATABASE LANGUAGES
Abhijit
5
Chris
6
Now let's again use the ROLLBACK command to roll back the state
of data to the savepoint A
ROLLBACK TO A;
SELECT * FROM class;
Now the table will look like,
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
So now you know how the commands COMMIT, ROLLBACK and SAVEPOINT works.
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 18