Different types of DBMS languages
DDL(Data definition language)
Data Definition Language(DDL) is used for describing data and its relationship
in a database. It is also used to define the database schema. The commands only
affect the database structure and not the data.
The main DDL commands are create, alter, drop and truncate.
Create Statement
It is used to create a database or table. While creating the table,we specify
table_name,column_name followed by data_types(int,float,varchar,etc) and
constraints(primary key,not null,etc)
Syntax
CREATE TABLE table_name
CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
.....
)
Example
In this example, we are going to create a table along with the column names and
data types.
Algorithm
Step 1 − Use CREATE to create a table
Step 2 − Provide the table name
Step 3 − Provide column name along with their data types
Step 4 − Provide constraints if any
Code
CREATE TABLE students
id INT PRIMARY KEY,
Name VARCHAR(20),
Age INT,
dob DATE
);z
ID Name AGE DOB
Alter Command
It is used to make a change in the structure of a database. Different operations
like adding new columns, removing columns, changing data types or
adding/removing constraints can be performed.
Syntax
ALTER TABLE table_name ADD column_name datatype;
Here,in place of ADD,we can use remove or modify
Example
In this example, we are going to use ALTER to add a column in a table
Algorithm
Step 1 − Use ALTER TABLE
Step 2 − Provide table name
Step 3 − Provide operation to be performed
Input
student
ID Name AGE DOB
Code
ALTER TABLE students#table on which operation is done
ADD roll_no int;#adding column(roll_no) to table
Output
Students
ID Name Age DOB
Truncate Command
It is used to delete the entities inside the table while holding the structure of the
table. It free up the space from the table whereas the column names are left
resulting to store new data in the table.
Syntax
TRUNCATE TABLE table_name;
Example
In this example, we are going to use truncate to remove the data inside the table.
Algorithm
Step 1 − Use truncate table
Step 2 − Provide table name
Input
Students
ID Name AGE DOB
1 Monu 21 5-12-2000
2 Sonu 22 5-12-1999
3 Aman 23 5-12-1998
Code
TRUNCATE TABLE students;#students table is truncated
Output
Students
ID Name Age DOB
Drop Command
It is used to completely delete a table from the database. the table along with the
structure is deleted and can't be found again in the database.
Syntax
DROP TABLE table_name;
Example
In this example, we are going to completely delete a table from the database.
Algorithm
Step 1 − Use drop table
Step 2 − Provide table name to be dropped
Step 3 − Select table to crosscheck
Input
Students
ID Name Age DOB
1 Monu 21 5-12-2000
2 Sonu 22 5-12-1999
3 Aman 23 5-12-1998
Code
DROP TABLE students;#table dropped
SELECT * FROM students;#to show the output
Output
No table is found
DML(Data Manipulation Language)
It refers to a language that is used to insert, delete and update data in a database.
It is used for the manipulation of data in the database.DCL is used together with
DML because the changes made by dml wouldn't be saved permanently and
have a chance of rolling back. Here are three DML commands:
Insert into command
It is used to insert a data row in a table.
Syntax
INSERT INTO table_name(col1,col2,col3,..) VALUES
(value1,value2,value3,...);
or
INSERT INTO table_name VALUES (value1,value2,value3,...);
Example
This example will show the use of the insert command in a table. Insert is used
to provide the values or entities into the table through the insert statement
Algorithm
Step1: Create a table
Step 2: Insert values into the table using INSERT.
Step3: Select the table to show output and to see the values that get
inserted
CREATE TABLE student(id int,name char(50),roll_no. (50),branch
char(50);#table created
INSERT INTO student(id,name,roll_no,branch) VALUES
(1,aman,20,cs),
(2,naman,21,civil), (3,raman,22,bao);#rows are inserted
SELECT * FROM student;#table selected to show output
Output
id name roll_no. branch
1 aman 20 cs
2 naman 21 civil
3 raman 22 bao
Update Command
It is used to update existing data in a table of a database. It can be used to
update single or multiple columns at a time as per the requirement.
Syntax
UPDATE table_name
SET values_to_update(1_col=1_value,2_col=2_value,....)
WHERE condition;
Here,
table_name is the name of the table
1_col,2_col... are the column
1_value,2_value...are the updated value for columns
conditions are there to select the row on which the value will get updated.
Algorithm
Step1: Use update to update the data
Step2: Provide table name
Step3: Set values to get updated
Step4: Provide condition of where to perform updation
Step5: Select the table to show the output
Example
UPDATE student #update operation is to be performed on student table
SET name='monu',roll_no=25#set updated values
WHERE id=1;#condition regarding where to update
SELECT * FROM student;#Select table to show output
Output
id name roll_no. branch
1 monu 25 cs
2 naman 21 civil
3 raman 22 bao
Delete command
It is used to delete records from a table given. One can delete single or multiple
records depending upon the condition provided in the WHERE clause.
Syntax
DELETE FROM table_name WHERE condition;
Here,
table_name is the name of the table
table_name is the name of the table
Example
This example will show how the delete command is used to delete or remove
the existing data present in the table.
Algorithm
Step1: Use delete
Step2: Provide table name
Step3: Provide condition of where to perform the deletion
Step4: Select the table to show the output
Input
id name roll_no. branch
1 monu 25 cs
2 naman 21 civil
3 raman 22 bao
Example
DELETE FROM student#table on which data deletion will occur
WHERE id=1;# condition regarding where to delete
SELECT * FROM student;#Select table to show output
Output
id name roll_no. branch
2 naman 21 civil
3 raman 22 bao