0% found this document useful (0 votes)
10 views18 pages

DDL& DML Commands

The document provides an overview of SQL, including its role as a standard database query language for managing data in relational databases. It details various SQL commands categorized into DDL, DML, TCL, and DCL, explaining their functions and providing syntax examples for creating, altering, and manipulating database tables. Additionally, it covers data types, transaction control, and the significance of commands like COMMIT, ROLLBACK, and SAVEPOINT.

Uploaded by

M. Madhusudhan M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

DDL& DML Commands

The document provides an overview of SQL, including its role as a standard database query language for managing data in relational databases. It details various SQL commands categorized into DDL, DML, TCL, and DCL, explaining their functions and providing syntax examples for creating, altering, and manipulating database tables. Additionally, it covers data types, transaction control, and the significance of commands like COMMIT, ROLLBACK, and SAVEPOINT.

Uploaded by

M. Madhusudhan M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

DATABASE LANGUAGES

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

create to create new table or database

alter for alteration

truncate delete data from table

drop to drop a table

rename to rename a table

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

insert to insert a new row

update to update existing row

delete to delete a row

select retrieve records from one or more table

TCL: Transaction Control Language

Command Description

commit to permanently save

rollback to undo change

savepoint to save temporarily

DCL: Data Control Language


Data control language are the commands to grant and take back
authority from any database user.

MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 2
DATABASE LANGUAGES
Command Description

grant grant permission of right

revoke take back permission.

DDL COMMAND :
1.Create queryuery

2.Alter query

3.Truncate, Drop and Rename query

SQL: create command


create is a DDL SQL command used to create a table or a
database in relational database management system.

Creating a Database
To create a database in DBMS, create command is used. Following is the
syntax,
CREATE DATABASE <DB_NAME>;

Example for creating Database


CREATE DATABASE Test;
The above command will create a database named Test, which will be
an empty schema without any table.
To create tables in this newly created database, we can again use
the create command.

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.

Example for creating Table


CREATE TABLE Student(

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.

Most commonly used datatypes for Table columns


Here we have listed some of the most commonly used datatypes used
for columns in tables.
MUDIMELA MADHUSUDHAN,ASST.PROFESSOR,CMRTC 4
DATABASE LANGUAGES
Datatype Use

INT used for columns which will store integer values.

FLOAT used for columns which will store float values.

DOUBLE used for columns which will store float values.

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).

DATE used for columns which will store date values.

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.

SQL: ALTER command


alter command is used for altering the table structure, such as,

 to add a column to existing table


 to rename any existing column
 to change datatype of any column or to modify its size.
 to drop a column from the table.

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,

ALTER TABLE table_name ADD(column_name datatype);


Here is an Example for this,
ALTER TABLE student ADD(address VARCHAR(200));
The above command will add a new column address to the
table student, which will hold data of type varchar which is nothing
but string, of length 200.

ALTER Command: Add multiple new Columns


Using ALTER command we can even add multiple new columns to
any existing table. Following is the syntax,
ALTER TABLE table_name ADD(
column_name1 datatype1,
column-name2 datatype2,
column-name3 datatype3);

Here is an Example for this,


ALTER TABLE student ADD(
father_name VARCHAR(60),
mother_name VARCHAR(60),
dob DATE);

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(

column-name1 datatype1 DEFAULT some_value

);

Here is an Example for this,


ALTER TABLE student ADD(

dob DATE DEFAULT '01-Jan-99'

);

The above command will add a new column with a pre-set default
value to the table student.

ALTER Command: Modify an existing Column


ALTER command can also be used to modify data type of any
existing column. Following is the syntax,

ALTER TABLE table_name modify(column_name datatype);


Here is an Example for this,
ALTER TABLE student MODIFY(address varchar(300));
Remember we added a new column address in the beginning? The
above command will modify the address column of the student table,
to now hold upto 300 characters.

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;

Here is an example for this,


ALTER TABLE student RENAME address TO location;
The above command will rename address column to location.

