DDL, DML, TCL Commands
DDL, DML, TCL Commands
create command
create is a DDL command used to create a table or a database.
Creating a Database
To create a database in RDBMS, create command is uses. Following is the Syntax,
create database database-name;
Creating a Table
create command is also used to create a table. We can specify names and datatypes of various
columns along.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 given table name
and column information.
The above command will create a new table Student in database system with 3 columns, namely
id, name and age.
alter command
alter command is used for alteration of table structures. There are various uses of alter
command, such as,
Using alter command we can add a column to an existing table. Following is the Syntax,
alter table table-name add(column-name datatype);
The above command will add a new column address to the Student table
Using alter command we can even add multiple columns to an existing table. Following is the
Syntax,
alter table table-name add(column-name1 datatype1, column-name2 datatype2,
column-name3 datatype3);
The above command will add three new columns to the Student table
alter command can add a new column to an existing table with default values. Following is the
Syntax,
alter table table-name add(column-name1 datatype1 default data);
The above command will add a new column with default value to the Student table
alter command is used to modify data type of an existing column . Following is the Syntax,
alter table table-name modify(column-name datatype);
The above command will modify address column of the Student table
To 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 column-name;
To Drop a Column
alter command is also used to drop columns also. Following is the Syntax,
alter table table-name drop(column-name);
The above command will drop address column from the Student table
SQL queries to Truncate, Drop or Rename a Table
truncate command
truncate command removes all records from a table. But this command will not destroy the
table's structure. When we apply truncate command on a table its Primary key is initialized.
Following is its Syntax,
truncate table table-name
The above query will delete all the records of Student table.
truncate command is different from delete command. delete command will delete all the rows
from a table whereas truncate command re-initializes a table(like a newly created table).
For eg. If you have a table with 10 rows and an auto_increment primary key, if you use delete
command to delete all the rows, it will delete all the rows, but will not initialize the primary key,
hence if you will insert any row after using delete command, the auto_increment primary key
will start from 11. But in case of truncate command, primary key is re-initialized.
drop command
drop query completely removes a table from database. This command will also destroy the table
structure. Following is its Syntax,
drop table table-name
The above query will delete the Student table completely. It can also be used on Databases. For
Example, to drop a database,
drop database Test;
The above query will drop a database named Test from the system.
rename query
DML command
Data Manipulation Language (DML) statements are used for managing data in database. DML
commands are not auto-committed. It means changes made by DML command are not
permanent to database, it can be rolled back.
1) INSERT command
Insert command is used to insert data into a table. Following is its general syntax,
INSERT into table-name values(data1,data2,..)
S_Name
age
S_Name
Adam
age
15
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 value other column is set to null.
S_id
S_Name
101
Adam
102
Alex
age
15
S_id
S_Name
101
Adam
102
Alex
103
chris
Suppose the age column of student table has default value of 14.
age
15
14
Also, if you run the below query, it will insert default value into the age column, whatever the
default value may be.
INSERT into Student values(103,'Chris')
2) UPDATE command
Update command is used to update a row of a table. Following is its general syntax,
UPDATE table-name set column-name = value where condition;
S_id
S_Name
age
101
Adam
15
102
Alex
18
103
chris
14
S_Name
age
101
Adam
15
102
Alex
18
103
Abhi
17
3) Delete command
Delete command is used to delete data from a table. Delete command can also be used with
condition to delete a particular row. Following is its general syntax,
DELETE from table-name;
The above command will delete all the records from Student table.
S_Name
age
101
Adam
15
102
Alex
18
103
Abhi
17
The above command will delete the record where s_id is 103 from Student table.
S_id
S_Name
age
101
Adam
15
102
Alex
18
TCL command
Commit command
Rollback command
This command restores the database to last commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.
Following is Rollback command's syntax,
rollback to savepoint-name;
Savepoint command
savepoint command is used to temporarily save a transaction so that you can rollback to that
point whenever necessary.
Following is savepoint command's syntax,
savepoint savepoint-name;
NAME
abhi
adam
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;
NAME
abhi
adam
alex
abhijit
chris
bravo
ID
NAME
abhi
adam
alex
abhijit
chris
NAME
abhi
adam
alex
abhijit