ALTER Command: Drop a Column


ALTER command can also be used to drop or remove columns.
Following is the syntax,
ALTER TABLE table_name DROP(column_name);
Here is an example for this,
ALTER TABLE student DROP(address);
The above command will drop the address column from the
table student.

SQL Truncate, Drop or Rename a Table


TRUNCATE command
TRUNCATE command removes all the records from a table. But this
command will not destroy the table's structure. When we
use TRUNCATE command on a table its (auto-increment) primary
key is also initialized. Following is its syntax,

TRUNCATE TABLE table_name


Here is an example explaining it,

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,

DROP TABLE table_name;


Here is an example explaining it,
DROP TABLE student;
The above query will delete the Student table completely. It can also
be used on Databases, to delete the complete database. For example,
to drop a database,

DROP DATABASE Test;


The above query will drop the database with name Test from the
system.

RENAME Query
RENAME command is used to set a new name for any existing table.
Following is the syntax,

RENAME TABLE old_table_name to new_table_name


Here is an example explaining it.
RENAME TABLE student to students_info;
The above query will rename the table student to students_info.

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,

INSERT INTO table_name VALUES(data1, data2, ...);


Lets see an example,
Consider a table student with the following fields.

s_id name age

INSERT INTO student VALUES(101, 'Adam', 15);


The above command will insert a new record into student table.

Insert value into only specific columns:


We can use the INSERT command to insert values for only some
specific columns of a row. We can specify the column names along
with the values to be inserted like this,

INSERT INTO student(id, name) values(102, 'Alex');


The above SQL query will only insert id and name values in the
newly inserted record.

s_id name age

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.

INSERT INTO student(id, name) values(102, 'Alex');


Or,
INSERT INTO Student VALUES(102,'Alex', null);
The above command will insert only two column values and the other
column is set to null.

S_id S_Name age

101 Adam 15

102 Alex

Insert Default value to a column:


INSERT INTO Student VALUES(103,'Chris', default);

S_id S_Name age

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,

UPDATE table_name SET column_name = new_value WHERE


some_condition;
WHERE is used to add a condition to any SQL query, we will soon study about
it in detail.
Lets take a sample table student,

student_id name age

101 Adam 15

102 Alex

103 chris 14

UPDATE student SET age=18 WHERE student_id=102;

S_id S_Name age

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.

Updating Multiple Columns:


We can also update values of multiple columns using a
single UPDATE statement.

UPDATE student SET name='Abhi', age=17 where s_id=103;


The above command will update two columns of the record which
has s_id 103.

s_id name age

101 Adam 15

102 Alex 18

103 Abhi 17

UPDATE Command: Incrementing Integer Value:


When we have to update any integer value in a table, then we can
fetch and update the value in the table in a single statement.
For example, if we have to update the age column of student table
every year for every student, then we can simply run the
following UPDATE statement to perform the following operation:

UPDATE student SET age = age+1;


As you can see, we have used age = age + 1 to increment the value of
age by 1.
NOTE: This style only works for integer values.

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,

DELETE FROM table_name;


Let's take a sample table student:

s_id name age

101 Adam 15

102 Alex 18

103 Abhi 17

Delete all Records from a Table:


DELETE FROM student;
The above command will delete all the records from the table student.

Delete a particular Record from a Table:


In our student table if we want to delete a single record, we can use
the WHERE clause to provide a condition in our DELETE statement.

DELETE FROM student WHERE s_id=103;


The above command will delete the record where s_id is 103 from the
table student.

S_id S_Name age

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.

Using Savepoint and Rollback:


Following is the table class,

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;

UPDATE class SET name = 'Abhijit' WHERE id = '5';

SAVEPOINT A;

INSERT INTO class VALUES(6, 'Chris');

SAVEPOINT B;

INSERT INTO class VALUES(7, 'Bravo');

SAVEPOINT C;

SELECT * FROM class;

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

You might also like